chburhan64 commited on
Commit
519bb2b
·
verified ·
1 Parent(s): f6b431a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +59 -149
app.py CHANGED
@@ -1,20 +1,11 @@
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"):
@@ -28,178 +19,97 @@ def download_audio(youtube_url, output_file="audio.mp3"):
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()
 
 
 
1
  import os
2
  import time
3
+ import yt_dlp
4
+ import requests
 
 
 
 
 
 
 
 
5
 
6
+ # You can replace these URLs with Hugging Face API URLs
7
+ BASE_URL = "https://api-inference.huggingface.co/models/"
8
+ HEADERS = {"Authorization": "Bearer YOUR_HUGGINGFACE_API_KEY"} # Replace with your actual Hugging Face API key
9
 
10
  # ✅ Download YouTube Audio
11
  def download_audio(youtube_url, output_file="audio.mp3"):
 
19
  }],
20
  'quiet': False, # Set to False for more verbose output to help debug
21
  }
22
+
23
  try:
24
  with yt_dlp.YoutubeDL(ydl_opts) as ydl:
25
+ print("Downloading audio...")
26
  ydl.download([youtube_url])
27
+ print(f"Audio downloaded successfully: {output_file}")
 
 
 
 
28
  return output_file
29
  except Exception as e:
30
+ print("Error downloading audio:", str(e))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31
  return None
32
 
33
+ # ✅ Upload Audio to Hugging Face STT Model for Transcription
34
+ def get_transcription(file_path):
35
+ with open(file_path, "rb") as audio_file:
36
+ audio = audio_file.read()
37
 
38
+ data = {"inputs": audio}
 
 
 
 
 
 
 
 
 
 
 
 
 
39
 
 
 
40
  try:
41
+ response = requests.post(f"{BASE_URL}/whisper-large", headers=HEADERS, files={"file": audio})
42
  if response.status_code == 200:
43
+ return response.json()["text"]
44
  else:
45
+ print(f"Error during transcription request: {response.status_code}")
46
  return None
47
  except Exception as e:
48
+ print(f"Error during transcription request: {str(e)}")
49
  return None
50
 
51
+ # ✅ Summarize Transcript using Hugging Face GPT-based Model
52
  def summarize_text(transcript_text):
53
  data = {
54
+ "inputs": f"Summarize the following text:\n\n{transcript_text}",
 
 
 
 
 
55
  }
56
  try:
57
+ response = requests.post(f"{BASE_URL}/gpt2", headers=HEADERS, json=data)
58
  if response.status_code == 200:
59
+ return response.json()[0]['generated_text']
60
  else:
61
+ print(f"Error summarizing transcript: {response.status_code}")
62
  return None
63
  except Exception as e:
64
+ print(f"Error summarizing transcript: {str(e)}")
65
  return None
66
 
67
+ # ✅ Text-to-Speech (TTS) using Hugging Face
68
+ def generate_tts_audio(summary_text):
69
+ data = {"inputs": summary_text}
 
 
 
70
  try:
71
+ response = requests.post(f"{BASE_URL}/tacotron2", headers=HEADERS, json=data)
72
  if response.status_code == 200:
73
+ with open("summary_audio.wav", "wb") as audio_file:
74
+ audio_file.write(response.content)
75
+ print(f"Audio summary saved as: summary_audio.wav")
76
+ return "summary_audio.wav"
77
  else:
78
+ print(f"Error generating TTS audio: {response.status_code}")
79
  return None
80
  except Exception as e:
81
+ print(f"Error generating TTS audio: {str(e)}")
82
  return None
83
 
84
  # ✅ Full Pipeline Execution
85
  def main():
86
+ video_url = input("Please enter the YouTube video URL: ") # User input for YouTube URL
87
+
88
+ # Step 1: Download Audio from YouTube
89
+ audio_file = download_audio(video_url)
90
+ if audio_file:
91
+ # Step 2: Get Transcription from Hugging Face
92
+ transcript = get_transcription(audio_file)
93
+ if transcript:
94
+ print("Transcript:\n", transcript)
95
+
96
+ # Step 3: Summarize the Transcript using Hugging Face Model
97
+ summary = summarize_text(transcript)
98
+ if summary:
99
+ print("\nSummary:\n", summary)
100
+
101
+ # Step 4: Generate TTS Audio for the Summary
102
+ tts_audio = generate_tts_audio(summary)
103
+ if tts_audio:
104
+ print(f"Text-to-Speech audio saved at: {tts_audio}")
 
 
 
 
 
 
 
 
 
 
 
105
  else:
106
+ print("Failed to generate TTS audio.")
107
  else:
108
+ print("Failed to summarize transcript.")
109
  else:
110
+ print("Failed to transcribe the audio.")
111
+ else:
112
+ print("Failed to download audio from the video.")
113
 
114
+ if __name__ == "__main__":
 
115
  main()