Update app.py
Browse files
app.py
CHANGED
|
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
import requests
|
| 4 |
+
from PyPDF2 import PdfReader
|
| 5 |
+
|
| 6 |
+
# Load Hugging Face Question Answering model
|
| 7 |
+
qa_pipeline = pipeline("question-answering", model="distilbert-base-cased-distilled-squad")
|
| 8 |
+
|
| 9 |
+
# Function to download and extract text from PDF
|
| 10 |
+
def extract_text_from_huggingface_pdf(url):
|
| 11 |
+
response = requests.get(url)
|
| 12 |
+
with open("document.pdf", "wb") as f:
|
| 13 |
+
f.write(response.content)
|
| 14 |
+
pdf_reader = PdfReader("document.pdf")
|
| 15 |
+
text = ""
|
| 16 |
+
for page in pdf_reader.pages:
|
| 17 |
+
text += page.extract_text() + "\n"
|
| 18 |
+
return text
|
| 19 |
+
|
| 20 |
+
# Hugging Face URL
|
| 21 |
+
PDF_URL = "https://huggingface.co/spaces/SujathaL/AWS_Restart_Program_Chatbot/blob/main/AWS%20restart%20program%20information.docx.pdf"
|
| 22 |
+
pdf_text = extract_text_from_huggingface_pdf(PDF_URL)
|
| 23 |
+
|
| 24 |
+
# Streamlit UI
|
| 25 |
+
st.title("Chat with Your PDF")
|
| 26 |
+
|
| 27 |
+
# User Question Input
|
| 28 |
+
question = st.text_input("Ask a question about the PDF:")
|
| 29 |
+
|
| 30 |
+
if st.button("Get Answer") and question:
|
| 31 |
+
response = qa_pipeline(question=question, context=pdf_text)
|
| 32 |
+
st.write("Answer:", response['answer'])
|