Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -3,22 +3,21 @@ 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 |
-
import re
|
9 |
-
import unicodedata
|
10 |
|
11 |
# --- SETUP ---
|
12 |
openai.api_key = os.getenv("OPENAI_API_KEY") # Set in HF Space Secrets
|
13 |
|
14 |
summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
|
15 |
kw_model = KeyBERT()
|
16 |
-
FONT_PATH = "DejaVuSans.ttf" # Must be uploaded to Space root!
|
17 |
|
|
|
18 |
BRANDS = [
|
19 |
-
"
|
20 |
-
"
|
21 |
-
"
|
|
|
|
|
22 |
]
|
23 |
|
24 |
def extract_brands(text):
|
@@ -49,72 +48,9 @@ def make_str(val):
|
|
49 |
except Exception:
|
50 |
return ""
|
51 |
|
52 |
-
def very_safe_multicell(pdf, text, w=0, h=8, maxlen=50):
|
53 |
-
"""Force-break lines so no line/word exceeds maxlen chars, avoiding fpdf2 crash."""
|
54 |
-
if not isinstance(text, str):
|
55 |
-
text = str(text)
|
56 |
-
# Remove unprintable chars (e.g. control characters)
|
57 |
-
text = "".join(ch for ch in text if unicodedata.category(ch)[0] != "C")
|
58 |
-
# Step 1: break long words
|
59 |
-
def break_long_words(t):
|
60 |
-
lines = []
|
61 |
-
for paragraph in t.split('\n'):
|
62 |
-
for word in paragraph.split(' '):
|
63 |
-
while len(word) > maxlen:
|
64 |
-
lines.append(word[:maxlen])
|
65 |
-
word = word[maxlen:]
|
66 |
-
lines.append(word)
|
67 |
-
lines.append('')
|
68 |
-
return '\n'.join(lines)
|
69 |
-
text = break_long_words(text)
|
70 |
-
# Step 2: ensure no line is too long (wrap at maxlen)
|
71 |
-
wrapped = []
|
72 |
-
for line in text.splitlines():
|
73 |
-
while len(line) > maxlen:
|
74 |
-
wrapped.append(line[:maxlen])
|
75 |
-
line = line[maxlen:]
|
76 |
-
wrapped.append(line)
|
77 |
-
safe_text = '\n'.join(wrapped)
|
78 |
-
pdf.multi_cell(w, h, safe_text)
|
79 |
-
|
80 |
-
def create_pdf_report(language, transcript_en, brands, topics, key_takeaways):
|
81 |
-
pdf = FPDF()
|
82 |
-
pdf.set_auto_page_break(auto=True, margin=10)
|
83 |
-
pdf.set_margins(left=10, top=10, right=10)
|
84 |
-
pdf.add_font("DejaVu", style="", fname=FONT_PATH, uni=True)
|
85 |
-
pdf.add_font("DejaVu", style="B", fname=FONT_PATH, uni=True)
|
86 |
-
pdf.add_page()
|
87 |
-
pdf.set_font("DejaVu", "B", 16)
|
88 |
-
pdf.cell(0, 10, "Audio Transcript & Analysis Report", ln=True, align="C")
|
89 |
-
pdf.set_font("DejaVu", size=12)
|
90 |
-
pdf.ln(5)
|
91 |
-
pdf.cell(0, 10, f"Detected Language: {language}", ln=True)
|
92 |
-
pdf.ln(5)
|
93 |
-
pdf.set_font("DejaVu", "B", 12)
|
94 |
-
pdf.cell(0, 10, "English Transcript:", ln=True)
|
95 |
-
pdf.set_font("DejaVu", size=12)
|
96 |
-
very_safe_multicell(pdf, transcript_en or "", maxlen=50)
|
97 |
-
pdf.ln(3)
|
98 |
-
pdf.set_font("DejaVu", "B", 12)
|
99 |
-
pdf.cell(0, 10, "Brands Detected:", ln=True)
|
100 |
-
pdf.set_font("DejaVu", size=12)
|
101 |
-
very_safe_multicell(pdf, ", ".join(brands), maxlen=50)
|
102 |
-
pdf.set_font("DejaVu", "B", 12)
|
103 |
-
pdf.cell(0, 10, "Key Topics:", ln=True)
|
104 |
-
pdf.set_font("DejaVu", size=12)
|
105 |
-
very_safe_multicell(pdf, ", ".join(topics), maxlen=50)
|
106 |
-
pdf.set_font("DejaVu", "B", 12)
|
107 |
-
pdf.cell(0, 10, "Summary (Bulleted):", ln=True)
|
108 |
-
pdf.set_font("DejaVu", size=10)
|
109 |
-
for takeaway in key_takeaways.split('\n'):
|
110 |
-
very_safe_multicell(pdf, takeaway, maxlen=50)
|
111 |
-
pdf_file = "/tmp/analysis_report.pdf"
|
112 |
-
pdf.output(pdf_file)
|
113 |
-
return pdf_file
|
114 |
-
|
115 |
def process_audio(audio_path):
|
116 |
if not audio_path or not isinstance(audio_path, str):
|
117 |
-
return ("No audio file provided.", "", "", "", "", ""
|
118 |
try:
|
119 |
with open(audio_path, "rb") as audio_file:
|
120 |
transcript = openai.audio.transcriptions.create(
|
@@ -124,7 +60,7 @@ def process_audio(audio_path):
|
|
124 |
)
|
125 |
transcript = make_str(transcript).strip()
|
126 |
except Exception as e:
|
127 |
-
return (f"Error in transcription: {e}", "", "", "", "", ""
|
128 |
try:
|
129 |
detected_lang = detect(transcript)
|
130 |
lang_text = {'en': 'English', 'hi': 'Hindi', 'ta': 'Tamil'}.get(detected_lang, detected_lang)
|
@@ -150,15 +86,13 @@ def process_audio(audio_path):
|
|
150 |
brands = extract_brands(transcript_en)
|
151 |
topics = extract_topics(transcript_en)
|
152 |
key_takeaways = make_bullets(summary)
|
153 |
-
pdf_file = create_pdf_report(lang_text, transcript_en, brands, topics, key_takeaways)
|
154 |
return (
|
155 |
lang_text,
|
156 |
transcript,
|
157 |
transcript_en,
|
158 |
", ".join(brands),
|
159 |
", ".join(topics),
|
160 |
-
key_takeaways
|
161 |
-
pdf_file
|
162 |
)
|
163 |
|
164 |
iface = gr.Interface(
|
@@ -168,13 +102,12 @@ iface = gr.Interface(
|
|
168 |
gr.Textbox(label="Detected Language"),
|
169 |
gr.Textbox(label="Original Transcript"),
|
170 |
gr.Textbox(label="English Transcript (if translated)"),
|
171 |
-
gr.Textbox(label="Brands Detected"),
|
172 |
gr.Textbox(label="Key Topics"),
|
173 |
-
gr.Textbox(label="Bulleted Key Takeaways")
|
174 |
-
gr.File(label="Download PDF Report")
|
175 |
],
|
176 |
-
title="Audio
|
177 |
-
description="Upload your audio file (MP3/WAV). Get transcript, summary,
|
178 |
)
|
179 |
|
180 |
iface.launch()
|
|
|
3 |
from langdetect import detect
|
4 |
from transformers import pipeline
|
5 |
from keybert import KeyBERT
|
|
|
6 |
import os
|
|
|
|
|
7 |
|
8 |
# --- SETUP ---
|
9 |
openai.api_key = os.getenv("OPENAI_API_KEY") # Set in HF Space Secrets
|
10 |
|
11 |
summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
|
12 |
kw_model = KeyBERT()
|
|
|
13 |
|
14 |
+
# Key Indian brokerages, investment apps, and fintech brands
|
15 |
BRANDS = [
|
16 |
+
"Zerodha", "Upstox", "Groww", "Angel One", "Motilal Oswal", "Sharekhan", "5paisa", "ICICI Direct",
|
17 |
+
"HDFC Securities", "Kotak Securities", "Axis Direct", "IIFL", "Paytm Money", "Edelweiss", "Geojit",
|
18 |
+
"Fyers", "Alice Blue", "mStock", "Stockal", "Kuvera", "Smallcase", "Jupiter", "Fi", "INDmoney",
|
19 |
+
"PhonePe", "Paytm", "Google Pay", "BHIM", "MobiKwik", "Cred", "Niyo", "Razorpay", "ETMoney",
|
20 |
+
"Bajaj Finserv", "SBI Securities", "YES Securities", "IDFC FIRST", "CAMS", "Karvy", "LIC", "ICICI Prudential"
|
21 |
]
|
22 |
|
23 |
def extract_brands(text):
|
|
|
48 |
except Exception:
|
49 |
return ""
|
50 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
51 |
def process_audio(audio_path):
|
52 |
if not audio_path or not isinstance(audio_path, str):
|
53 |
+
return ("No audio file provided.", "", "", "", "", "")
|
54 |
try:
|
55 |
with open(audio_path, "rb") as audio_file:
|
56 |
transcript = openai.audio.transcriptions.create(
|
|
|
60 |
)
|
61 |
transcript = make_str(transcript).strip()
|
62 |
except Exception as e:
|
63 |
+
return (f"Error in transcription: {e}", "", "", "", "", "")
|
64 |
try:
|
65 |
detected_lang = detect(transcript)
|
66 |
lang_text = {'en': 'English', 'hi': 'Hindi', 'ta': 'Tamil'}.get(detected_lang, detected_lang)
|
|
|
86 |
brands = extract_brands(transcript_en)
|
87 |
topics = extract_topics(transcript_en)
|
88 |
key_takeaways = make_bullets(summary)
|
|
|
89 |
return (
|
90 |
lang_text,
|
91 |
transcript,
|
92 |
transcript_en,
|
93 |
", ".join(brands),
|
94 |
", ".join(topics),
|
95 |
+
key_takeaways
|
|
|
96 |
)
|
97 |
|
98 |
iface = gr.Interface(
|
|
|
102 |
gr.Textbox(label="Detected Language"),
|
103 |
gr.Textbox(label="Original Transcript"),
|
104 |
gr.Textbox(label="English Transcript (if translated)"),
|
105 |
+
gr.Textbox(label="Indian Brokerages & Fintech Brands Detected"),
|
106 |
gr.Textbox(label="Key Topics"),
|
107 |
+
gr.Textbox(label="Bulleted Key Takeaways")
|
|
|
108 |
],
|
109 |
+
title="Audio Brand & Topic Analysis for Indian Finance Apps",
|
110 |
+
description="Upload your audio file (MP3/WAV). Get transcript, summary, *Indian brokerage & fintech brand detection*, key topics, and a bulleted summary. Powered by OpenAI Whisper and BART."
|
111 |
)
|
112 |
|
113 |
iface.launch()
|