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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +59 -66
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 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")
39
- st.write("Upload a PDF file, and ask a question based on its content.")
40
-
41
- uploaded_file = st.file_uploader("Choose a PDF file", type="pdf")
42
- question = st.text_input("Enter your question:")
 
 
 
 
 
 
 
43
 
44
- if uploaded_file is not None and question:
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.")
 
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()