Spaces:
Sleeping
Sleeping
app.py
Browse files
app.py
ADDED
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
3 |
+
import torch
|
4 |
+
import fitz # PyMuPDF
|
5 |
+
|
6 |
+
# Load the tokenizer and model
|
7 |
+
@st.cache_resource
|
8 |
+
def load_model():
|
9 |
+
tokenizer = AutoTokenizer.from_pretrained("ricepaper/vi-gemma-2b-RAG")
|
10 |
+
model = AutoModelForCausalLM.from_pretrained(
|
11 |
+
"ricepaper/vi-gemma-2b-RAG",
|
12 |
+
device_map="auto",
|
13 |
+
torch_dtype=torch.bfloat16
|
14 |
+
)
|
15 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
16 |
+
model.to(device)
|
17 |
+
return tokenizer, model
|
18 |
+
|
19 |
+
tokenizer, model = load_model()
|
20 |
+
|
21 |
+
# Function to read text from a PDF file
|
22 |
+
def read_pdf(file):
|
23 |
+
text = ""
|
24 |
+
with fitz.open("pdf", file.read()) as doc:
|
25 |
+
for page in doc:
|
26 |
+
text += page.get_text()
|
27 |
+
return text
|
28 |
+
|
29 |
+
# Streamlit app
|
30 |
+
st.title("PDF Question Answering with vi-gemma-2b-RAG")
|
31 |
+
st.write("Upload a PDF file, and ask a question based on its content.")
|
32 |
+
|
33 |
+
uploaded_file = st.file_uploader("Choose a PDF file", type="pdf")
|
34 |
+
question = st.text_input("Enter your question:")
|
35 |
+
|
36 |
+
if uploaded_file is not None and question:
|
37 |
+
# Read PDF content
|
38 |
+
pdf_text = read_pdf(uploaded_file)
|
39 |
+
|
40 |
+
# Prepare the input for the model
|
41 |
+
prompt_template = """
|
42 |
+
### Instruction and Input:
|
43 |
+
Based on the following context/documentation:
|
44 |
+
{}
|
45 |
+
Please answer the question: {}
|
46 |
+
|
47 |
+
### Response:
|
48 |
+
{}
|
49 |
+
"""
|
50 |
+
input_text = prompt_template.format(pdf_text, question, "")
|
51 |
+
input_ids = tokenizer(input_text, return_tensors="pt").to(model.device)
|
52 |
+
|
53 |
+
# Generate a response
|
54 |
+
with torch.cuda.amp.autocast():
|
55 |
+
outputs = model.generate(
|
56 |
+
**input_ids,
|
57 |
+
max_new_tokens=200,
|
58 |
+
no_repeat_ngram_size=5
|
59 |
+
)
|
60 |
+
|
61 |
+
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
62 |
+
st.subheader("Answer:")
|
63 |
+
st.write(response)
|