Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,5 +1,4 @@
|
|
1 |
import streamlit as st
|
2 |
-
from streamlit.state.session_state import SessionState
|
3 |
from PyPDF2 import PdfReader
|
4 |
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
5 |
from langchain_groq import ChatGroq
|
@@ -11,14 +10,6 @@ import tempfile
|
|
11 |
from gtts import gTTS
|
12 |
import os
|
13 |
|
14 |
-
def text_to_speech(text):
|
15 |
-
tts = gTTS(text=text, lang='en')
|
16 |
-
audio_file = tempfile.NamedTemporaryFile(suffix=".mp3", delete=False)
|
17 |
-
temp_filename = audio_file.name
|
18 |
-
tts.save(temp_filename)
|
19 |
-
st.audio(temp_filename, format='audio/mp3')
|
20 |
-
os.remove(temp_filename)
|
21 |
-
|
22 |
def get_pdf_text(pdf_docs):
|
23 |
text = ""
|
24 |
for pdf in pdf_docs:
|
@@ -49,6 +40,14 @@ def get_conversational_chain():
|
|
49 |
prompt = PromptTemplate(template=prompt_template, input_variables=["context", "question"])
|
50 |
chain = load_qa_chain(model, chain_type="stuff", prompt=prompt)
|
51 |
return chain
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
52 |
|
53 |
def user_input(user_question, api_key):
|
54 |
embeddings = HuggingFaceInferenceAPIEmbeddings(api_key=api_key, model_name="sentence-transformers/all-MiniLM-l6-v2")
|
@@ -72,43 +71,42 @@ def main():
|
|
72 |
st.markdown("<h1 style='font-size:20px;'>ChatBot by Muhammad Huzaifa</h1>", unsafe_allow_html=True)
|
73 |
api_key = st.secrets["inference_api_key"]
|
74 |
|
75 |
-
session_state = SessionState.get(pdf_docs=None, raw_text=None, processing_complete=False)
|
76 |
-
|
77 |
# Sidebar column for file upload
|
78 |
with st.sidebar:
|
79 |
st.header("Chat with PDF")
|
80 |
-
|
81 |
|
82 |
# Main column for displaying extracted text and user interaction
|
83 |
col1, col2 = st.columns([1, 2])
|
84 |
-
|
85 |
-
if
|
86 |
with col1:
|
87 |
if st.button("Submit"):
|
88 |
with st.spinner("Processing..."):
|
89 |
-
|
90 |
-
text_chunks = get_text_chunks(
|
91 |
get_vector_store(text_chunks, api_key)
|
92 |
st.success("Processing Complete")
|
93 |
-
|
94 |
-
|
95 |
# Check if PDF documents are uploaded and processing is complete
|
96 |
-
if
|
97 |
with col1:
|
98 |
user_question = st.text_input("Ask a question from the Docs")
|
99 |
if user_question:
|
100 |
user_input(user_question, api_key)
|
101 |
-
|
|
|
|
|
|
|
|
|
|
|
102 |
# Display extracted text if available
|
103 |
-
if
|
104 |
with col2:
|
105 |
st.subheader("Extracted Text from PDF:")
|
106 |
-
st.text(
|
|
|
107 |
|
108 |
-
# Show message if no PDF documents are uploaded
|
109 |
-
if not session_state.pdf_docs:
|
110 |
-
with col1:
|
111 |
-
st.write("Please upload a document first to proceed.")
|
112 |
|
113 |
if __name__ == "__main__":
|
114 |
main()
|
|
|
1 |
import streamlit as st
|
|
|
2 |
from PyPDF2 import PdfReader
|
3 |
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
4 |
from langchain_groq import ChatGroq
|
|
|
10 |
from gtts import gTTS
|
11 |
import os
|
12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
def get_pdf_text(pdf_docs):
|
14 |
text = ""
|
15 |
for pdf in pdf_docs:
|
|
|
40 |
prompt = PromptTemplate(template=prompt_template, input_variables=["context", "question"])
|
41 |
chain = load_qa_chain(model, chain_type="stuff", prompt=prompt)
|
42 |
return chain
|
43 |
+
|
44 |
+
def text_to_speech(text):
|
45 |
+
tts = gTTS(text=text, lang='en')
|
46 |
+
audio_file = tempfile.NamedTemporaryFile(suffix=".mp3", delete=False)
|
47 |
+
temp_filename = audio_file.name
|
48 |
+
tts.save(temp_filename)
|
49 |
+
st.audio(temp_filename, format='audio/mp3')
|
50 |
+
os.remove(temp_filename)
|
51 |
|
52 |
def user_input(user_question, api_key):
|
53 |
embeddings = HuggingFaceInferenceAPIEmbeddings(api_key=api_key, model_name="sentence-transformers/all-MiniLM-l6-v2")
|
|
|
71 |
st.markdown("<h1 style='font-size:20px;'>ChatBot by Muhammad Huzaifa</h1>", unsafe_allow_html=True)
|
72 |
api_key = st.secrets["inference_api_key"]
|
73 |
|
|
|
|
|
74 |
# Sidebar column for file upload
|
75 |
with st.sidebar:
|
76 |
st.header("Chat with PDF")
|
77 |
+
pdf_docs = st.file_uploader("Upload your PDF Files", accept_multiple_files=True, type=["pdf"])
|
78 |
|
79 |
# Main column for displaying extracted text and user interaction
|
80 |
col1, col2 = st.columns([1, 2])
|
81 |
+
raw_text = None
|
82 |
+
if pdf_docs:
|
83 |
with col1:
|
84 |
if st.button("Submit"):
|
85 |
with st.spinner("Processing..."):
|
86 |
+
raw_text = get_pdf_text(pdf_docs)
|
87 |
+
text_chunks = get_text_chunks(raw_text)
|
88 |
get_vector_store(text_chunks, api_key)
|
89 |
st.success("Processing Complete")
|
90 |
+
|
|
|
91 |
# Check if PDF documents are uploaded and processing is complete
|
92 |
+
if pdf_docs is not None and raw_text is not None:
|
93 |
with col1:
|
94 |
user_question = st.text_input("Ask a question from the Docs")
|
95 |
if user_question:
|
96 |
user_input(user_question, api_key)
|
97 |
+
raw_text = get_pdf_text(pdf_docs)
|
98 |
+
# Show message if no PDF documents are uploaded
|
99 |
+
if pdf_docs is None:
|
100 |
+
with col1:
|
101 |
+
st.write("Please upload a document first to proceed.")
|
102 |
+
|
103 |
# Display extracted text if available
|
104 |
+
if raw_text is not None:
|
105 |
with col2:
|
106 |
st.subheader("Extracted Text from PDF:")
|
107 |
+
st.text(raw_text)
|
108 |
+
|
109 |
|
|
|
|
|
|
|
|
|
110 |
|
111 |
if __name__ == "__main__":
|
112 |
main()
|