abid-ai commited on
Commit
aaadeb9
Β·
verified Β·
1 Parent(s): b0bdd70

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -9
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 fpdf2 import FPDF
7
  from youtube_comment_downloader import YoutubeCommentDownloader
8
  import re
9
  import os
@@ -12,14 +12,23 @@ import warnings
12
  warnings.filterwarnings("ignore")
13
 
14
  # ====================== CONFIG ======================
15
- GROQ_API_KEY = os.getenv("GROQ_API_KEY")
 
 
 
 
 
 
 
 
 
16
 
17
  # ====================== SYSTEM PROMPT ======================
18
  SYSTEM_PROMPT = """
19
  You are an expert social media sentiment and poll analysis AI.
20
  Focus on Yes/No, Agree/Disagree, Support/Oppose, and sentiment.
21
 
22
- Handle English + Urdu well.
23
  Return ONLY valid JSON in this exact format:
24
  {
25
  "main_poll": {
@@ -45,6 +54,18 @@ Return ONLY valid JSON in this exact format:
45
  }
46
  """
47
 
 
 
 
 
 
 
 
 
 
 
 
 
48
  # ====================== HELPER FUNCTIONS ======================
49
  def extract_youtube_id(url):
50
  patterns = [
@@ -82,7 +103,9 @@ def fetch_youtube_comments(url, limit=150):
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}
@@ -114,20 +137,20 @@ def create_pdf_report(analysis_result, poll_question):
114
  pdf.ln(10)
115
 
116
  pdf.set_font('Arial', 'B', 12)
117
- pdf.cell(0, 10, f'Poll Question: {poll_question}', 0, 1, 'L')
118
  pdf.ln(5)
119
 
120
  pdf.set_font('Arial', 'B', 12)
121
  pdf.cell(0, 10, 'Summary:', 0, 1, 'L')
122
  pdf.set_font('Arial', '', 11)
123
- pdf.multi_cell(0, 5, analysis_result.get('summary', 'No summary available.'))
124
  pdf.ln(10)
125
 
126
  pdf.set_font('Arial', 'B', 12)
127
  pdf.cell(0, 10, 'Top Themes:', 0, 1, 'L')
128
  pdf.set_font('Arial', '', 11)
129
  for theme in analysis_result.get('top_themes', []):
130
- pdf.cell(0, 7, f'β€’ {theme}', 0, 1, 'L')
131
 
132
  pdf.output("CommentSurvey_Report.pdf")
133
  return "CommentSurvey_Report.pdf"
@@ -136,7 +159,7 @@ def create_pdf_report(analysis_result, poll_question):
136
  def analyze(url):
137
  try:
138
  if not GROQ_API_KEY:
139
- return None, "**❌ Groq API Key is missing!** Add it in Space Secrets.", None, None, None, None
140
 
141
  if not url or not url.strip():
142
  return None, "**❌ Please paste a YouTube URL**", None, None, None, None
@@ -180,7 +203,7 @@ def analyze(url):
180
  except Exception as e:
181
  return None, f"**❌ Error:** {str(e)}", None, None, None, None
182
 
183
- # ====================== GRADIO INTERFACE ======================
184
  with gr.Blocks(title="CommentSurvey AI", theme=gr.themes.Soft()) as demo:
185
  gr.Markdown("# πŸ“Š CommentSurvey AI\n**Turn YouTube Comments into Smart Surveys & Insights**")
186
 
 
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
 
12
  warnings.filterwarnings("ignore")
13
 
14
  # ====================== CONFIG ======================
15
+ #GROQ_API_KEY = os.getenv("translapikey") or os.getenv("GROQ_API_KEY")
16
+
17
+ from google.colab import userdata
18
+ GROQ_API_KEY = userdata.get('commentsurveyapi')
19
+
20
+
21
+ if not GROQ_API_KEY:
22
+ print("❌ API Key not found!")
23
+ else:
24
+ print("βœ… API Key loaded successfully!")
25
 
26
  # ====================== SYSTEM PROMPT ======================
27
  SYSTEM_PROMPT = """
28
  You are an expert social media sentiment and poll analysis AI.
29
  Focus on Yes/No, Agree/Disagree, Support/Oppose, and sentiment.
30
 
31
+ Handle English + Urdu + Hindi + other languages well.
32
  Return ONLY valid JSON in this exact format:
33
  {
34
  "main_poll": {
 
54
  }
55
  """
56
 
57
+ # ====================== TEXT CLEANING FOR UNICODE ======================
58
+ def clean_text(text):
59
+ if not text:
60
+ return ""
61
+ # Replace problematic characters
62
+ text = re.sub(r'[\u2022\u2023\u25CF\u25BA\u25C4]', '-', text) # bullets
63
+ text = re.sub(r'[\u2018\u2019\u201C\u201D]', '"', text) # quotes
64
+ text = re.sub(r'[\u2013\u2014]', '-', text) # dashes
65
+ # Remove any remaining control characters
66
+ text = re.sub(r'[\x00-\x08\x0B\x0C\x0E-\x1F\x7F]', '', text)
67
+ return text.strip()
68
+
69
  # ====================== HELPER FUNCTIONS ======================
70
  def extract_youtube_id(url):
71
  patterns = [
 
103
  def analyze_comments_with_groq(comments, post_context=""):
104
  try:
105
  client = Groq(api_key=GROQ_API_KEY)
106
+ # Clean comments before sending to Groq
107
+ cleaned_comments = [clean_text(c) for c in comments]
108
+ comments_text = "\n\n".join([f"Comment {i+1}: {c[:250]}" for i, c in enumerate(cleaned_comments[:130])])
109
 
110
  user_prompt = f"""
111
  Post Context: {post_context}
 
137
  pdf.ln(10)
138
 
139
  pdf.set_font('Arial', 'B', 12)
140
+ pdf.cell(0, 10, clean_text(f'Poll Question: {poll_question}'), 0, 1, 'L')
141
  pdf.ln(5)
142
 
143
  pdf.set_font('Arial', 'B', 12)
144
  pdf.cell(0, 10, 'Summary:', 0, 1, 'L')
145
  pdf.set_font('Arial', '', 11)
146
+ pdf.multi_cell(0, 5, clean_text(analysis_result.get('summary', 'No summary available.')))
147
  pdf.ln(10)
148
 
149
  pdf.set_font('Arial', 'B', 12)
150
  pdf.cell(0, 10, 'Top Themes:', 0, 1, 'L')
151
  pdf.set_font('Arial', '', 11)
152
  for theme in analysis_result.get('top_themes', []):
153
+ pdf.cell(0, 7, clean_text(f'β€’ {theme}'), 0, 1, 'L')
154
 
155
  pdf.output("CommentSurvey_Report.pdf")
156
  return "CommentSurvey_Report.pdf"
 
159
  def analyze(url):
160
  try:
161
  if not GROQ_API_KEY:
162
+ return None, "**❌ Groq API Key is missing!**", None, None, None, None
163
 
164
  if not url or not url.strip():
165
  return None, "**❌ Please paste a YouTube URL**", None, None, None, None
 
203
  except Exception as e:
204
  return None, f"**❌ Error:** {str(e)}", None, None, None, None
205
 
206
+ # ====================== GRADIO UI ======================
207
  with gr.Blocks(title="CommentSurvey AI", theme=gr.themes.Soft()) as demo:
208
  gr.Markdown("# πŸ“Š CommentSurvey AI\n**Turn YouTube Comments into Smart Surveys & Insights**")
209