Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -17,7 +17,7 @@ FONT_PATH = "DejaVuSans.ttf" # Must be uploaded to Space root!
|
|
17 |
BRANDS = [
|
18 |
"Apple", "Google", "Microsoft", "Amazon", "Coca-Cola", "Pepsi", "Samsung", "Nike", "Adidas",
|
19 |
"Meta", "Facebook", "Instagram", "YouTube", "Netflix", "Reliance", "Tata", "Airtel", "Jio",
|
20 |
-
"Infosys", "Wipro", "Paytm", "Zomato", "Swiggy", "OLA", "Uber"
|
21 |
]
|
22 |
|
23 |
def extract_brands(text):
|
@@ -48,17 +48,31 @@ def make_str(val):
|
|
48 |
except Exception:
|
49 |
return ""
|
50 |
|
51 |
-
def
|
52 |
-
"""
|
53 |
if not isinstance(text, str):
|
54 |
text = str(text)
|
55 |
-
#
|
56 |
-
def break_long_words(t
|
57 |
-
|
58 |
-
|
59 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
60 |
text = break_long_words(text)
|
61 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
62 |
|
63 |
def create_pdf_report(language, transcript, transcript_en, summary, brands, topics, key_takeaways):
|
64 |
pdf = FPDF()
|
@@ -71,23 +85,23 @@ def create_pdf_report(language, transcript, transcript_en, summary, brands, topi
|
|
71 |
pdf.ln(5)
|
72 |
pdf.cell(0, 10, f"Detected Language: {language}", ln=True)
|
73 |
pdf.ln(5)
|
74 |
-
|
75 |
pdf.ln(3)
|
76 |
-
|
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 |
-
|
82 |
pdf.set_font("DejaVu", "B", 12)
|
83 |
pdf.cell(0, 10, "Key Topics:", ln=True)
|
84 |
pdf.set_font("DejaVu", size=12)
|
85 |
-
|
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 |
-
|
91 |
pdf_file = "/tmp/analysis_report.pdf"
|
92 |
pdf.output(pdf_file)
|
93 |
return pdf_file
|
|
|
17 |
BRANDS = [
|
18 |
"Apple", "Google", "Microsoft", "Amazon", "Coca-Cola", "Pepsi", "Samsung", "Nike", "Adidas",
|
19 |
"Meta", "Facebook", "Instagram", "YouTube", "Netflix", "Reliance", "Tata", "Airtel", "Jio",
|
20 |
+
"Infosys", "Wipro", "Paytm", "Zomato", "Swiggy", "OLA", "Uber","Zerodha","Motilal","ICICI","HDFC","grow", "Ind Money"
|
21 |
]
|
22 |
|
23 |
def extract_brands(text):
|
|
|
48 |
except Exception:
|
49 |
return ""
|
50 |
|
51 |
+
def very_safe_multicell(pdf, text, w=0, h=8, maxlen=80):
|
52 |
+
"""Force-break lines so no line/word exceeds maxlen chars, avoiding fpdf2 crash."""
|
53 |
if not isinstance(text, str):
|
54 |
text = str(text)
|
55 |
+
# Step 1: break any long 'words'
|
56 |
+
def break_long_words(t):
|
57 |
+
lines = []
|
58 |
+
for paragraph in t.split('\n'):
|
59 |
+
for word in paragraph.split(' '):
|
60 |
+
while len(word) > maxlen:
|
61 |
+
lines.append(word[:maxlen])
|
62 |
+
word = word[maxlen:]
|
63 |
+
lines.append(word)
|
64 |
+
lines.append('')
|
65 |
+
return '\n'.join(lines)
|
66 |
text = break_long_words(text)
|
67 |
+
# Step 2: ensure no line is too long (wrap at maxlen, regardless of word boundaries)
|
68 |
+
wrapped = []
|
69 |
+
for line in text.splitlines():
|
70 |
+
while len(line) > maxlen:
|
71 |
+
wrapped.append(line[:maxlen])
|
72 |
+
line = line[maxlen:]
|
73 |
+
wrapped.append(line)
|
74 |
+
safe_text = '\n'.join(wrapped)
|
75 |
+
pdf.multi_cell(w, h, safe_text)
|
76 |
|
77 |
def create_pdf_report(language, transcript, transcript_en, summary, brands, topics, key_takeaways):
|
78 |
pdf = FPDF()
|
|
|
85 |
pdf.ln(5)
|
86 |
pdf.cell(0, 10, f"Detected Language: {language}", ln=True)
|
87 |
pdf.ln(5)
|
88 |
+
very_safe_multicell(pdf, "Original Transcript:\n" + (transcript or ""))
|
89 |
pdf.ln(3)
|
90 |
+
very_safe_multicell(pdf, "English Transcript:\n" + (transcript_en or ""))
|
91 |
pdf.ln(3)
|
92 |
pdf.set_font("DejaVu", "B", 12)
|
93 |
pdf.cell(0, 10, "Brands Detected:", ln=True)
|
94 |
pdf.set_font("DejaVu", size=12)
|
95 |
+
very_safe_multicell(pdf, ", ".join(brands))
|
96 |
pdf.set_font("DejaVu", "B", 12)
|
97 |
pdf.cell(0, 10, "Key Topics:", ln=True)
|
98 |
pdf.set_font("DejaVu", size=12)
|
99 |
+
very_safe_multicell(pdf, ", ".join(topics))
|
100 |
pdf.set_font("DejaVu", "B", 12)
|
101 |
pdf.cell(0, 10, "Summary (Bulleted):", ln=True)
|
102 |
pdf.set_font("DejaVu", size=12)
|
103 |
for takeaway in key_takeaways.split('\n'):
|
104 |
+
very_safe_multicell(pdf, takeaway)
|
105 |
pdf_file = "/tmp/analysis_report.pdf"
|
106 |
pdf.output(pdf_file)
|
107 |
return pdf_file
|