Spaces:
Sleeping
Sleeping
Create App.py
Browse files
App.py
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import pdfplumber
|
| 3 |
+
from transformers import pipeline
|
| 4 |
+
from gtts import gTTS
|
| 5 |
+
import os
|
| 6 |
+
|
| 7 |
+
# Function to extract text from PDF
|
| 8 |
+
def extract_text_from_pdf(pdf_file):
|
| 9 |
+
text = ""
|
| 10 |
+
try:
|
| 11 |
+
with pdfplumber.open(pdf_file) as pdf:
|
| 12 |
+
for page in pdf.pages:
|
| 13 |
+
text += page.extract_text() or ""
|
| 14 |
+
except Exception as e:
|
| 15 |
+
st.error(f"Error reading the PDF: {e}")
|
| 16 |
+
return text
|
| 17 |
+
|
| 18 |
+
# Function to generate discussion points
|
| 19 |
+
def generate_discussion_points(text):
|
| 20 |
+
summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
|
| 21 |
+
summary = summarizer(text, max_length=130, min_length=30, do_sample=False)
|
| 22 |
+
return summary[0]['summary_text']
|
| 23 |
+
|
| 24 |
+
# Function to convert text to speech
|
| 25 |
+
def text_to_speech(text, output_file="output.mp3"):
|
| 26 |
+
tts = gTTS(text)
|
| 27 |
+
tts.save(output_file)
|
| 28 |
+
return output_file
|
| 29 |
+
|
| 30 |
+
# Streamlit app starts here
|
| 31 |
+
st.title("📄 PDF Discussion Points Generator")
|
| 32 |
+
st.write("Upload a small PDF file to generate discussion points and listen to them.")
|
| 33 |
+
|
| 34 |
+
# File uploader
|
| 35 |
+
uploaded_file = st.file_uploader("Upload a PDF", type=["pdf"])
|
| 36 |
+
|
| 37 |
+
if uploaded_file:
|
| 38 |
+
# Extract text from uploaded PDF
|
| 39 |
+
with st.spinner("Extracting text from the uploaded PDF..."):
|
| 40 |
+
pdf_text = extract_text_from_pdf(uploaded_file)
|
| 41 |
+
|
| 42 |
+
if pdf_text.strip():
|
| 43 |
+
st.write("### Extracted Text")
|
| 44 |
+
st.text_area("Extracted Text", pdf_text, height=200)
|
| 45 |
+
|
| 46 |
+
# Generate discussion points
|
| 47 |
+
with st.spinner("Generating discussion points..."):
|
| 48 |
+
try:
|
| 49 |
+
discussion_points = generate_discussion_points(pdf_text)
|
| 50 |
+
st.write("### Discussion Points")
|
| 51 |
+
st.text_area("Discussion Points", discussion_points, height=150)
|
| 52 |
+
except Exception as e:
|
| 53 |
+
st.error(f"Error during summarization: {e}")
|
| 54 |
+
|
| 55 |
+
# Convert discussion points to audio
|
| 56 |
+
with st.spinner("Converting discussion points to audio..."):
|
| 57 |
+
try:
|
| 58 |
+
audio_file = text_to_speech(discussion_points)
|
| 59 |
+
st.success("Audio file generated successfully!")
|
| 60 |
+
except Exception as e:
|
| 61 |
+
st.error(f"Error during text-to-speech conversion: {e}")
|
| 62 |
+
audio_file = None
|
| 63 |
+
|
| 64 |
+
if audio_file:
|
| 65 |
+
# Audio playback
|
| 66 |
+
st.write("### Listen to the Discussion Points")
|
| 67 |
+
audio_bytes = open(audio_file, "rb").read()
|
| 68 |
+
st.audio(audio_bytes, format="audio/mp3")
|
| 69 |
+
|
| 70 |
+
# Option to download the audio file
|
| 71 |
+
st.download_button(
|
| 72 |
+
label="Download Audio",
|
| 73 |
+
data=audio_bytes,
|
| 74 |
+
file_name="discussion_points.mp3",
|
| 75 |
+
mime="audio/mp3"
|
| 76 |
+
)
|
| 77 |
+
else:
|
| 78 |
+
st.error("No text was extracted from the uploaded PDF. Please try with another file.")
|
| 79 |
+
else:
|
| 80 |
+
st.info("Upload a PDF file to start.")
|
| 81 |
+
|