Freddy Boulton commited on
Commit
740c813
·
1 Parent(s): a8b2330
Files changed (2) hide show
  1. app.py +79 -0
  2. requirements.txt +4 -0
app.py ADDED
@@ -0,0 +1,79 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import assemblyai as aai
2
+ import gradio as gr
3
+ import os
4
+ import subprocess
5
+ import uuid
6
+ from google import genai
7
+ from dotenv import load_dotenv
8
+
9
+ load_dotenv()
10
+
11
+ client = genai.Client(api_key=os.getenv("GEMINI_API_KEY"))
12
+
13
+
14
+ aai.settings.api_key = os.getenv("ASSEMBLYAI_API_KEY")
15
+
16
+
17
+
18
+ def _extract_audio_to_mp3(video_file: str) -> str:
19
+ """Extract audio from an mp4 video file to a uniquely named mp3 file using ffmpeg."""
20
+ unique_id = uuid.uuid4().hex
21
+ mp3_file = f"audio_{unique_id}.mp3"
22
+ command = [
23
+ "ffmpeg",
24
+ "-i", video_file,
25
+ "-vn",
26
+ "-acodec", "libmp3lame",
27
+ "-y",
28
+ mp3_file
29
+ ]
30
+ subprocess.run(command, check=True)
31
+ return mp3_file
32
+
33
+
34
+ def get_transcript(video_file: str) -> str:
35
+ """Get a transcript of a video file using assembly ai API.
36
+ Arguments:
37
+ video_file: str - The path to the video file to transcribe.
38
+ Returns:
39
+ transcript: str - The transcript of the video file.
40
+ """
41
+ try:
42
+ audio_file = _extract_audio_to_mp3(video_file)
43
+ config = aai.TranscriptionConfig(speech_model=aai.SpeechModel.best)
44
+ transcript = aai.Transcriber(config=config).transcribe(audio_file)
45
+ return transcript.text or "Could not transcribe video."
46
+ except Exception as e:
47
+ raise gr.Error(f"Error transcribing video: {e}")
48
+
49
+ transcript_iface = gr.Interface(fn=get_transcript, inputs=gr.Video(),
50
+ outputs="text")
51
+
52
+
53
+ def get_initial_post(transcript: str) -> str:
54
+
55
+ response = client.models.generate_content(
56
+ model="gemini-2.5-flash",
57
+ contents=f"""
58
+ You are an expert social media copywriter.
59
+ You are given a transcript of a video and you need to write a post for social media.
60
+ The post should be 280 characters or less so that it can be posted on Twitter without being truncated in the preview.
61
+ The post should include a catchy title that will grab the viewer's attention.
62
+ After the title, include some bullet points that will help the viewer understand the main points of the video.
63
+ Here is the transcript of a video:
64
+ {transcript}
65
+ """,
66
+ )
67
+
68
+ return response.text or "Could not generate post."
69
+
70
+
71
+ post_iface = gr.Interface(fn=get_initial_post, inputs=gr.Textbox(),
72
+ outputs=gr.Textbox())
73
+
74
+
75
+ iface = gr.TabbedInterface([transcript_iface, post_iface], ["Transcript", "Post"])
76
+
77
+
78
+ if __name__ == "__main__":
79
+ iface.launch()
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ assemblyai
2
+ google-genai
3
+ gradio
4
+ python-dotenv