abid-ai commited on
Commit
b62e3d4
Β·
verified Β·
1 Parent(s): 68e247f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -11
app.py CHANGED
@@ -3,7 +3,7 @@ import json
3
  import plotly.express as px
4
  import pandas as pd
5
  from groq import Groq
6
- from fpdf import FPDF
7
  from youtube_comment_downloader import YoutubeCommentDownloader
8
  import re
9
  import os
@@ -65,7 +65,7 @@ def clean_youtube_url(url):
65
  return f"https://www.youtube.com/watch?v={video_id}"
66
  return url
67
 
68
- def fetch_youtube_comments(url, limit=150):
69
  try:
70
  clean_url = clean_youtube_url(url)
71
  downloader = YoutubeCommentDownloader()
@@ -76,14 +76,23 @@ def fetch_youtube_comments(url, limit=150):
76
  break
77
  return comments
78
  except Exception as e:
79
- print(f"Fetch error: {e}")
80
  return []
81
 
 
 
 
 
 
 
 
 
 
82
  def analyze_comments_with_groq(comments, post_context=""):
83
  try:
84
  client = Groq(api_key=GROQ_API_KEY)
85
  comments_text = "\n\n".join([f"Comment {i+1}: {c[:250]}" for i, c in enumerate(comments[:130])])
86
-
87
  user_prompt = f"""
88
  Post Context: {post_context}
89
  Analyze these comments and return proper JSON:
@@ -119,19 +128,19 @@ def create_pdf_report(analysis_result, poll_question):
119
  pdf.set_font('Arial', 'B', 12)
120
  pdf.cell(0, 10, 'Summary:', 0, 1, 'L')
121
  pdf.set_font('Arial', '', 11)
122
- pdf.multi_cell(0, 6, analysis_result.get('summary', 'No summary available.'))
123
  pdf.ln(10)
124
 
125
  pdf.set_font('Arial', 'B', 12)
126
  pdf.cell(0, 10, 'Top Themes:', 0, 1, 'L')
127
  pdf.set_font('Arial', '', 11)
128
- for theme in analysis_result.get('top_themes', [])[:8]:
129
- pdf.cell(0, 6, f"β€’ {theme}", 0, 1, 'L')
130
 
131
  pdf.output("CommentSurvey_Report.pdf")
132
  return "CommentSurvey_Report.pdf"
133
 
134
- # ====================== GRADIO INTERFACE ======================
135
  with gr.Blocks(title="CommentSurvey AI", theme=gr.themes.Soft()) as demo:
136
  gr.Markdown("# πŸ“Š CommentSurvey AI\n**Turn YouTube Comments into Smart Surveys & Insights**")
137
 
@@ -139,7 +148,7 @@ with gr.Blocks(title="CommentSurvey AI", theme=gr.themes.Soft()) as demo:
139
  with gr.Column(scale=4):
140
  url_input = gr.Textbox(
141
  label="🌐 Paste YouTube Video Link",
142
- placeholder="https://youtu.be/xxxxxxxxxx",
143
  lines=1
144
  )
145
  analyze_btn = gr.Button("πŸš€ Analyze Comments", variant="primary", size="large")
@@ -168,9 +177,13 @@ with gr.Blocks(title="CommentSurvey AI", theme=gr.themes.Soft()) as demo:
168
  if not GROQ_API_KEY:
169
  return None, "**❌ Groq API Key is missing!**", None, None, None, None
170
 
171
- comments = fetch_youtube_comments(url)
 
 
 
 
172
  if len(comments) == 0:
173
- return None, "**❌ Could not fetch comments.** Try a different public video.", None, None, None, None
174
 
175
  result = analyze_comments_with_groq(comments)
176
 
 
3
  import plotly.express as px
4
  import pandas as pd
5
  from groq import Groq
6
+ from fpdf2 import FPDF
7
  from youtube_comment_downloader import YoutubeCommentDownloader
8
  import re
9
  import os
 
65
  return f"https://www.youtube.com/watch?v={video_id}"
66
  return url
67
 
68
+ def fetch_youtube_comments(url, limit=120):
69
  try:
70
  clean_url = clean_youtube_url(url)
71
  downloader = YoutubeCommentDownloader()
 
76
  break
77
  return comments
78
  except Exception as e:
79
+ print(f"Error fetching YouTube: {e}")
80
  return []
81
 
82
+ def fetch_comments_from_url(url):
83
+ if not url or not url.strip():
84
+ return []
85
+ url_lower = url.lower()
86
+ if any(x in url_lower for x in ["youtube", "youtu.be", "pie.yt"]):
87
+ return fetch_youtube_comments(url)
88
+ else:
89
+ return ["[Please use a valid YouTube link]"]
90
+
91
  def analyze_comments_with_groq(comments, post_context=""):
92
  try:
93
  client = Groq(api_key=GROQ_API_KEY)
94
  comments_text = "\n\n".join([f"Comment {i+1}: {c[:250]}" for i, c in enumerate(comments[:130])])
95
+
96
  user_prompt = f"""
97
  Post Context: {post_context}
98
  Analyze these comments and return proper JSON:
 
128
  pdf.set_font('Arial', 'B', 12)
129
  pdf.cell(0, 10, 'Summary:', 0, 1, 'L')
130
  pdf.set_font('Arial', '', 11)
131
+ pdf.multi_cell(0, 5, analysis_result.get('summary', 'No summary available.'))
132
  pdf.ln(10)
133
 
134
  pdf.set_font('Arial', 'B', 12)
135
  pdf.cell(0, 10, 'Top Themes:', 0, 1, 'L')
136
  pdf.set_font('Arial', '', 11)
137
+ for theme in analysis_result.get('top_themes', []):
138
+ pdf.cell(0, 7, f'- {theme}', 0, 1, 'L')
139
 
140
  pdf.output("CommentSurvey_Report.pdf")
141
  return "CommentSurvey_Report.pdf"
142
 
143
+ # ====================== GRADIO APP ======================
144
  with gr.Blocks(title="CommentSurvey AI", theme=gr.themes.Soft()) as demo:
145
  gr.Markdown("# πŸ“Š CommentSurvey AI\n**Turn YouTube Comments into Smart Surveys & Insights**")
146
 
 
148
  with gr.Column(scale=4):
149
  url_input = gr.Textbox(
150
  label="🌐 Paste YouTube Video Link",
151
+ placeholder="https://youtu.be/dAAzqMSknkI",
152
  lines=1
153
  )
154
  analyze_btn = gr.Button("πŸš€ Analyze Comments", variant="primary", size="large")
 
177
  if not GROQ_API_KEY:
178
  return None, "**❌ Groq API Key is missing!**", None, None, None, None
179
 
180
+ if not url or not url.strip():
181
+ return None, "**❌ Please paste a YouTube URL**", None, None, None, None
182
+
183
+ comments = fetch_comments_from_url(url)
184
+
185
  if len(comments) == 0:
186
+ return None, "**❌ Could not fetch comments.** Try a different public YouTube video.", None, None, None, None
187
 
188
  result = analyze_comments_with_groq(comments)
189