Upload 2 files
Browse files- app (2).py +133 -0
- requirements (2).txt +6 -0
app (2).py
ADDED
@@ -0,0 +1,133 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import gradio as gr
|
3 |
+
import requests
|
4 |
+
import json
|
5 |
+
from moviepy import VideoFileClip
|
6 |
+
import uuid
|
7 |
+
|
8 |
+
ELEVENLABS_API_KEY = os.environ.get("ELEVENLABS_API_KEY", None)
|
9 |
+
|
10 |
+
def extract_audio(video_path, output_format="mp3"):
|
11 |
+
if not video_path:
|
12 |
+
return None, "No video provided"
|
13 |
+
|
14 |
+
output_path = f"extracted_audio_{uuid.uuid4().hex[:8]}.{output_format}"
|
15 |
+
|
16 |
+
try:
|
17 |
+
video = VideoFileClip(video_path)
|
18 |
+
video.audio.write_audiofile(output_path, logger=None)
|
19 |
+
video.close()
|
20 |
+
return output_path, f"Audio extracted successfully"
|
21 |
+
except Exception as e:
|
22 |
+
return None, f"Error extracting audio: {str(e)}"
|
23 |
+
|
24 |
+
def save_transcription(transcription):
|
25 |
+
if "error" in transcription:
|
26 |
+
return None, transcription["error"]
|
27 |
+
transcript_filename = f"transcription_{uuid.uuid4().hex[:8]}.txt"
|
28 |
+
|
29 |
+
try:
|
30 |
+
with open(transcript_filename, "w", encoding="utf-8") as f:
|
31 |
+
f.write(transcription.get('text', 'No text found'))
|
32 |
+
|
33 |
+
return transcript_filename, "Transcription saved as text file"
|
34 |
+
except Exception as e:
|
35 |
+
return None, f"Error saving transcription: {str(e)}"
|
36 |
+
|
37 |
+
def process_video_file(video_file, output_format, api_key, model_id):
|
38 |
+
if video_file is None:
|
39 |
+
return None, "Please upload a video file", None, "No video provided"
|
40 |
+
|
41 |
+
audio_path, message = extract_audio(video_file, output_format)
|
42 |
+
|
43 |
+
if audio_path and os.path.exists(audio_path):
|
44 |
+
transcription = transcribe_audio(audio_path, api_key, model_id)
|
45 |
+
transcript_file, transcript_message = save_transcription(transcription)
|
46 |
+
return audio_path, message, transcript_file, transcript_message
|
47 |
+
else:
|
48 |
+
return None, message, None, "Audio extraction failed, cannot transcribe"
|
49 |
+
|
50 |
+
def process_video_url(video_url, output_format, api_key, model_id):
|
51 |
+
if not video_url.strip():
|
52 |
+
return None, "Please enter a video URL", None, "No URL provided"
|
53 |
+
|
54 |
+
video_path, error = download_video_from_url(video_url)
|
55 |
+
if error:
|
56 |
+
return None, error, None, "Video download failed, cannot transcribe"
|
57 |
+
|
58 |
+
audio_path, message = extract_audio(video_path, output_format)
|
59 |
+
if video_path and os.path.exists(video_path):
|
60 |
+
try:
|
61 |
+
os.remove(video_path)
|
62 |
+
except:
|
63 |
+
pass
|
64 |
+
|
65 |
+
if audio_path and os.path.exists(audio_path):
|
66 |
+
transcription = transcribe_audio(audio_path, api_key, model_id)
|
67 |
+
transcript_file, transcript_message = save_transcription(transcription)
|
68 |
+
return audio_path, message, transcript_file, transcript_message
|
69 |
+
else:
|
70 |
+
return None, message, None, "Audio extraction failed, cannot transcribe"
|
71 |
+
|
72 |
+
def transcribe_audio(audio_file, api_key, model_id="scribe_v1"):
|
73 |
+
if not api_key:
|
74 |
+
return {"error": "Please provide an API key"}
|
75 |
+
|
76 |
+
url = "https://api.elevenlabs.io/v1/speech-to-text"
|
77 |
+
headers = {
|
78 |
+
"xi-api-key": api_key
|
79 |
+
}
|
80 |
+
|
81 |
+
try:
|
82 |
+
with open(audio_file, "rb") as f:
|
83 |
+
files = {
|
84 |
+
"file": f,
|
85 |
+
"model_id": (None, model_id)
|
86 |
+
}
|
87 |
+
response = requests.post(url, headers=headers, files=files)
|
88 |
+
response.raise_for_status()
|
89 |
+
result = response.json()
|
90 |
+
return result
|
91 |
+
except requests.exceptions.RequestException as e:
|
92 |
+
return {"error": f"API request failed: {str(e)}"}
|
93 |
+
except json.JSONDecodeError:
|
94 |
+
return {"error": "Failed to parse API response"}
|
95 |
+
|
96 |
+
with gr.Blocks(title="Video to Audio to Transcription") as app:
|
97 |
+
gr.Markdown("# Video => Audio => Transcription")
|
98 |
+
|
99 |
+
api_key = gr.Textbox(
|
100 |
+
placeholder="Enter your ElevenLabs API key",
|
101 |
+
label="ElevenLabs API Key",
|
102 |
+
type="password",
|
103 |
+
value=ELEVENLABS_API_KEY
|
104 |
+
)
|
105 |
+
|
106 |
+
model_id = gr.Dropdown(
|
107 |
+
choices=["scribe_v1", "eleven_turbo_v2"],
|
108 |
+
value="scribe_v1",
|
109 |
+
label="Transcription Model"
|
110 |
+
)
|
111 |
+
|
112 |
+
with gr.Tabs():
|
113 |
+
with gr.TabItem("Upload Video"):
|
114 |
+
with gr.Row():
|
115 |
+
with gr.Column():
|
116 |
+
video_input = gr.Video(label="Upload Video")
|
117 |
+
format_choice_file = gr.Radio(["mp3", "wav"], value="mp3", label="Output Format")
|
118 |
+
extract_button_file = gr.Button("Extract Audio & Transcribe")
|
119 |
+
|
120 |
+
with gr.Column():
|
121 |
+
audio_output_file = gr.Audio(label="Extracted Audio", type="filepath")
|
122 |
+
status_output_file = gr.Textbox(label="Audio Extraction Status")
|
123 |
+
transcript_file_output = gr.File(label="Transcription Text File")
|
124 |
+
transcript_status_output = gr.Textbox(label="Transcription Status")
|
125 |
+
|
126 |
+
extract_button_file.click(
|
127 |
+
fn=process_video_file,
|
128 |
+
inputs=[video_input, format_choice_file, api_key, model_id],
|
129 |
+
outputs=[audio_output_file, status_output_file, transcript_file_output, transcript_status_output]
|
130 |
+
)
|
131 |
+
|
132 |
+
if __name__ == "__main__":
|
133 |
+
app.launch()
|
requirements (2).txt
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
gradio
|
2 |
+
uuid
|
3 |
+
yt-dlp
|
4 |
+
moviepy
|
5 |
+
python-dotenv
|
6 |
+
requests
|