Update app.py
Browse files
app.py
CHANGED
@@ -1,30 +1,41 @@
|
|
1 |
import streamlit as st
|
2 |
-
|
|
|
3 |
|
4 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
|
6 |
-
|
7 |
-
|
8 |
-
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
9 |
-
model = AutoModelForCausalLM.from_pretrained(model_name)
|
10 |
-
chat_pipeline = pipeline("text-generation", model=model, tokenizer=tokenizer, max_length=300)
|
11 |
-
return chat_pipeline
|
12 |
|
13 |
-
#
|
14 |
-
|
|
|
|
|
15 |
|
16 |
-
|
|
|
|
|
17 |
|
18 |
-
#
|
19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
else:
|
30 |
-
st.write("Please enter a prompt.")
|
|
|
1 |
import streamlit as st
|
2 |
+
import fitz # PyMuPDF
|
3 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
4 |
|
5 |
+
# Function to read and extract text from a PDF document
|
6 |
+
def read_pdf(file_path):
|
7 |
+
text = ""
|
8 |
+
with fitz.open(file_path) as doc:
|
9 |
+
for page in doc:
|
10 |
+
text += page.get_text()
|
11 |
+
return text
|
12 |
|
13 |
+
# Load the document text
|
14 |
+
document_text = read_pdf("jeff_wo.pdf") # Adjust path to your PDF file
|
|
|
|
|
|
|
|
|
15 |
|
16 |
+
# Streamlit UI
|
17 |
+
st.title("LLaMA 2-based Q&A System")
|
18 |
+
st.write("### Enter your query below:")
|
19 |
+
query = st.text_input("Query")
|
20 |
|
21 |
+
# Load tokenizer and model
|
22 |
+
tokenizer = AutoTokenizer.from_pretrained("NousResearch/Llama-2-7b-chat-hf")
|
23 |
+
model = AutoModelForCausalLM.from_pretrained("NousResearch/Llama-2-7b-chat-hf")
|
24 |
|
25 |
+
# Function to get answers using the LLaMA 2 model
|
26 |
+
def get_answer(context, query):
|
27 |
+
input_text = f"Context: {context}\nQ: {query}\nA:"
|
28 |
+
input_ids = tokenizer.encode(input_text, return_tensors="pt")
|
29 |
+
# Generate an answer to the query
|
30 |
+
output = model.generate(input_ids, max_length=512, num_return_sequences=1)
|
31 |
+
answer = tokenizer.decode(output[0], skip_special_tokens=True)
|
32 |
+
return answer
|
33 |
|
34 |
+
# Button to generate answers
|
35 |
+
if st.button("Get Answer"):
|
36 |
+
with st.spinner("Finding the answer..."):
|
37 |
+
answer = get_answer(document_text, query)
|
38 |
+
st.write("### Answer:")
|
39 |
+
st.write(answer)
|
40 |
+
|
41 |
+
# Ensure to replace `path/to/your/document.pdf` with the actual path to the PDF in your repo
|
|
|
|