Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -6,25 +6,33 @@ import fitz # PyMuPDF
|
|
6 |
# Load the tokenizer and model
|
7 |
@st.cache_resource
|
8 |
def load_model():
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
|
|
|
|
|
|
|
|
18 |
|
19 |
tokenizer, model = load_model()
|
20 |
|
21 |
# Function to read text from a PDF file
|
22 |
def read_pdf(file):
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
|
|
|
|
|
|
|
|
28 |
|
29 |
# Streamlit app
|
30 |
st.title("PDF Question Answering with vi-gemma-2b-RAG")
|
@@ -37,27 +45,34 @@ if uploaded_file is not None and question:
|
|
37 |
# Read PDF content
|
38 |
pdf_text = read_pdf(uploaded_file)
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
# Load the tokenizer and model
|
7 |
@st.cache_resource
|
8 |
def load_model():
|
9 |
+
try:
|
10 |
+
tokenizer = AutoTokenizer.from_pretrained("ricepaper/vi-gemma-2b-RAG")
|
11 |
+
model = AutoModelForCausalLM.from_pretrained(
|
12 |
+
"ricepaper/vi-gemma-2b-RAG",
|
13 |
+
device_map="auto",
|
14 |
+
torch_dtype=torch.bfloat16
|
15 |
+
)
|
16 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
17 |
+
model.to(device)
|
18 |
+
return tokenizer, model
|
19 |
+
except Exception as e:
|
20 |
+
st.error(f"Error loading model: {e}")
|
21 |
+
return None, None
|
22 |
|
23 |
tokenizer, model = load_model()
|
24 |
|
25 |
# Function to read text from a PDF file
|
26 |
def read_pdf(file):
|
27 |
+
try:
|
28 |
+
text = ""
|
29 |
+
with fitz.open("pdf", file.read()) as doc:
|
30 |
+
for page in doc:
|
31 |
+
text += page.get_text()
|
32 |
+
return text
|
33 |
+
except Exception as e:
|
34 |
+
st.error(f"Error reading PDF file: {e}")
|
35 |
+
return None
|
36 |
|
37 |
# Streamlit app
|
38 |
st.title("PDF Question Answering with vi-gemma-2b-RAG")
|
|
|
45 |
# Read PDF content
|
46 |
pdf_text = read_pdf(uploaded_file)
|
47 |
|
48 |
+
if pdf_text:
|
49 |
+
# Prepare the input for the model
|
50 |
+
prompt_template = """
|
51 |
+
### Instruction and Input:
|
52 |
+
Based on the following context/documentation:
|
53 |
+
{}
|
54 |
+
Please answer the question: {}
|
55 |
+
|
56 |
+
### Response:
|
57 |
+
{}
|
58 |
+
"""
|
59 |
+
input_text = prompt_template.format(pdf_text, question, "")
|
60 |
+
input_ids = tokenizer(input_text, return_tensors="pt").to(model.device)
|
61 |
+
|
62 |
+
try:
|
63 |
+
# Generate a response
|
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.")
|