jaisun2004 commited on
Commit
e6845d8
·
verified ·
1 Parent(s): a442406

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -34
app.py CHANGED
@@ -6,13 +6,20 @@ from keybert import KeyBERT
6
  from fpdf import FPDF
7
  import os
8
 
9
- openai.api_key = os.getenv("OPENAI_API_KEY") # Set this in HF Space secrets
 
10
 
11
  summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
12
  kw_model = KeyBERT()
13
- # Sample brand list for detection (customize as needed)
14
- BRANDS = ["Zerodha", "Motilal", "ICICI", "HDFC", "ShareKhan", "IND Money", "Samsung", "Nike", "Adidas"]
15
 
 
 
 
 
 
 
 
16
  def extract_brands(text):
17
  found = [brand for brand in BRANDS if brand.lower() in text.lower()]
18
  return found if found else ["None detected"]
@@ -27,54 +34,58 @@ def make_bullets(summary):
27
  bullets = [f"- {s.strip()}" for s in sentences if s.strip()]
28
  return "\n".join(bullets)
29
 
30
- def make_str(val):
31
- try:
32
- if val is None:
33
- return ""
34
- if isinstance(val, (bool, int, float)):
35
- return str(val)
36
- if isinstance(val, list):
37
- return "\n".join([make_str(v) for v in val])
38
- if isinstance(val, dict):
39
- return str(val)
40
- return str(val)
41
- except Exception:
42
- return ""
43
-
44
  def create_pdf_report(language, transcript, transcript_en, summary, brands, topics, key_takeaways):
45
  pdf = FPDF()
46
  pdf.add_page()
47
- pdf.set_font("Arial", "B", 16)
 
 
 
48
  pdf.cell(0, 10, "Audio Transcript & Analysis Report", ln=True, align="C")
49
- pdf.set_font("Arial", size=12)
50
  pdf.ln(5)
51
  pdf.cell(0, 10, f"Detected Language: {language}", ln=True)
52
  pdf.ln(5)
53
- pdf.multi_cell(0, 8, "Original Transcript:\n" + transcript)
54
  pdf.ln(3)
55
- pdf.multi_cell(0, 8, "English Transcript:\n" + transcript_en)
56
  pdf.ln(3)
57
- pdf.set_font("Arial", "B", 12)
58
  pdf.cell(0, 10, "Brands Detected:", ln=True)
59
- pdf.set_font("Arial", size=12)
60
  pdf.multi_cell(0, 8, ", ".join(brands))
61
- pdf.set_font("Arial", "B", 12)
62
  pdf.cell(0, 10, "Key Topics:", ln=True)
63
- pdf.set_font("Arial", size=12)
64
  pdf.multi_cell(0, 8, ", ".join(topics))
65
- pdf.set_font("Arial", "B", 12)
66
  pdf.cell(0, 10, "Summary (Bulleted):", ln=True)
67
- pdf.set_font("Arial", size=12)
68
  for takeaway in key_takeaways.split('\n'):
69
  pdf.multi_cell(0, 8, takeaway)
70
- # Save to temporary file
71
  pdf_file = "/tmp/analysis_report.pdf"
72
  pdf.output(pdf_file)
73
  return pdf_file
74
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
75
  def process_audio(audio_path):
76
  if not audio_path or not isinstance(audio_path, str):
77
- return ("No audio file provided.", "", "", "", "", "", "", None)
78
  try:
79
  with open(audio_path, "rb") as audio_file:
80
  transcript = openai.audio.transcriptions.create(
@@ -84,7 +95,7 @@ def process_audio(audio_path):
84
  )
85
  transcript = make_str(transcript).strip()
86
  except Exception as e:
87
- return (make_str(f"Error in transcription: {e}"), "", "", "", "", "", "", None)
88
  try:
89
  detected_lang = detect(transcript)
90
  lang_text = {'en': 'English', 'hi': 'Hindi', 'ta': 'Tamil'}.get(detected_lang, detected_lang)
@@ -101,17 +112,17 @@ def process_audio(audio_path):
101
  )
102
  transcript_en = make_str(transcript_en).strip()
103
  except Exception as e:
104
- transcript_en = make_str(f"Error translating: {e}")
105
  try:
106
  summary_obj = summarizer(transcript_en, max_length=100, min_length=30, do_sample=False)
107
  summary = summary_obj[0]["summary_text"] if isinstance(summary_obj, list) and "summary_text" in summary_obj[0] else make_str(summary_obj)
108
  except Exception as e:
109
- summary = make_str(f"Error summarizing: {e}")
110
  # New: Brands, topics, bullets
111
  brands = extract_brands(transcript_en)
112
  topics = extract_topics(transcript_en)
113
  key_takeaways = make_bullets(summary)
114
- # New: PDF file generation
115
  pdf_file = create_pdf_report(lang_text, transcript, transcript_en, summary, brands, topics, key_takeaways)
116
  return (
117
  lang_text,
@@ -123,6 +134,7 @@ def process_audio(audio_path):
123
  pdf_file
124
  )
125
 
 
126
  iface = gr.Interface(
127
  fn=process_audio,
128
  inputs=gr.Audio(type="filepath", label="Upload MP3/WAV Audio"),
@@ -135,7 +147,7 @@ iface = gr.Interface(
135
  gr.Textbox(label="Bulleted Key Takeaways"),
136
  gr.File(label="Download PDF Report")
137
  ],
138
- title="Audio Transcript, Brand & Topic Analysis (OpenAI Whisper + PDF Download)",
139
  description="Upload your audio file (MP3/WAV). Get full transcript, summary, brand and topic detection, and download results as PDF."
140
  )
141
 
 
6
  from fpdf import FPDF
7
  import os
8
 
9
+ # --- SETUP ---
10
+ openai.api_key = os.getenv("OPENAI_API_KEY") # Set this in your HF Space Secrets
11
 
12
  summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
13
  kw_model = KeyBERT()
14
+ FONT_PATH = "DejaVuSans.ttf" # Upload this to your Space!
 
15
 
16
+ BRANDS = [
17
+ "Apple", "Google", "Microsoft", "Amazon", "Coca-Cola", "Pepsi", "Samsung", "Nike", "Adidas",
18
+ "Meta", "Facebook", "Instagram", "YouTube", "Netflix", "Reliance", "Tata", "Airtel", "Jio",
19
+ "Infosys", "Wipro", "Paytm", "Zomato", "Swiggy", "OLA", "Uber"
20
+ ]
21
+
22
+ # --- HELPERS ---
23
  def extract_brands(text):
24
  found = [brand for brand in BRANDS if brand.lower() in text.lower()]
25
  return found if found else ["None detected"]
 
34
  bullets = [f"- {s.strip()}" for s in sentences if s.strip()]
35
  return "\n".join(bullets)
36
 
37
+ # --- PDF REPORT CREATION ---
 
 
 
 
 
 
 
 
 
 
 
 
 
38
  def create_pdf_report(language, transcript, transcript_en, summary, brands, topics, key_takeaways):
39
  pdf = FPDF()
40
  pdf.add_page()
41
+ # Use DejaVu Unicode font (must be uploaded to Space root)
42
+ pdf.add_font("DejaVu", style="", fname=FONT_PATH, uni=True)
43
+ pdf.add_font("DejaVu", style="B", fname=FONT_PATH, uni=True)
44
+ pdf.set_font("DejaVu", "B", 16)
45
  pdf.cell(0, 10, "Audio Transcript & Analysis Report", ln=True, align="C")
46
+ pdf.set_font("DejaVu", size=12)
47
  pdf.ln(5)
48
  pdf.cell(0, 10, f"Detected Language: {language}", ln=True)
49
  pdf.ln(5)
50
+ pdf.multi_cell(0, 8, "Original Transcript:\n" + (transcript or ""))
51
  pdf.ln(3)
52
+ pdf.multi_cell(0, 8, "English Transcript:\n" + (transcript_en or ""))
53
  pdf.ln(3)
54
+ pdf.set_font("DejaVu", "B", 12)
55
  pdf.cell(0, 10, "Brands Detected:", ln=True)
56
+ pdf.set_font("DejaVu", size=12)
57
  pdf.multi_cell(0, 8, ", ".join(brands))
58
+ pdf.set_font("DejaVu", "B", 12)
59
  pdf.cell(0, 10, "Key Topics:", ln=True)
60
+ pdf.set_font("DejaVu", size=12)
61
  pdf.multi_cell(0, 8, ", ".join(topics))
62
+ pdf.set_font("DejaVu", "B", 12)
63
  pdf.cell(0, 10, "Summary (Bulleted):", ln=True)
64
+ pdf.set_font("DejaVu", size=12)
65
  for takeaway in key_takeaways.split('\n'):
66
  pdf.multi_cell(0, 8, takeaway)
 
67
  pdf_file = "/tmp/analysis_report.pdf"
68
  pdf.output(pdf_file)
69
  return pdf_file
70
 
71
+ def make_str(val):
72
+ try:
73
+ if val is None:
74
+ return ""
75
+ if isinstance(val, (bool, int, float)):
76
+ return str(val)
77
+ if isinstance(val, list):
78
+ return "\n".join([make_str(v) for v in val])
79
+ if isinstance(val, dict):
80
+ return str(val)
81
+ return str(val)
82
+ except Exception:
83
+ return ""
84
+
85
+ # --- MAIN APP FUNCTION ---
86
  def process_audio(audio_path):
87
  if not audio_path or not isinstance(audio_path, str):
88
+ return ("No audio file provided.", "", "", "", "", "", None)
89
  try:
90
  with open(audio_path, "rb") as audio_file:
91
  transcript = openai.audio.transcriptions.create(
 
95
  )
96
  transcript = make_str(transcript).strip()
97
  except Exception as e:
98
+ return (f"Error in transcription: {e}", "", "", "", "", "", None)
99
  try:
100
  detected_lang = detect(transcript)
101
  lang_text = {'en': 'English', 'hi': 'Hindi', 'ta': 'Tamil'}.get(detected_lang, detected_lang)
 
112
  )
113
  transcript_en = make_str(transcript_en).strip()
114
  except Exception as e:
115
+ transcript_en = f"Error translating: {e}"
116
  try:
117
  summary_obj = summarizer(transcript_en, max_length=100, min_length=30, do_sample=False)
118
  summary = summary_obj[0]["summary_text"] if isinstance(summary_obj, list) and "summary_text" in summary_obj[0] else make_str(summary_obj)
119
  except Exception as e:
120
+ summary = f"Error summarizing: {e}"
121
  # New: Brands, topics, bullets
122
  brands = extract_brands(transcript_en)
123
  topics = extract_topics(transcript_en)
124
  key_takeaways = make_bullets(summary)
125
+ # PDF file generation
126
  pdf_file = create_pdf_report(lang_text, transcript, transcript_en, summary, brands, topics, key_takeaways)
127
  return (
128
  lang_text,
 
134
  pdf_file
135
  )
136
 
137
+ # --- GRADIO INTERFACE ---
138
  iface = gr.Interface(
139
  fn=process_audio,
140
  inputs=gr.Audio(type="filepath", label="Upload MP3/WAV Audio"),
 
147
  gr.Textbox(label="Bulleted Key Takeaways"),
148
  gr.File(label="Download PDF Report")
149
  ],
150
+ title="Audio Transcript, Brand & Topic Analysis (OpenAI Whisper + Unicode PDF Download)",
151
  description="Upload your audio file (MP3/WAV). Get full transcript, summary, brand and topic detection, and download results as PDF."
152
  )
153