Sayiqa commited on
Commit
08a338d
·
verified ·
1 Parent(s): 41a7e4a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -18
app.py CHANGED
@@ -134,8 +134,6 @@ from collections import Counter
134
  from googleapiclient.discovery import build
135
 
136
 
137
- YOUTUBE_API_KEY = "AIzaSyD_SDF4lC3vpHVAMnBOcN2ZCTz7dRjUc98" # Replace with your actual API key
138
-
139
  def extract_video_id(url):
140
  match = re.search(r"(?:v=|\/)([0-9A-Za-z_-]{11})", url)
141
  return match.group(1) if match else None
@@ -166,14 +164,10 @@ def extract_subtitle_info(text):
166
  words = text.split()
167
  common_words = Counter(words).most_common(10)
168
  key_topics = ", ".join([word for word, count in common_words])
169
- info = {
170
- "key_topics": key_topics,
171
- "sentence_count": len(sentences),
172
- "word_count": len(words),
173
- }
174
  return info
175
  except Exception as e:
176
- return {"error": str(e)}
177
 
178
  def get_recommendations(keywords, max_results=5):
179
  if not keywords:
@@ -207,12 +201,11 @@ def process_youtube_video(url, keywords):
207
  thumbnail = None
208
  summary = "No transcript available"
209
  sentiment_label = "N/A"
210
- subtitle_info = {}
211
  recommendations = ""
212
 
213
  video_id = extract_video_id(url)
214
  if not video_id:
215
- return None, "Invalid YouTube URL", "N/A", {}, ""
216
 
217
  thumbnail = f"https://img.youtube.com/vi/{video_id}/maxresdefault.jpg"
218
 
@@ -229,7 +222,6 @@ def process_youtube_video(url, keywords):
229
  raise ValueError("Transcript is empty")
230
 
231
  cleaned_text = clean_text_for_analysis(text)
232
- subtitle_info = extract_subtitle_info(cleaned_text)
233
 
234
  sentiment = TextBlob(cleaned_text).sentiment
235
  sentiment_label = f"{'Positive' if sentiment.polarity > 0 else 'Negative' if sentiment.polarity < 0 else 'Neutral'} ({sentiment.polarity:.2f})"
@@ -244,13 +236,38 @@ def process_youtube_video(url, keywords):
244
  if keywords.strip():
245
  recommendations = get_recommendations(keywords)
246
 
247
- return thumbnail, summary, sentiment_label, subtitle_info, recommendations
248
 
249
  except Exception as e:
250
- return None, f"Error: {str(e)}", "N/A", {}, ""
251
-
252
-
253
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
254
 
255
  # Gradio Interface
256
  with gr.Blocks(theme=gr.themes.Soft()) as app:
@@ -396,5 +413,4 @@ with gr.Blocks(theme=gr.themes.Soft()) as app:
396
  )
397
 
398
  if __name__ == "__main__":
399
- app.launch()
400
-
 
134
  from googleapiclient.discovery import build
135
 
136
 
 
 
137
  def extract_video_id(url):
138
  match = re.search(r"(?:v=|\/)([0-9A-Za-z_-]{11})", url)
139
  return match.group(1) if match else None
 
164
  words = text.split()
165
  common_words = Counter(words).most_common(10)
166
  key_topics = ", ".join([word for word, count in common_words])
167
+ info = f"Key topics discussed: {key_topics}. \nNumber of sentences: {len(sentences)}. \nTotal words: {len(words)}."
 
 
 
 
168
  return info
169
  except Exception as e:
170
+ return f"Error extracting subtitle information: {str(e)}"
171
 
172
  def get_recommendations(keywords, max_results=5):
173
  if not keywords:
 
201
  thumbnail = None
202
  summary = "No transcript available"
203
  sentiment_label = "N/A"
 
204
  recommendations = ""
205
 
206
  video_id = extract_video_id(url)
207
  if not video_id:
208
+ return None, "Invalid YouTube URL", "N/A", "", ""
209
 
210
  thumbnail = f"https://img.youtube.com/vi/{video_id}/maxresdefault.jpg"
211
 
 
222
  raise ValueError("Transcript is empty")
223
 
224
  cleaned_text = clean_text_for_analysis(text)
 
225
 
226
  sentiment = TextBlob(cleaned_text).sentiment
227
  sentiment_label = f"{'Positive' if sentiment.polarity > 0 else 'Negative' if sentiment.polarity < 0 else 'Neutral'} ({sentiment.polarity:.2f})"
 
236
  if keywords.strip():
237
  recommendations = get_recommendations(keywords)
238
 
239
+ return thumbnail, summary, sentiment_label, recommendations
240
 
241
  except Exception as e:
242
+ return None, f"Error: {str(e)}", "N/A", ""
243
+
244
+
245
+ # def get_recommendations(keywords, max_results=5):
246
+ # if not keywords:
247
+ # return "Please provide search keywords"
248
+ # try:
249
+ # response = requests.get(
250
+ # "https://www.googleapis.com/youtube/v3/search",
251
+ # params={
252
+ # "part": "snippet",
253
+ # "q": f"educational {keywords}",
254
+ # "type": "video",
255
+ # "maxResults": max_results,
256
+ # "relevanceLanguage": "en",
257
+ # "key": YOUTUBE_API_KEY
258
+ # }
259
+ # ).json()
260
+
261
+ # results = []
262
+ # for item in response.get("items", []):
263
+ # title = item["snippet"]["title"]
264
+ # channel = item["snippet"]["channelTitle"]
265
+ # video_id = item["id"]["videoId"]
266
+ # results.append(f"📺 {title}\n👤 {channel}\n🔗 https://youtube.com/watch?v={video_id}\n")
267
+
268
+ # return "\n".join(results) if results else "No recommendations found"
269
+ # except Exception as e:
270
+ # return f"Error: {str(e)}"
271
 
272
  # Gradio Interface
273
  with gr.Blocks(theme=gr.themes.Soft()) as app:
 
413
  )
414
 
415
  if __name__ == "__main__":
416
+ app.launch()