dpv007 commited on
Commit
3b7dd4e
·
verified ·
1 Parent(s): 477ee99

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +68 -0
app.py ADDED
@@ -0,0 +1,68 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torch
3
+ from transformers import AutoProcessor, AutoModelForImageTextToText
4
+ from PIL import Image
5
+
6
+ # Load model + processor
7
+ model_id = "microsoft/GUI-Actor-Verifier-2B"
8
+
9
+ processor = AutoProcessor.from_pretrained(model_id)
10
+ model = AutoModelForImageTextToText.from_pretrained(
11
+ model_id,
12
+ torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
13
+ device_map="auto"
14
+ )
15
+
16
+
17
+ def run_model(image, prompt):
18
+ try:
19
+ messages = [
20
+ {
21
+ "role": "user",
22
+ "content": [
23
+ {"type": "image"},
24
+ {"type": "text", "text": prompt}
25
+ ]
26
+ }
27
+ ]
28
+
29
+ inputs = processor.apply_chat_template(
30
+ messages,
31
+ add_generation_prompt=True,
32
+ tokenize=True,
33
+ return_dict=True,
34
+ return_tensors="pt",
35
+ )
36
+
37
+ # Attach image
38
+ inputs["pixel_values"] = processor(images=image, return_tensors="pt").pixel_values
39
+
40
+ inputs = {k: v.to(model.device) for k, v in inputs.items()}
41
+
42
+ with torch.no_grad():
43
+ outputs = model.generate(**inputs, max_new_tokens=100)
44
+
45
+ result = processor.decode(
46
+ outputs[0][inputs["input_ids"].shape[-1]:],
47
+ skip_special_tokens=True
48
+ )
49
+
50
+ return result
51
+
52
+ except Exception as e:
53
+ return str(e)
54
+
55
+
56
+ # Gradio UI
57
+ demo = gr.Interface(
58
+ fn=run_model,
59
+ inputs=[
60
+ gr.Image(type="pil", label="Upload Image"),
61
+ gr.Textbox(label="Your Question", placeholder="What is in this image?")
62
+ ],
63
+ outputs=gr.Textbox(label="Model Output"),
64
+ title="GUI Actor Verifier 2B",
65
+ description="Ask questions about an image using Microsoft's multimodal model."
66
+ )
67
+
68
+ demo.launch()