Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -2,14 +2,32 @@ import gradio as gr
|
|
2 |
import openai
|
3 |
from langdetect import detect
|
4 |
from transformers import pipeline
|
|
|
|
|
5 |
import os
|
6 |
|
7 |
openai.api_key = os.getenv("OPENAI_API_KEY") # Set this in HF Space secrets
|
8 |
|
9 |
summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
|
11 |
def make_str(val):
|
12 |
-
# Always return string, even if val is None, bool, list, dict, etc.
|
13 |
try:
|
14 |
if val is None:
|
15 |
return ""
|
@@ -23,10 +41,40 @@ def make_str(val):
|
|
23 |
except Exception:
|
24 |
return ""
|
25 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
def process_audio(audio_path):
|
27 |
-
# Accept only valid, non-empty file path (string)
|
28 |
if not audio_path or not isinstance(audio_path, str):
|
29 |
-
return ("No audio file provided.", "", "", "")
|
30 |
try:
|
31 |
with open(audio_path, "rb") as audio_file:
|
32 |
transcript = openai.audio.transcriptions.create(
|
@@ -36,7 +84,7 @@ def process_audio(audio_path):
|
|
36 |
)
|
37 |
transcript = make_str(transcript).strip()
|
38 |
except Exception as e:
|
39 |
-
return (make_str(f"Error in transcription: {e}"), "", "", "")
|
40 |
try:
|
41 |
detected_lang = detect(transcript)
|
42 |
lang_text = {'en': 'English', 'hi': 'Hindi', 'ta': 'Tamil'}.get(detected_lang, detected_lang)
|
@@ -55,16 +103,25 @@ def process_audio(audio_path):
|
|
55 |
except Exception as e:
|
56 |
transcript_en = make_str(f"Error translating: {e}")
|
57 |
try:
|
58 |
-
|
59 |
-
|
60 |
-
if isinstance(summary, list) and len(summary) > 0 and "summary_text" in summary[0]:
|
61 |
-
summary_text = make_str(summary[0]["summary_text"])
|
62 |
-
else:
|
63 |
-
summary_text = make_str(summary)
|
64 |
except Exception as e:
|
65 |
-
|
66 |
-
#
|
67 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
68 |
|
69 |
iface = gr.Interface(
|
70 |
fn=process_audio,
|
@@ -73,10 +130,13 @@ iface = gr.Interface(
|
|
73 |
gr.Textbox(label="Detected Language"),
|
74 |
gr.Textbox(label="Original Transcript"),
|
75 |
gr.Textbox(label="English Transcript (if translated)"),
|
76 |
-
gr.Textbox(label="
|
|
|
|
|
|
|
77 |
],
|
78 |
-
title="Audio Transcript,
|
79 |
-
description="Upload your audio file (MP3/WAV).
|
80 |
)
|
81 |
|
82 |
iface.launch()
|
|
|
2 |
import openai
|
3 |
from langdetect import detect
|
4 |
from transformers import pipeline
|
5 |
+
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"]
|
19 |
+
|
20 |
+
def extract_topics(text, top_n=5):
|
21 |
+
keywords = kw_model.extract_keywords(text, top_n=top_n, stop_words='english')
|
22 |
+
topics = [kw for kw, score in keywords]
|
23 |
+
return topics if topics else ["None extracted"]
|
24 |
+
|
25 |
+
def make_bullets(summary):
|
26 |
+
sentences = summary.replace("\n", " ").split('. ')
|
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 ""
|
|
|
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 |
)
|
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)
|
|
|
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,
|
118 |
+
transcript,
|
119 |
+
transcript_en,
|
120 |
+
", ".join(brands),
|
121 |
+
", ".join(topics),
|
122 |
+
key_takeaways,
|
123 |
+
pdf_file
|
124 |
+
)
|
125 |
|
126 |
iface = gr.Interface(
|
127 |
fn=process_audio,
|
|
|
130 |
gr.Textbox(label="Detected Language"),
|
131 |
gr.Textbox(label="Original Transcript"),
|
132 |
gr.Textbox(label="English Transcript (if translated)"),
|
133 |
+
gr.Textbox(label="Brands Detected"),
|
134 |
+
gr.Textbox(label="Key Topics"),
|
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 |
|
142 |
iface.launch()
|