Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,78 +1,71 @@
|
|
1 |
import streamlit as st
|
2 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
3 |
import torch
|
4 |
-
import fitz # PyMuPDF
|
5 |
|
6 |
-
# Load the
|
7 |
@st.cache_resource
|
8 |
def load_model():
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
return tokenizer, model
|
19 |
-
except Exception as e:
|
20 |
-
st.error(f"Error loading model: {e}")
|
21 |
-
return None, None
|
22 |
|
23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
|
25 |
-
# Function to
|
26 |
-
def
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
return text
|
33 |
-
except Exception as e:
|
34 |
-
st.error(f"Error reading PDF file: {e}")
|
35 |
-
return None
|
36 |
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
43 |
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
""
|
59 |
-
|
60 |
-
|
|
|
|
|
61 |
|
62 |
-
|
63 |
-
|
64 |
-
with torch.no_grad(): # Disable gradient calculation for inference
|
65 |
-
with torch.cuda.amp.autocast():
|
66 |
-
outputs = model.generate(
|
67 |
-
**input_ids,
|
68 |
-
max_new_tokens=200,
|
69 |
-
no_repeat_ngram_size=5
|
70 |
-
)
|
71 |
-
|
72 |
-
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
73 |
-
st.subheader("Answer:")
|
74 |
-
st.write(response)
|
75 |
-
except Exception as e:
|
76 |
-
st.error(f"Error generating response: {e}")
|
77 |
-
else:
|
78 |
-
st.error("Unable to read text from the uploaded PDF file.")
|
|
|
1 |
import streamlit as st
|
2 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
3 |
import torch
|
4 |
+
import fitz # PyMuPDF for PDF handling
|
5 |
|
6 |
+
# Load the model and tokenizer
|
7 |
@st.cache_resource
|
8 |
def load_model():
|
9 |
+
tokenizer = AutoTokenizer.from_pretrained("himmeow/vi-gemma-2b-RAG")
|
10 |
+
model = AutoModelForCausalLM.from_pretrained(
|
11 |
+
"himmeow/vi-gemma-2b-RAG",
|
12 |
+
device_map="auto",
|
13 |
+
torch_dtype=torch.bfloat16
|
14 |
+
)
|
15 |
+
if torch.cuda.is_available():
|
16 |
+
model.to("cuda")
|
17 |
+
return tokenizer, model
|
|
|
|
|
|
|
|
|
18 |
|
19 |
+
# Function to extract text from PDF
|
20 |
+
def extract_text_from_pdf(pdf_file):
|
21 |
+
doc = fitz.open(stream=pdf_file.read(), filetype="pdf")
|
22 |
+
text = ""
|
23 |
+
for page_num in range(doc.page_count):
|
24 |
+
page = doc.load_page(page_num)
|
25 |
+
text += page.get_text()
|
26 |
+
return text
|
27 |
|
28 |
+
# Function to generate response from model
|
29 |
+
def generate_response(input_text, query, tokenizer, model):
|
30 |
+
prompt = """
|
31 |
+
### Instruction and Input:
|
32 |
+
Based on the following context/document:
|
33 |
+
{}
|
34 |
+
Please answer the question: {}
|
|
|
|
|
|
|
|
|
35 |
|
36 |
+
### Response:
|
37 |
+
{}
|
38 |
+
"""
|
39 |
+
formatted_input = prompt.format(input_text, query, " ")
|
40 |
+
input_ids = tokenizer(formatted_input, return_tensors="pt")
|
41 |
+
if torch.cuda.is_available():
|
42 |
+
input_ids = input_ids.to("cuda")
|
43 |
+
outputs = model.generate(
|
44 |
+
**input_ids,
|
45 |
+
max_new_tokens=500,
|
46 |
+
no_repeat_ngram_size=5
|
47 |
+
)
|
48 |
+
return tokenizer.decode(outputs[0], skip_special_tokens=True)
|
49 |
|
50 |
+
# Streamlit app
|
51 |
+
def main():
|
52 |
+
st.title("PDF Question Answering with vi-gemma-2b-RAG")
|
53 |
|
54 |
+
pdf_file = st.file_uploader("Upload a PDF file", type=["pdf"])
|
55 |
+
|
56 |
+
if pdf_file is not None:
|
57 |
+
with st.spinner("Reading the PDF..."):
|
58 |
+
pdf_text = extract_text_from_pdf(pdf_file)
|
59 |
+
|
60 |
+
st.text_area("Extracted Text", pdf_text, height=300)
|
61 |
+
|
62 |
+
query = st.text_input("Enter your question:")
|
63 |
+
|
64 |
+
if st.button("Get Answer"):
|
65 |
+
with st.spinner("Generating response..."):
|
66 |
+
tokenizer, model = load_model()
|
67 |
+
response = generate_response(pdf_text, query, tokenizer, model)
|
68 |
+
st.text_area("Response", response, height=200)
|
69 |
|
70 |
+
if __name__ == "__main__":
|
71 |
+
main()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|