Spaces:
Running
Running
Upload 2 files
Browse files- app.py +39 -0
- requirements.txt +4 -0
app.py
ADDED
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import PyPDF2
|
2 |
+
import streamlit as st
|
3 |
+
from transformers import GPT2LMHeadModel, GPT2Tokenizer
|
4 |
+
|
5 |
+
# Load pre-trained GPT-3 model and tokenizer
|
6 |
+
tokenizer = GPT2Tokenizer.from_pretrained("meta-llama/Meta-Llama-3-8B")
|
7 |
+
model = GPT2LMHeadModel.from_pretrained("meta-llama/Meta-Llama-3-8B")
|
8 |
+
|
9 |
+
def extract_text_from_pdf(pdf_path):
|
10 |
+
text = ""
|
11 |
+
with open(pdf_path, "rb") as f:
|
12 |
+
reader = PyPDF2.PdfFileReader(f)
|
13 |
+
for page_num in range(reader.numPages):
|
14 |
+
text += reader.getPage(page_num).extractText()
|
15 |
+
return text
|
16 |
+
|
17 |
+
def generate_response(user_input):
|
18 |
+
input_ids = tokenizer.encode(user_input, return_tensors="pt")
|
19 |
+
output = model.generate(input_ids, max_length=100, num_return_sequences=1, temperature=0.7)
|
20 |
+
response = tokenizer.decode(output[0], skip_special_tokens=True)
|
21 |
+
return response
|
22 |
+
|
23 |
+
def main():
|
24 |
+
st.title("PDF Chatbot")
|
25 |
+
|
26 |
+
# File upload
|
27 |
+
uploaded_file = st.file_uploader("Upload PDF", type=["pdf"])
|
28 |
+
|
29 |
+
if uploaded_file is not None:
|
30 |
+
pdf_text = extract_text_from_pdf(uploaded_file)
|
31 |
+
st.text_area("PDF Content", pdf_text)
|
32 |
+
|
33 |
+
user_input = st.text_input("You:", "")
|
34 |
+
if st.button("Send"):
|
35 |
+
response = generate_response(user_input)
|
36 |
+
st.text_area("Chatbot:", response)
|
37 |
+
|
38 |
+
if __name__ == "__main__":
|
39 |
+
main()
|
requirements.txt
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
PyPDF2==3.0.1
|
2 |
+
streamlit==1.33.0
|
3 |
+
transformers==4.40.1
|
4 |
+
torch==2.3.0
|