m7mdal7aj commited on
Commit
c0bfbf8
1 Parent(s): 0150d31

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -4
app.py CHANGED
@@ -1,9 +1,39 @@
1
  import streamlit as st
 
2
 
 
3
 
4
- def answer_question(image, question):
5
- # Integrate your model logic here
6
- answer = "This is where the answer will appear."
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
  return answer
8
 
9
  st.title("Image Question Answering")
@@ -19,7 +49,8 @@ if st.button("Get Answer"):
19
  # Display the image
20
  st.image(image, use_column_width=True)
21
  # Get and display the answer
22
- answer = answer_question(image, question)
 
23
  st.write(answer)
24
  else:
25
  st.write("Please upload an image and enter a question.")
 
1
  import streamlit as st
2
+ import torch
3
 
4
+ from transformers import Blip2Processor, Blip2ForConditionalGeneration
5
 
6
+ global device = 'cuda' if torch.cuda.is_available() else 'cpu'
7
+
8
+ def load_caption_model():
9
+ # Quantization Config
10
+ bnb_config = BitsAndBytesConfig(
11
+ load_in_8bit=True,
12
+ bnb_8bit_quant_type="nf4",
13
+ bnb_8bit_compute_dtype=torch.float16,
14
+ bnb_8bit_use_double_quant=False
15
+ )
16
+
17
+
18
+
19
+
20
+ processor = Blip2Processor.from_pretrained("Salesforce/blip2-opt-2.7b")
21
+ model = Blip2ForConditionalGeneration.from_pretrained("Salesforce/blip2-opt-2.7b", load_in_8bit=True,torch_dtype=torch.float16, device_map="auto")
22
+
23
+ return, model, processor
24
+
25
+
26
+
27
+ def answer_question(image, question, model, processor):
28
+
29
+
30
+ image = Image.open(image).convert('RGB')
31
+
32
+ inputs = processor(image, question, return_tensors="pt").to("cuda", torch.float16)
33
+
34
+ out = model.generate(**inputs, max_length=200, min_length=20, num_beams=1)
35
+
36
+ answer = processor.decode(out[0], skip_special_tokens=True).strip()
37
  return answer
38
 
39
  st.title("Image Question Answering")
 
49
  # Display the image
50
  st.image(image, use_column_width=True)
51
  # Get and display the answer
52
+ model, processor = load_caption_model()
53
+ answer = answer_question(image, question, model, processor)
54
  st.write(answer)
55
  else:
56
  st.write("Please upload an image and enter a question.")