Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
import assemblyai as aai | |
import gradio as gr | |
import os | |
import subprocess | |
import uuid | |
from google import genai | |
from dotenv import load_dotenv | |
load_dotenv() | |
client = genai.Client(api_key=os.getenv("GEMINI_API_KEY")) | |
aai.settings.api_key = os.getenv("ASSEMBLYAI_API_KEY") | |
def _extract_audio_to_mp3(video_file: str) -> str: | |
"""Extract audio from an mp4 video file to a uniquely named mp3 file using ffmpeg.""" | |
unique_id = uuid.uuid4().hex | |
mp3_file = f"audio_{unique_id}.mp3" | |
command = [ | |
"ffmpeg", | |
"-i", video_file, | |
"-vn", | |
"-acodec", "libmp3lame", | |
"-y", | |
mp3_file | |
] | |
subprocess.run(command, check=True) | |
return mp3_file | |
def get_transcript(video_file: str) -> str: | |
"""Get a transcript of a video file using assembly ai API. | |
Arguments: | |
video_file: str - The path to the video file to transcribe. | |
Returns: | |
transcript: str - The transcript of the video file. | |
""" | |
try: | |
audio_file = _extract_audio_to_mp3(video_file) | |
config = aai.TranscriptionConfig(speech_model=aai.SpeechModel.best) | |
transcript = aai.Transcriber(config=config).transcribe(audio_file) | |
return transcript.text or "Could not transcribe video." | |
except Exception as e: | |
raise gr.Error(f"Error transcribing video: {e}") | |
transcript_iface = gr.Interface(fn=get_transcript, inputs=gr.Video(), | |
outputs="text") | |
def get_initial_post(transcript: str) -> str: | |
"""Given a video transcript, draft an engaging social media post | |
Arguments: | |
transcript: The transcript of the video | |
Returns: | |
Social Media Post | |
""" | |
response = client.models.generate_content( | |
model="gemini-2.5-flash", | |
contents=f""" | |
You are an expert social media copywriter. | |
You are given a transcript of a video and you need to write a post for social media. | |
The post should be 280 characters or less so that it can be posted on Twitter without being truncated in the preview. | |
The post should include a catchy title that will grab the viewer's attention. | |
After the title, include some bullet points that will help the viewer understand the main points of the video. | |
Here is the transcript of a video: | |
{transcript} | |
""", | |
) | |
return response.text or "Could not generate post." | |
post_iface = gr.Interface(fn=get_initial_post, inputs=gr.Textbox(), | |
outputs=gr.Textbox()) | |
iface = gr.TabbedInterface([transcript_iface, post_iface], ["Transcript", "Post"]) | |
if __name__ == "__main__": | |
iface.launch(mcp_server=True) |