Update app.py
Browse files123423 unmodified lines282930313233343536373839404119 unmodified lines616263646566676869707172737475767778798081828384858687888990# app.py (Streamlit-only version for Hugging Face Spaces)import osimport tempfile23 unmodified linesif not topics:return "Unknown (no topics provided)"classifier = pipeline("zero-shot-classification", model="facebook/bart-large-mnli")result = classifier(text[:1000], candidate_labels=topics)if 'labels' in result and len(result['labels']) > 0:return result['labels'][0]return "Unknown (classification failed)"def generate_audio(text: str, output_path: str):tts = gTTS(text)tts.save(output_path)19 unmodified linestry:temp_dir = tempfile.mkdtemp()file_path = os.path.join(temp_dir, uploaded_file.name)with open(file_path, "wb") as f:f.write(uploaded_file.read())text = extract_text_from_pdf(file_path)topic_list = [t.strip() for t in topic_input.split(",") if t.strip()]if not text.strip():st.error("β No text could be extracted from the PDF. Try another file.")else:classified_topic = classify_topic(text, topic_list)summary = summarize_text(text)st.markdown(f"### π§ Classified Topic: `{classified_topic}`")st.markdown("### βοΈ Summary:")st.write(summary)audio_path = os.path.join(temp_dir, "summary.mp3")generate_audio(summary, audio_path)st.markdown("### π Audio Summary")st.audio(audio_path)st.success("Done! Audio summary is ready.")except Exception as e:st.error(f"β Error: {str(e)}")123423 unmodified lines2829303132333435363738394019 unmodified lines6061626364656667686970717273747576777879808182838485868788# app.py (Streamlit-only version for Hugging Face Spaces with error handling)import osimport tempfile23 unmodified linesif not topics:return "Unknown (no topics provided)"classifier = pipeline("zero-shot-classification", model="valhalla/distilbart-mnli-12-3")result = classifier(text[:1000], candidate_labels=topics)if 'labels' in result and len(result['labels']) > 0:return result['labels'][0]return "Unknown (classification failed)"def generate_audio(text: str, output_path: str):tts = gTTS(text)tts.save(output_path)19 unmodified linestry:temp_dir = tempfile.mkdtemp()file_path = os.path.join(temp_dir, uploaded_file.name)with open(file_path, "wb") as f:f.write(uploaded_file.read())text = extract_text_from_pdf(file_path)if not text.strip():st.error("β No text could be extracted from the PDF. Try another file.")else:topic_list = [t.strip() for t in topic_input.split(",") if t.strip()]classified_topic = classify_topic(text, topic_list)summary = summarize_text(text)st.markdown(f"### π§ Classified Topic: `{classified_topic}`")st.markdown("### βοΈ Summary:")st.write(summary)audio_path = os.path.join(temp_dir, "summary.mp3")generate_audio(summary, audio_path)st.markdown("### π Audio Summary")st.audio(audio_path)st.success("Done! Audio summary is ready.")except Exception as e:st.error(f"β Error: {str(e)}")
app.py+17-19