Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,11 +1,13 @@
|
|
1 |
import streamlit as st
|
2 |
import pdfplumber
|
3 |
import re
|
4 |
-
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, pipeline
|
5 |
from gtts import gTTS
|
6 |
from sklearn.feature_extraction.text import CountVectorizer
|
7 |
import nltk
|
8 |
from nltk.sentiment import SentimentIntensityAnalyzer
|
|
|
|
|
9 |
|
10 |
# Download necessary NLTK data
|
11 |
nltk.download('vader_lexicon')
|
@@ -16,6 +18,10 @@ model = AutoModelForSeq2SeqLM.from_pretrained("google/pegasus-xsum")
|
|
16 |
qa_pipeline = pipeline("question-answering", model="distilbert-base-cased-distilled-squad")
|
17 |
sia = SentimentIntensityAnalyzer()
|
18 |
|
|
|
|
|
|
|
|
|
19 |
# Helper functions
|
20 |
def extract_text_from_pdf(file):
|
21 |
with pdfplumber.open(file) as pdf:
|
@@ -42,14 +48,30 @@ def summarize_large_document(text, max_length=800):
|
|
42 |
summaries = [summarize_text_pegasus(paragraph, max_length=max_length) for paragraph in paragraphs]
|
43 |
return " ".join(summaries)
|
44 |
|
45 |
-
def
|
46 |
-
|
47 |
-
|
|
|
|
|
|
|
|
|
|
|
48 |
for paragraph in paragraphs:
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
53 |
return " ".join(answers)
|
54 |
|
55 |
def text_to_speech(text, lang="en"):
|
@@ -73,6 +95,9 @@ def analyze_sentiment(text):
|
|
73 |
st.title("Enhanced PDF to Audiobook App")
|
74 |
st.markdown("### Turn documents into interactive audiobooks with advanced features.")
|
75 |
|
|
|
|
|
|
|
76 |
uploaded_file = st.file_uploader("Upload a PDF", type="pdf")
|
77 |
|
78 |
if uploaded_file:
|
@@ -89,20 +114,24 @@ if uploaded_file:
|
|
89 |
if st.button("Convert Summary to Audiobook"):
|
90 |
with st.spinner("Generating audio..."):
|
91 |
audio_path = text_to_speech(summary)
|
92 |
-
|
93 |
-
|
|
|
|
|
94 |
|
95 |
st.markdown("### Ask Questions About the Document")
|
96 |
question = st.text_input("Your Question:")
|
97 |
if question:
|
98 |
with st.spinner("Answering your question..."):
|
99 |
-
answer =
|
100 |
st.write(f"**Answer:** {answer}")
|
101 |
if st.button("Convert Answer to Audio"):
|
102 |
with st.spinner("Generating answer audio..."):
|
103 |
answer_audio_path = text_to_speech(answer)
|
104 |
-
|
105 |
-
|
|
|
|
|
106 |
|
107 |
st.markdown("### Document Insights")
|
108 |
if st.checkbox("Extract Keywords"):
|
|
|
1 |
import streamlit as st
|
2 |
import pdfplumber
|
3 |
import re
|
4 |
+
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, pipeline, DPRQuestionEncoder, DPRContextEncoder
|
5 |
from gtts import gTTS
|
6 |
from sklearn.feature_extraction.text import CountVectorizer
|
7 |
import nltk
|
8 |
from nltk.sentiment import SentimentIntensityAnalyzer
|
9 |
+
import faiss
|
10 |
+
import numpy as np
|
11 |
|
12 |
# Download necessary NLTK data
|
13 |
nltk.download('vader_lexicon')
|
|
|
18 |
qa_pipeline = pipeline("question-answering", model="distilbert-base-cased-distilled-squad")
|
19 |
sia = SentimentIntensityAnalyzer()
|
20 |
|
21 |
+
# Initialize RAG components
|
22 |
+
question_encoder = DPRQuestionEncoder.from_pretrained("facebook/dpr-question_encoder-single-nq-base")
|
23 |
+
context_encoder = DPRContextEncoder.from_pretrained("facebook/dpr-ctx_encoder-single-nq-base")
|
24 |
+
|
25 |
# Helper functions
|
26 |
def extract_text_from_pdf(file):
|
27 |
with pdfplumber.open(file) as pdf:
|
|
|
48 |
summaries = [summarize_text_pegasus(paragraph, max_length=max_length) for paragraph in paragraphs]
|
49 |
return " ".join(summaries)
|
50 |
|
51 |
+
def embed_text(text, encoder, tokenizer):
|
52 |
+
inputs = tokenizer(text, return_tensors="pt", truncation=True, padding="longest")
|
53 |
+
embeddings = encoder(**inputs).pooler_output
|
54 |
+
return embeddings.detach().numpy()
|
55 |
+
|
56 |
+
def build_index(paragraphs):
|
57 |
+
index = faiss.IndexFlatL2(768)
|
58 |
+
embeddings = []
|
59 |
for paragraph in paragraphs:
|
60 |
+
embeddings.append(embed_text(paragraph, context_encoder, tokenizer))
|
61 |
+
embeddings = np.vstack(embeddings)
|
62 |
+
index.add(embeddings)
|
63 |
+
return index, paragraphs
|
64 |
+
|
65 |
+
def retrieve_relevant_paragraphs(question, index, paragraphs, top_k=5):
|
66 |
+
question_embedding = embed_text(question, question_encoder, tokenizer)
|
67 |
+
distances, indices = index.search(question_embedding, top_k)
|
68 |
+
return [paragraphs[i] for i in indices[0]]
|
69 |
+
|
70 |
+
def answer_question_with_rag(question, context, top_k=5):
|
71 |
+
paragraphs = split_text_into_paragraphs(context)
|
72 |
+
index, paragraphs = build_index(paragraphs)
|
73 |
+
relevant_paragraphs = retrieve_relevant_paragraphs(question, index, paragraphs, top_k)
|
74 |
+
answers = [qa_pipeline({'question': question, 'context': paragraph})['answer'] for paragraph in relevant_paragraphs]
|
75 |
return " ".join(answers)
|
76 |
|
77 |
def text_to_speech(text, lang="en"):
|
|
|
95 |
st.title("Enhanced PDF to Audiobook App")
|
96 |
st.markdown("### Turn documents into interactive audiobooks with advanced features.")
|
97 |
|
98 |
+
if 'audio_path' not in st.session_state:
|
99 |
+
st.session_state['audio_path'] = None
|
100 |
+
|
101 |
uploaded_file = st.file_uploader("Upload a PDF", type="pdf")
|
102 |
|
103 |
if uploaded_file:
|
|
|
114 |
if st.button("Convert Summary to Audiobook"):
|
115 |
with st.spinner("Generating audio..."):
|
116 |
audio_path = text_to_speech(summary)
|
117 |
+
st.session_state['audio_path'] = audio_path
|
118 |
+
if st.session_state['audio_path']:
|
119 |
+
st.audio(st.session_state['audio_path'], format="audio/mp3")
|
120 |
+
st.download_button("Download Audio", data=open(st.session_state['audio_path'], "rb"), file_name="summary_audio.mp3")
|
121 |
|
122 |
st.markdown("### Ask Questions About the Document")
|
123 |
question = st.text_input("Your Question:")
|
124 |
if question:
|
125 |
with st.spinner("Answering your question..."):
|
126 |
+
answer = answer_question_with_rag(question, cleaned_text)
|
127 |
st.write(f"**Answer:** {answer}")
|
128 |
if st.button("Convert Answer to Audio"):
|
129 |
with st.spinner("Generating answer audio..."):
|
130 |
answer_audio_path = text_to_speech(answer)
|
131 |
+
st.session_state['audio_path'] = answer_audio_path
|
132 |
+
if st.session_state['audio_path']:
|
133 |
+
st.audio(st.session_state['audio_path'], format="audio/mp3")
|
134 |
+
st.download_button("Download Answer Audio", data=open(st.session_state['audio_path'], "rb"), file_name="answer_audio.mp3")
|
135 |
|
136 |
st.markdown("### Document Insights")
|
137 |
if st.checkbox("Extract Keywords"):
|