snehasanjana commited on
Commit
a696afd
·
verified ·
1 Parent(s): b8305fb

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -0
app.py ADDED
@@ -0,0 +1,23 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import AutoProcessor, AutoModelForVision2Seq
3
+ from PIL import Image
4
+ import torch
5
+
6
+ # Load model and processor
7
+ processor = AutoProcessor.from_pretrained("microsoft/kosmos-2-patch14-224")
8
+ model = AutoModelForVision2Seq.from_pretrained("microsoft/kosmos-2-patch14-224")
9
+ model.eval()
10
+
11
+ def grounding(image, prompt):
12
+ inputs = processor(text=prompt, images=image, return_tensors="pt")
13
+ with torch.no_grad():
14
+ generated_ids = model.generate(**inputs, max_new_tokens=256)
15
+ generated_text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
16
+ return generated_text
17
+
18
+ gr.Interface(
19
+ fn=grounding,
20
+ inputs=[gr.Image(type="pil"), gr.Textbox(label="Text Prompt")],
21
+ outputs="text",
22
+ title="Kosmos-2 Grounding Demo"
23
+ ).launch()