Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -5,13 +5,14 @@ from transformers import pipeline
|
|
5 |
from keybert import KeyBERT
|
6 |
from fpdf import FPDF
|
7 |
import os
|
|
|
8 |
|
9 |
# --- SETUP ---
|
10 |
-
openai.api_key = os.getenv("OPENAI_API_KEY") # Set
|
11 |
|
12 |
summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
|
13 |
kw_model = KeyBERT()
|
14 |
-
FONT_PATH = "DejaVuSans.ttf" #
|
15 |
|
16 |
BRANDS = [
|
17 |
"Apple", "Google", "Microsoft", "Amazon", "Coca-Cola", "Pepsi", "Samsung", "Nike", "Adidas",
|
@@ -19,7 +20,6 @@ BRANDS = [
|
|
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,11 +34,35 @@ def make_bullets(summary):
|
|
34 |
bullets = [f"- {s.strip()}" for s in sentences if s.strip()]
|
35 |
return "\n".join(bullets)
|
36 |
|
37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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)
|
@@ -47,42 +71,27 @@ def create_pdf_report(language, transcript, transcript_en, summary, brands, topi
|
|
47 |
pdf.ln(5)
|
48 |
pdf.cell(0, 10, f"Detected Language: {language}", ln=True)
|
49 |
pdf.ln(5)
|
50 |
-
pdf
|
51 |
pdf.ln(3)
|
52 |
-
pdf
|
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
|
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
|
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
|
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)
|
@@ -118,11 +127,9 @@ def process_audio(audio_path):
|
|
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,7 +141,6 @@ def process_audio(audio_path):
|
|
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"),
|
@@ -148,7 +154,7 @@ iface = gr.Interface(
|
|
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
|
152 |
)
|
153 |
|
154 |
iface.launch()
|
|
|
5 |
from keybert import KeyBERT
|
6 |
from fpdf import FPDF
|
7 |
import os
|
8 |
+
import re
|
9 |
|
10 |
# --- SETUP ---
|
11 |
+
openai.api_key = os.getenv("OPENAI_API_KEY") # Set in HF Space Secrets
|
12 |
|
13 |
summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
|
14 |
kw_model = KeyBERT()
|
15 |
+
FONT_PATH = "DejaVuSans.ttf" # Must be uploaded to Space root!
|
16 |
|
17 |
BRANDS = [
|
18 |
"Apple", "Google", "Microsoft", "Amazon", "Coca-Cola", "Pepsi", "Samsung", "Nike", "Adidas",
|
|
|
20 |
"Infosys", "Wipro", "Paytm", "Zomato", "Swiggy", "OLA", "Uber"
|
21 |
]
|
22 |
|
|
|
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 |
+
def make_str(val):
|
38 |
+
try:
|
39 |
+
if val is None:
|
40 |
+
return ""
|
41 |
+
if isinstance(val, (bool, int, float)):
|
42 |
+
return str(val)
|
43 |
+
if isinstance(val, list):
|
44 |
+
return "\n".join([make_str(v) for v in val])
|
45 |
+
if isinstance(val, dict):
|
46 |
+
return str(val)
|
47 |
+
return str(val)
|
48 |
+
except Exception:
|
49 |
+
return ""
|
50 |
+
|
51 |
+
def safe_multicell(pdf, text, w=0, h=8):
|
52 |
+
"""Safely adds text to PDF, handling super long words."""
|
53 |
+
if not isinstance(text, str):
|
54 |
+
text = str(text)
|
55 |
+
# Split very long words (>80 chars) to avoid fpdf2 crash
|
56 |
+
def break_long_words(t, maxlen=80):
|
57 |
+
return re.sub(r'(\S{%d,})' % maxlen,
|
58 |
+
lambda m: ' '.join([m.group(0)[i:i+maxlen] for i in range(0, len(m.group(0)), maxlen)]),
|
59 |
+
t)
|
60 |
+
text = break_long_words(text)
|
61 |
+
pdf.multi_cell(w, h, text)
|
62 |
+
|
63 |
def create_pdf_report(language, transcript, transcript_en, summary, brands, topics, key_takeaways):
|
64 |
pdf = FPDF()
|
65 |
pdf.add_page()
|
|
|
66 |
pdf.add_font("DejaVu", style="", fname=FONT_PATH, uni=True)
|
67 |
pdf.add_font("DejaVu", style="B", fname=FONT_PATH, uni=True)
|
68 |
pdf.set_font("DejaVu", "B", 16)
|
|
|
71 |
pdf.ln(5)
|
72 |
pdf.cell(0, 10, f"Detected Language: {language}", ln=True)
|
73 |
pdf.ln(5)
|
74 |
+
safe_multicell(pdf, "Original Transcript:\n" + (transcript or ""))
|
75 |
pdf.ln(3)
|
76 |
+
safe_multicell(pdf, "English Transcript:\n" + (transcript_en or ""))
|
77 |
pdf.ln(3)
|
78 |
pdf.set_font("DejaVu", "B", 12)
|
79 |
pdf.cell(0, 10, "Brands Detected:", ln=True)
|
80 |
pdf.set_font("DejaVu", size=12)
|
81 |
+
safe_multicell(pdf, ", ".join(brands))
|
82 |
pdf.set_font("DejaVu", "B", 12)
|
83 |
pdf.cell(0, 10, "Key Topics:", ln=True)
|
84 |
pdf.set_font("DejaVu", size=12)
|
85 |
+
safe_multicell(pdf, ", ".join(topics))
|
86 |
pdf.set_font("DejaVu", "B", 12)
|
87 |
pdf.cell(0, 10, "Summary (Bulleted):", ln=True)
|
88 |
pdf.set_font("DejaVu", size=12)
|
89 |
for takeaway in key_takeaways.split('\n'):
|
90 |
+
safe_multicell(pdf, takeaway)
|
91 |
pdf_file = "/tmp/analysis_report.pdf"
|
92 |
pdf.output(pdf_file)
|
93 |
return pdf_file
|
94 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
95 |
def process_audio(audio_path):
|
96 |
if not audio_path or not isinstance(audio_path, str):
|
97 |
return ("No audio file provided.", "", "", "", "", "", None)
|
|
|
127 |
summary = summary_obj[0]["summary_text"] if isinstance(summary_obj, list) and "summary_text" in summary_obj[0] else make_str(summary_obj)
|
128 |
except Exception as e:
|
129 |
summary = f"Error summarizing: {e}"
|
|
|
130 |
brands = extract_brands(transcript_en)
|
131 |
topics = extract_topics(transcript_en)
|
132 |
key_takeaways = make_bullets(summary)
|
|
|
133 |
pdf_file = create_pdf_report(lang_text, transcript, transcript_en, summary, brands, topics, key_takeaways)
|
134 |
return (
|
135 |
lang_text,
|
|
|
141 |
pdf_file
|
142 |
)
|
143 |
|
|
|
144 |
iface = gr.Interface(
|
145 |
fn=process_audio,
|
146 |
inputs=gr.Audio(type="filepath", label="Upload MP3/WAV Audio"),
|
|
|
154 |
gr.File(label="Download PDF Report")
|
155 |
],
|
156 |
title="Audio Transcript, Brand & Topic Analysis (OpenAI Whisper + Unicode PDF Download)",
|
157 |
+
description="Upload your audio file (MP3/WAV). Get transcript, summary, brand & topic detection, and download PDF. Unicode (Indian language/emoji) supported."
|
158 |
)
|
159 |
|
160 |
iface.launch()
|