Spaces:
Build error
Build error
Update app.py
Browse files12345678910111213141516171819202122232425262728293031323334353637383940414243444546475 unmodified lines535455565758596061626364656667686970717273747576777879808182838485import streamlit as stimport pdfplumberfrom transformers import pipelinefrom gtts import gTTSimport os# Function to extract text from PDFdef extract_text_from_pdf(pdf_file):text = ""try:with pdfplumber.open(pdf_file) as pdf:for page in pdf.pages:text += page.extract_text() or ""except Exception as e:st.error(f"Error reading the PDF: {e}")return text# Function to generate discussion pointsdef generate_discussion_points(text):try:summarizer = pipeline("summarization", model="facebook/bart-large-cnn")summary = summarizer(text, max_length=130, min_length=30, do_sample=False)return summary[0]['summary_text']except Exception as e:st.error(f"Error during summarization: {e}")return None# Function to convert text to speechdef text_to_speech(text, output_file="output.mp3"):try:tts = gTTS(text)tts.save(output_file)return output_fileexcept Exception as e:st.error(f"Error during text-to-speech conversion: {e}")return None# Streamlit app starts herest.title("๐ PDF Discussion Points Generator")st.write("Upload a small PDF file to generate discussion points and listen to them.")# File uploaderuploaded_file = st.file_uploader("Upload a PDF", type=["pdf"])if uploaded_file:# Extract text from uploaded PDFwith st.spinner("Extracting text from the uploaded PDF..."):5 unmodified lines# Generate discussion pointswith st.spinner("Generating discussion points..."):discussion_points = generate_discussion_points(pdf_text)if discussion_points:st.write("### Discussion Points")st.text_area("Discussion Points", discussion_points, height=150)# Convert discussion points to audiowith st.spinner("Converting discussion points to audio..."):audio_file = text_to_speech(discussion_points)if audio_file:st.success("Audio file generated successfully!")# Audio playbackst.write("### Listen to the Discussion Points")audio_bytes = open(audio_file, "rb").read()st.audio(audio_bytes, format="audio/mp3")# Option to download the audio filest.download_button(label="Download Audio",data=audio_bytes,file_name="discussion_points.mp3",mime="audio/mp3")else:st.error("Failed to generate discussion points. Please try again.")else:st.error("No text was extracted from the uploaded PDF. Please try with another file.")else:1234567891011121314151617181920215 unmodified lines2728293031323334353637383940414243444546474849505152535455565758596061# Function to generate discussion points with a promptdef generate_discussion_points(text, user_prompt=""):summarizer = pipeline("summarization", model="facebook/bart-large-cnn")# Prepend the user prompt to the text for guided summarizationinput_text = f"{user_prompt}\n\n{text}"summary = summarizer(input_text, max_length=130, min_length=30, do_sample=False)return summary[0]['summary_text']# Streamlit appst.title("๐ PDF Discussion Points Generator with User Prompts")st.write("Upload a PDF file, provide a prompt for the summary, and listen to the generated discussion points.")# File uploaderuploaded_file = st.file_uploader("Upload a PDF", type=["pdf"])# Prompt inputuser_prompt = st.text_input("Enter a specific prompt for the summary (optional):", "")if uploaded_file:# Extract text from uploaded PDFwith st.spinner("Extracting text from the uploaded PDF..."):5 unmodified lines# Generate discussion pointswith st.spinner("Generating discussion points..."):try:discussion_points = generate_discussion_points(pdf_text, user_prompt)st.write("### Discussion Points")st.text_area("Discussion Points", discussion_points, height=150)except Exception as e:st.error(f"Error during summarization: {e}")# Convert discussion points to audiowith st.spinner("Converting discussion points to audio..."):try:audio_file = text_to_speech(discussion_points)st.success("Audio file generated successfully!")except Exception as e:st.error(f"Error during text-to-speech conversion: {e}")audio_file = Noneif audio_file:# Audio playbackst.write("### Listen to the Discussion Points")audio_bytes = open(audio_file, "rb").read()st.audio(audio_bytes, format="audio/mp3")# Option to download the audio filest.download_button(label="Download Audio",data=audio_bytes,file_name="discussion_points.mp3",mime="audio/mp3")else:st.error("No text was extracted from the uploaded PDF. Please try with another file.")else:
app.py+39-63