arad1367 commited on
Commit
9673377
1 Parent(s): 461f2d9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -0
app.py CHANGED
@@ -0,0 +1,39 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torch
3
+ from PIL import Image
4
+ from transformers import AutoModel, AutoTokenizer
5
+
6
+ # Load the model and tokenizer
7
+ model = AutoModel.from_pretrained('openbmb/MiniCPM-Llama3-V-2_5', trust_remote_code=True, torch_dtype=torch.float16)
8
+ model = model.to(device='cuda')
9
+ tokenizer = AutoTokenizer.from_pretrained('openbmb/MiniCPM-Llama3-V-2_5', trust_remote_code=True)
10
+ model.eval()
11
+
12
+ # Define a function to generate a response
13
+ def generate_response(image, question):
14
+ msgs = [{'role': 'user', 'content': question}]
15
+ res = model.chat(
16
+ image=image,
17
+ msgs=msgs,
18
+ tokenizer=tokenizer,
19
+ sampling=True,
20
+ temperature=0.7,
21
+ stream=True
22
+ )
23
+ generated_text = ""
24
+ for new_text in res:
25
+ generated_text += new_text
26
+ return generated_text
27
+
28
+
29
+ # Create a Gradio interface
30
+ iface = gr.Interface(
31
+ fn=generate_response,
32
+ inputs=[gr.Image(type="pil"), "text"],
33
+ outputs="text",
34
+ title="Visual Question Answering",
35
+ description="Input an image and a question related to the image to receive a response.",
36
+ )
37
+
38
+ # Launch the app
39
+ iface.launch(debug=True)