Arslan17121 commited on
Commit
8cf3d5d
·
verified ·
1 Parent(s): 498090e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -63
app.py CHANGED
@@ -1,47 +1,21 @@
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
- try:
21
- summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
22
- summary = summarizer(text, max_length=130, min_length=30, do_sample=False)
23
- return summary[0]['summary_text']
24
- except Exception as e:
25
- st.error(f"Error during summarization: {e}")
26
- return None
27
-
28
- # Function to convert text to speech
29
- def text_to_speech(text, output_file="output.mp3"):
30
- try:
31
- tts = gTTS(text)
32
- tts.save(output_file)
33
- return output_file
34
- except Exception as e:
35
- st.error(f"Error during text-to-speech conversion: {e}")
36
- return None
37
-
38
- # Streamlit app starts here
39
- st.title("📄 PDF Discussion Points Generator")
40
- st.write("Upload a small PDF file to generate discussion points and listen to them.")
41
 
42
  # File uploader
43
  uploaded_file = st.file_uploader("Upload a PDF", type=["pdf"])
44
 
 
 
 
45
  if uploaded_file:
46
  # Extract text from uploaded PDF
47
  with st.spinner("Extracting text from the uploaded PDF..."):
@@ -53,33 +27,35 @@ if uploaded_file:
53
 
54
  # Generate discussion points
55
  with st.spinner("Generating discussion points..."):
56
- discussion_points = generate_discussion_points(pdf_text)
57
-
58
- if discussion_points:
59
  st.write("### Discussion Points")
60
  st.text_area("Discussion Points", discussion_points, height=150)
 
 
61
 
62
- # Convert discussion points to audio
63
- with st.spinner("Converting discussion points to audio..."):
 
64
  audio_file = text_to_speech(discussion_points)
65
-
66
- if audio_file:
67
- st.success("Audio file generated successfully!")
68
-
69
- # Audio playback
70
- st.write("### Listen to the Discussion Points")
71
- audio_bytes = open(audio_file, "rb").read()
72
- st.audio(audio_bytes, format="audio/mp3")
73
-
74
- # Option to download the audio file
75
- st.download_button(
76
- label="Download Audio",
77
- data=audio_bytes,
78
- file_name="discussion_points.mp3",
79
- mime="audio/mp3"
80
- )
81
- else:
82
- st.error("Failed to generate discussion points. Please try again.")
83
  else:
84
  st.error("No text was extracted from the uploaded PDF. Please try with another file.")
85
  else:
 
1
+ # Function to generate discussion points with a prompt
2
+ def generate_discussion_points(text, user_prompt=""):
3
+ summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
4
+ # Prepend the user prompt to the text for guided summarization
5
+ input_text = f"{user_prompt}\n\n{text}"
6
+ summary = summarizer(input_text, max_length=130, min_length=30, do_sample=False)
7
+ return summary[0]['summary_text']
8
+
9
+ # Streamlit app
10
+ st.title("📄 PDF Discussion Points Generator with User Prompts")
11
+ st.write("Upload a PDF file, provide a prompt for the summary, and listen to the generated discussion points.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
 
13
  # File uploader
14
  uploaded_file = st.file_uploader("Upload a PDF", type=["pdf"])
15
 
16
+ # Prompt input
17
+ user_prompt = st.text_input("Enter a specific prompt for the summary (optional):", "")
18
+
19
  if uploaded_file:
20
  # Extract text from uploaded PDF
21
  with st.spinner("Extracting text from the uploaded PDF..."):
 
27
 
28
  # Generate discussion points
29
  with st.spinner("Generating discussion points..."):
30
+ try:
31
+ discussion_points = generate_discussion_points(pdf_text, user_prompt)
 
32
  st.write("### Discussion Points")
33
  st.text_area("Discussion Points", discussion_points, height=150)
34
+ except Exception as e:
35
+ st.error(f"Error during summarization: {e}")
36
 
37
+ # Convert discussion points to audio
38
+ with st.spinner("Converting discussion points to audio..."):
39
+ try:
40
  audio_file = text_to_speech(discussion_points)
41
+ st.success("Audio file generated successfully!")
42
+ except Exception as e:
43
+ st.error(f"Error during text-to-speech conversion: {e}")
44
+ audio_file = None
45
+
46
+ if audio_file:
47
+ # Audio playback
48
+ st.write("### Listen to the Discussion Points")
49
+ audio_bytes = open(audio_file, "rb").read()
50
+ st.audio(audio_bytes, format="audio/mp3")
51
+
52
+ # Option to download the audio file
53
+ st.download_button(
54
+ label="Download Audio",
55
+ data=audio_bytes,
56
+ file_name="discussion_points.mp3",
57
+ mime="audio/mp3"
58
+ )
59
  else:
60
  st.error("No text was extracted from the uploaded PDF. Please try with another file.")
61
  else: