Spaces:
Running
on
Zero
Running
on
Zero
Maxi W
commited on
Commit
•
a02f343
1
Parent(s):
0f71ee7
added app
Browse files
app.py
ADDED
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import AutoModelForCausalLM, AutoProcessor
|
3 |
+
import spaces
|
4 |
+
import torch
|
5 |
+
from PIL import Image
|
6 |
+
|
7 |
+
models = {
|
8 |
+
"microsoft/Phi-3.5-vision-instruct": AutoModelForCausalLM.from_pretrained("microsoft/Phi-3.5-vision-instruct", trust_remote_code=True, torch_dtype="auto", _attn_implementation="flash_attention_2").cuda().eval()
|
9 |
+
|
10 |
+
}
|
11 |
+
|
12 |
+
processors = {
|
13 |
+
"microsoft/Phi-3.5-vision-instruct": AutoProcessor.from_pretrained("microsoft/Phi-3.5-vision-instruct", trust_remote_code=True)
|
14 |
+
}
|
15 |
+
|
16 |
+
DESCRIPTION = "# [Phi-3.5-vision Demo](https://huggingface.co/microsoft/Phi-3.5-vision-instruct)"
|
17 |
+
|
18 |
+
kwargs = {}
|
19 |
+
kwargs['torch_dtype'] = torch.bfloat16
|
20 |
+
|
21 |
+
user_prompt = '<|user|>\n'
|
22 |
+
assistant_prompt = '<|assistant|>\n'
|
23 |
+
prompt_suffix = "<|end|>\n"
|
24 |
+
|
25 |
+
@spaces.GPU
|
26 |
+
def run_example(image, text_input=None, model_id="microsoft/Phi-3.5-vision-instruct"):
|
27 |
+
model = models[model_id]
|
28 |
+
processor = processors[model_id]
|
29 |
+
|
30 |
+
prompt = f"{user_prompt}<|image_1|>\n{text_input}{prompt_suffix}{assistant_prompt}"
|
31 |
+
image = Image.fromarray(image).convert("RGB")
|
32 |
+
|
33 |
+
inputs = processor(prompt, image, return_tensors="pt").to("cuda:0")
|
34 |
+
generate_ids = model.generate(**inputs,
|
35 |
+
max_new_tokens=1000,
|
36 |
+
eos_token_id=processor.tokenizer.eos_token_id,
|
37 |
+
)
|
38 |
+
generate_ids = generate_ids[:, inputs['input_ids'].shape[1]:]
|
39 |
+
response = processor.batch_decode(generate_ids,
|
40 |
+
skip_special_tokens=True,
|
41 |
+
clean_up_tokenization_spaces=False)[0]
|
42 |
+
return response
|
43 |
+
|
44 |
+
css = """
|
45 |
+
#output {
|
46 |
+
height: 500px;
|
47 |
+
overflow: auto;
|
48 |
+
border: 1px solid #ccc;
|
49 |
+
}
|
50 |
+
"""
|
51 |
+
|
52 |
+
with gr.Blocks(css=css) as demo:
|
53 |
+
gr.Markdown(DESCRIPTION)
|
54 |
+
with gr.Tab(label="Phi-3.5 Input"):
|
55 |
+
with gr.Row():
|
56 |
+
with gr.Column():
|
57 |
+
input_img = gr.Image(label="Input Picture")
|
58 |
+
model_selector = gr.Dropdown(choices=list(models.keys()), label="Model", value="microsoft/Phi-3.5-vision-instruct")
|
59 |
+
text_input = gr.Textbox(label="Question")
|
60 |
+
submit_btn = gr.Button(value="Submit")
|
61 |
+
with gr.Column():
|
62 |
+
output_text = gr.Textbox(label="Output Text")
|
63 |
+
|
64 |
+
submit_btn.click(run_example, [input_img, text_input, model_selector], [output_text])
|
65 |
+
|
66 |
+
demo.launch(debug=True)
|