Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -17,16 +17,18 @@ if torch.cuda.is_available():
|
|
| 17 |
|
| 18 |
# Function to extract text from PDF
|
| 19 |
def extract_text_from_pdf(pdf_path):
|
| 20 |
-
|
| 21 |
with open(pdf_path, "rb") as file:
|
| 22 |
reader = PdfReader(file)
|
| 23 |
for page_num in range(len(reader.pages)):
|
| 24 |
page = reader.pages[page_num]
|
| 25 |
text = page.extract_text()
|
| 26 |
-
|
| 27 |
-
return
|
| 28 |
|
| 29 |
# Streamlit app
|
|
|
|
|
|
|
| 30 |
st.title("π PDF Question Answering")
|
| 31 |
|
| 32 |
# Sidebar for PDF upload
|
|
@@ -41,6 +43,7 @@ if uploaded_file is not None:
|
|
| 41 |
# Input field for the user's question
|
| 42 |
user_query = st.text_input("Enter your question:")
|
| 43 |
|
|
|
|
| 44 |
if st.button("Submit") and user_query:
|
| 45 |
# Format the input text
|
| 46 |
input_text = f"{user_query}\n\n### Response:\n"
|
|
@@ -63,5 +66,20 @@ if uploaded_file is not None:
|
|
| 63 |
answer = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 64 |
|
| 65 |
# Display question and answer
|
| 66 |
-
st.write(f"**Q: {user_query}**")
|
| 67 |
-
st.write(f"**A: {answer.strip()}**")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
# Function to extract text from PDF
|
| 19 |
def extract_text_from_pdf(pdf_path):
|
| 20 |
+
pdf_text = ""
|
| 21 |
with open(pdf_path, "rb") as file:
|
| 22 |
reader = PdfReader(file)
|
| 23 |
for page_num in range(len(reader.pages)):
|
| 24 |
page = reader.pages[page_num]
|
| 25 |
text = page.extract_text()
|
| 26 |
+
pdf_text += text + "\n"
|
| 27 |
+
return pdf_text
|
| 28 |
|
| 29 |
# Streamlit app
|
| 30 |
+
st.write("**Created by: Engr. Hamesh Raj** [LinkedIn](https://www.linkedin.com/in/datascientisthameshraj/)")
|
| 31 |
+
|
| 32 |
st.title("π PDF Question Answering")
|
| 33 |
|
| 34 |
# Sidebar for PDF upload
|
|
|
|
| 43 |
# Input field for the user's question
|
| 44 |
user_query = st.text_input("Enter your question:")
|
| 45 |
|
| 46 |
+
# Display the submit button below the input field
|
| 47 |
if st.button("Submit") and user_query:
|
| 48 |
# Format the input text
|
| 49 |
input_text = f"{user_query}\n\n### Response:\n"
|
|
|
|
| 66 |
answer = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 67 |
|
| 68 |
# Display question and answer
|
| 69 |
+
st.write(f"**Q{len(st.session_state) + 1}: {user_query}**")
|
| 70 |
+
st.write(f"**A{len(st.session_state) + 1}: {answer.strip()}**")
|
| 71 |
+
|
| 72 |
+
# Store in session state for chat history
|
| 73 |
+
if "history" not in st.session_state:
|
| 74 |
+
st.session_state.history = []
|
| 75 |
+
|
| 76 |
+
st.session_state.history.append({
|
| 77 |
+
"question": user_query,
|
| 78 |
+
"answer": answer.strip()
|
| 79 |
+
})
|
| 80 |
+
|
| 81 |
+
# Display chat history
|
| 82 |
+
if "history" in st.session_state:
|
| 83 |
+
for i, qa in enumerate(st.session_state.history):
|
| 84 |
+
st.write(f"**Q{i + 1}: {qa['question']}**")
|
| 85 |
+
st.write(f"**A{i + 1}: {qa['answer']}**")
|