Waseem7711 commited on
Commit
cfdd22d
·
verified ·
1 Parent(s): ca53cec

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +53 -38
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
- 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")
@@ -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
- # 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)
 
 
 
 
 
 
 
 
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.")