Update app.py
Browse files
app.py
CHANGED
@@ -1,4 +1,4 @@
|
|
1 |
-
# app.py (Streamlit-only version for Hugging Face Spaces)
|
2 |
|
3 |
import os
|
4 |
import tempfile
|
@@ -28,14 +28,13 @@ def classify_topic(text: str, topics: List[str]) -> str:
|
|
28 |
if not topics:
|
29 |
return "Unknown (no topics provided)"
|
30 |
|
31 |
-
classifier = pipeline("zero-shot-classification", model="
|
32 |
result = classifier(text[:1000], candidate_labels=topics)
|
33 |
|
34 |
if 'labels' in result and len(result['labels']) > 0:
|
35 |
return result['labels'][0]
|
36 |
return "Unknown (classification failed)"
|
37 |
|
38 |
-
|
39 |
def generate_audio(text: str, output_path: str):
|
40 |
tts = gTTS(text)
|
41 |
tts.save(output_path)
|
@@ -61,30 +60,29 @@ if submitted and uploaded_file and topic_input:
|
|
61 |
try:
|
62 |
temp_dir = tempfile.mkdtemp()
|
63 |
file_path = os.path.join(temp_dir, uploaded_file.name)
|
64 |
-
|
65 |
with open(file_path, "wb") as f:
|
66 |
f.write(uploaded_file.read())
|
67 |
-
|
68 |
text = extract_text_from_pdf(file_path)
|
69 |
-
|
70 |
if not text.strip():
|
71 |
st.error("β No text could be extracted from the PDF. Try another file.")
|
72 |
else:
|
|
|
73 |
classified_topic = classify_topic(text, topic_list)
|
74 |
summary = summarize_text(text)
|
75 |
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
except Exception as e:
|
89 |
st.error(f"β Error: {str(e)}")
|
90 |
-
|
|
|
1 |
+
# app.py (Streamlit-only version for Hugging Face Spaces with error handling)
|
2 |
|
3 |
import os
|
4 |
import tempfile
|
|
|
28 |
if not topics:
|
29 |
return "Unknown (no topics provided)"
|
30 |
|
31 |
+
classifier = pipeline("zero-shot-classification", model="valhalla/distilbart-mnli-12-3")
|
32 |
result = classifier(text[:1000], candidate_labels=topics)
|
33 |
|
34 |
if 'labels' in result and len(result['labels']) > 0:
|
35 |
return result['labels'][0]
|
36 |
return "Unknown (classification failed)"
|
37 |
|
|
|
38 |
def generate_audio(text: str, output_path: str):
|
39 |
tts = gTTS(text)
|
40 |
tts.save(output_path)
|
|
|
60 |
try:
|
61 |
temp_dir = tempfile.mkdtemp()
|
62 |
file_path = os.path.join(temp_dir, uploaded_file.name)
|
63 |
+
|
64 |
with open(file_path, "wb") as f:
|
65 |
f.write(uploaded_file.read())
|
66 |
+
|
67 |
text = extract_text_from_pdf(file_path)
|
68 |
+
|
69 |
if not text.strip():
|
70 |
st.error("β No text could be extracted from the PDF. Try another file.")
|
71 |
else:
|
72 |
+
topic_list = [t.strip() for t in topic_input.split(",") if t.strip()]
|
73 |
classified_topic = classify_topic(text, topic_list)
|
74 |
summary = summarize_text(text)
|
75 |
|
76 |
+
st.markdown(f"### π§ Classified Topic: `{classified_topic}`")
|
77 |
+
st.markdown("### βοΈ Summary:")
|
78 |
+
st.write(summary)
|
79 |
+
|
80 |
+
audio_path = os.path.join(temp_dir, "summary.mp3")
|
81 |
+
generate_audio(summary, audio_path)
|
82 |
+
|
83 |
+
st.markdown("### π Audio Summary")
|
84 |
+
st.audio(audio_path)
|
85 |
+
st.success("Done! Audio summary is ready.")
|
86 |
+
|
|
|
87 |
except Exception as e:
|
88 |
st.error(f"β Error: {str(e)}")
|
|