chburhan64 commited on
Commit
fe4da73
·
verified ·
1 Parent(s): 4d93edd

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +205 -0
app.py ADDED
@@ -0,0 +1,205 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import yt_dlp
2
+ import requests
3
+ import os
4
+ import time
5
+ from google.oauth2.credentials import Credentials
6
+ from google_auth_oauthlib.flow import InstalledAppFlow
7
+ from google.auth.transport.requests import Request
8
+ from googleapiclient.discovery import build
9
+ from googleapiclient.http import MediaFileUpload
10
+ import streamlit as st
11
+
12
+ API_KEY = "d0ec0d1455bc43a48b6596efb16abcd2" # ← Replace with your actual API key
13
+ BASE_URL = "https://api.aimlapi.com/v1"
14
+ HEADERS = {"Authorization": f"Bearer {API_KEY}"}
15
+
16
+ # Streamlit UI
17
+ st.title("YouTube Video to Audio and Transcription")
18
+
19
+ # ✅ Download YouTube Audio
20
+ def download_audio(youtube_url, output_file="audio.mp3"):
21
+ ydl_opts = {
22
+ 'format': 'bestaudio/best',
23
+ 'outtmpl': output_file,
24
+ 'postprocessors': [{
25
+ 'key': 'FFmpegExtractAudio',
26
+ 'preferredcodec': 'mp3',
27
+ 'preferredquality': '192',
28
+ }],
29
+ 'quiet': False, # Set to False for more verbose output to help debug
30
+ }
31
+
32
+ try:
33
+ with yt_dlp.YoutubeDL(ydl_opts) as ydl:
34
+ st.write("Downloading audio...")
35
+ ydl.download([youtube_url])
36
+ # Fixing the extension issue by renaming the file to 'audio.mp3'
37
+ if os.path.exists("audio.mp3.mp3"):
38
+ os.rename("audio.mp3.mp3", "audio.mp3")
39
+ st.write(f"Renamed file to audio.mp3")
40
+ st.success(f"Audio downloaded successfully: {output_file}")
41
+ return output_file
42
+ except Exception as e:
43
+ st.error(f"Error downloading audio: {str(e)}")
44
+ return None
45
+
46
+
47
+ # Function to upload file to Google Drive
48
+ def upload_to_google_drive(file_path, credentials_file="credentials.json"):
49
+ SCOPES = ['https://www.googleapis.com/auth/drive.file']
50
+ creds = None
51
+ if os.path.exists('token.json'):
52
+ creds = Credentials.from_authorized_user_file('token.json', SCOPES)
53
+
54
+ if not creds or not creds.valid:
55
+ if creds and creds.expired and creds.refresh_token:
56
+ creds.refresh(Request())
57
+ else:
58
+ flow = InstalledAppFlow.from_client_secrets_file(credentials_file, SCOPES)
59
+ creds = flow.run_local_server(port=0)
60
+
61
+ with open('token.json', 'w') as token:
62
+ token.write(creds.to_json())
63
+
64
+ try:
65
+ drive_service = build('drive', 'v3', credentials=creds)
66
+ file_metadata = {'name': os.path.basename(file_path)}
67
+ media = MediaFileUpload(file_path, mimetype='audio/mp3')
68
+
69
+ file = drive_service.files().create(body=file_metadata, media_body=media, fields='id').execute()
70
+
71
+ st.success(f"File uploaded successfully: https://drive.google.com/file/d/{file['id']}/view")
72
+ return f"https://drive.google.com/file/d/{file['id']}/view"
73
+
74
+ except Exception as e:
75
+ st.error(f"Error uploading to Google Drive: {e}")
76
+ return None
77
+
78
+ # Updated function to handle upload to STT (using Google Drive URL)
79
+ def upload_audio_to_stt(file_path):
80
+ file_url = upload_to_google_drive(file_path)
81
+
82
+ if file_url:
83
+ st.success(f"File uploaded successfully to Google Drive: {file_url}")
84
+ return file_url
85
+ else:
86
+ st.error("Failed to upload audio to Google Drive.")
87
+ return None
88
+
89
+ def get_stt_result(file_url):
90
+ if file_url is None:
91
+ st.error("No valid file URL received. Skipping transcription.")
92
+ return None
93
+
94
+ data = {
95
+ "model": "#g1_whisper-small",
96
+ "url": file_url
97
+ }
98
+ try:
99
+ response = requests.post(f"{BASE_URL}/stt/create", headers=HEADERS, json=data)
100
+ if response.status_code == 200:
101
+ return response.json().get("generation_id")
102
+ else:
103
+ st.error(f"Error during transcription request: {response.status_code}")
104
+ return None
105
+ except Exception as e:
106
+ st.error(f"Error during transcription request: {str(e)}")
107
+ return None
108
+
109
+ def get_transcription_result(gen_id):
110
+ time.sleep(10) # Adjust the wait time based on the API's response time
111
+ try:
112
+ response = requests.get(f"{BASE_URL}/stt/{gen_id}", headers=HEADERS)
113
+ if response.status_code == 200:
114
+ return response.json().get("result")
115
+ else:
116
+ st.error(f"Error retrieving transcription result: {response.status_code}")
117
+ return None
118
+ except Exception as e:
119
+ st.error(f"Error getting transcription result: {str(e)}")
120
+ return None
121
+
122
+ # ✅ Summarize Transcript
123
+ def summarize_text(transcript_text):
124
+ data = {
125
+ "model": "gpt-4o",
126
+ "messages": [
127
+ {"role": "system", "content": "You are a helpful assistant who summarizes YouTube videos."},
128
+ {"role": "user", "content": f"Summarize the following video transcript:\n\n{transcript_text}"}
129
+ ],
130
+ "temperature": 0.7
131
+ }
132
+ try:
133
+ response = requests.post(f"{BASE_URL}/chat/completions", headers=HEADERS, json=data)
134
+ if response.status_code == 200:
135
+ return response.json()["choices"][0]["message"]["content"]
136
+ else:
137
+ st.error(f"Error summarizing transcript: {response.status_code}")
138
+ return None
139
+ except Exception as e:
140
+ st.error(f"Error summarizing transcript: {str(e)}")
141
+ return None
142
+
143
+ # ✅ Text-to-Speech
144
+ def generate_tts_audio(text, output_file="summary.mp3"):
145
+ data = {
146
+ "model": "#g1_aura-angus-en",
147
+ "input": text
148
+ }
149
+ try:
150
+ response = requests.post(f"{BASE_URL}/tts", headers=HEADERS, json=data)
151
+ if response.status_code == 200:
152
+ with open(output_file, "wb") as f:
153
+ f.write(response.content)
154
+ st.success(f"Audio summary saved as: {output_file}")
155
+ return output_file
156
+ else:
157
+ st.error(f"Error generating TTS audio: {response.status_code}")
158
+ return None
159
+ except Exception as e:
160
+ st.error(f"Error generating TTS audio: {str(e)}")
161
+ return None
162
+
163
+ # ✅ Full Pipeline Execution
164
+ def main():
165
+ video_url = st.text_input("Please enter the YouTube video URL:")
166
+
167
+ if video_url:
168
+ # Step 1: Download Audio from YouTube
169
+ audio_file = download_audio(video_url)
170
+ if audio_file:
171
+ # Step 2: Upload the Audio for Transcription
172
+ file_url = upload_audio_to_stt(audio_file)
173
+ if file_url:
174
+ st.write("Transcribing...")
175
+ # Step 3: Get Transcription ID
176
+ gen_id = get_stt_result(file_url)
177
+ if gen_id:
178
+ st.write("Transcription ID generated.")
179
+ # Step 4: Get the Transcription
180
+ transcript = get_transcription_result(gen_id)
181
+ if transcript:
182
+ st.write("Transcript:\n", transcript)
183
+
184
+ # Step 5: Summarize the Transcript
185
+ summary = summarize_text(transcript)
186
+ if summary:
187
+ st.write("\nSummary:\n", summary)
188
+
189
+ # Step 6: Generate TTS Audio for the Summary
190
+ summary_audio = generate_tts_audio(summary)
191
+ else:
192
+ st.error("Failed to generate summary.")
193
+ else:
194
+ st.error("Failed to retrieve transcription result.")
195
+ else:
196
+ st.error("Failed to generate transcription ID.")
197
+ else:
198
+ st.error("Failed to upload audio to STT service.")
199
+ else:
200
+ st.error("Failed to download audio from the video.")
201
+
202
+
203
+ # Run the full pipeline when the button is clicked
204
+ if st.button('Start Processing'):
205
+ main()