Spaces:
Running
on
Zero
Running
on
Zero
MaziyarPanahi
commited on
Commit
•
dcf6d05
1
Parent(s):
a68639a
Update app.py (#1)
Browse files- Update app.py (1486c51bb59f5ea02f5531d825fedb701358850c)
app.py
CHANGED
@@ -7,15 +7,15 @@ import subprocess
|
|
7 |
subprocess.run('pip install flash-attn --no-build-isolation', env={'FLASH_ATTENTION_SKIP_CUDA_BUILD': "TRUE"}, shell=True)
|
8 |
|
9 |
models = {
|
10 |
-
"
|
11 |
|
12 |
}
|
13 |
|
14 |
processors = {
|
15 |
-
"
|
16 |
}
|
17 |
|
18 |
-
DESCRIPTION = "[
|
19 |
|
20 |
kwargs = {}
|
21 |
kwargs['torch_dtype'] = torch.bfloat16
|
@@ -25,23 +25,49 @@ assistant_prompt = '<|assistant|>\n'
|
|
25 |
prompt_suffix = "<|end|>\n"
|
26 |
|
27 |
@spaces.GPU
|
28 |
-
def run_example(image, text_input=None, model_id="
|
29 |
model = models[model_id]
|
30 |
processor = processors[model_id]
|
31 |
|
32 |
prompt = f"{user_prompt}<|image_1|>\n{text_input}{prompt_suffix}{assistant_prompt}"
|
33 |
image = Image.fromarray(image).convert("RGB")
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
|
46 |
css = """
|
47 |
#output {
|
@@ -53,11 +79,11 @@ css = """
|
|
53 |
|
54 |
with gr.Blocks(css=css) as demo:
|
55 |
gr.Markdown(DESCRIPTION)
|
56 |
-
with gr.Tab(label="
|
57 |
with gr.Row():
|
58 |
with gr.Column():
|
59 |
input_img = gr.Image(label="Input Picture")
|
60 |
-
model_selector = gr.Dropdown(choices=list(models.keys()), label="Model", value="
|
61 |
text_input = gr.Textbox(label="Question")
|
62 |
submit_btn = gr.Button(value="Submit")
|
63 |
with gr.Column():
|
|
|
7 |
subprocess.run('pip install flash-attn --no-build-isolation', env={'FLASH_ATTENTION_SKIP_CUDA_BUILD': "TRUE"}, shell=True)
|
8 |
|
9 |
models = {
|
10 |
+
"Qwen/Qwen2-VL-2B-Instruct": AutoModelForCausalLM.from_pretrained("Qwen/Qwen2-VL-2B-Instruct", trust_remote_code=True, torch_dtype="auto", _attn_implementation="flash_attention_2").cuda().eval()
|
11 |
|
12 |
}
|
13 |
|
14 |
processors = {
|
15 |
+
"Qwen/Qwen2-VL-2B-Instruct": AutoProcessor.from_pretrained("Qwen/Qwen2-VL-2B-Instruct", trust_remote_code=True)
|
16 |
}
|
17 |
|
18 |
+
DESCRIPTION = "[Qwen2-VL-2B Demo](https://huggingface.co/Qwen/Qwen2-VL-2B-Instruct)"
|
19 |
|
20 |
kwargs = {}
|
21 |
kwargs['torch_dtype'] = torch.bfloat16
|
|
|
25 |
prompt_suffix = "<|end|>\n"
|
26 |
|
27 |
@spaces.GPU
|
28 |
+
def run_example(image, text_input=None, model_id="Qwen/Qwen2-VL-2B-Instruct"):
|
29 |
model = models[model_id]
|
30 |
processor = processors[model_id]
|
31 |
|
32 |
prompt = f"{user_prompt}<|image_1|>\n{text_input}{prompt_suffix}{assistant_prompt}"
|
33 |
image = Image.fromarray(image).convert("RGB")
|
34 |
+
messages = [
|
35 |
+
{
|
36 |
+
"role": "user",
|
37 |
+
"content": [
|
38 |
+
{
|
39 |
+
"type": "image",
|
40 |
+
"image": "https://qianwen-res.oss-cn-beijing.aliyuncs.com/Qwen-VL/assets/demo.jpeg",
|
41 |
+
},
|
42 |
+
{"type": "text", "text": "Describe this image."},
|
43 |
+
],
|
44 |
+
}
|
45 |
+
]
|
46 |
+
|
47 |
+
# Preparation for inference
|
48 |
+
text = processor.apply_chat_template(
|
49 |
+
messages, tokenize=False, add_generation_prompt=True
|
50 |
+
)
|
51 |
+
image_inputs, video_inputs = process_vision_info(messages)
|
52 |
+
inputs = processor(
|
53 |
+
text=[text],
|
54 |
+
images=image_inputs,
|
55 |
+
videos=video_inputs,
|
56 |
+
padding=True,
|
57 |
+
return_tensors="pt",
|
58 |
+
)
|
59 |
+
inputs = inputs.to("cuda")
|
60 |
+
|
61 |
+
# Inference: Generation of the output
|
62 |
+
generated_ids = model.generate(**inputs, max_new_tokens=128)
|
63 |
+
generated_ids_trimmed = [
|
64 |
+
out_ids[len(in_ids) :] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)
|
65 |
+
]
|
66 |
+
output_text = processor.batch_decode(
|
67 |
+
generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False
|
68 |
+
)
|
69 |
+
|
70 |
+
return output_text
|
71 |
|
72 |
css = """
|
73 |
#output {
|
|
|
79 |
|
80 |
with gr.Blocks(css=css) as demo:
|
81 |
gr.Markdown(DESCRIPTION)
|
82 |
+
with gr.Tab(label="Qwen2-VL-2B Input"):
|
83 |
with gr.Row():
|
84 |
with gr.Column():
|
85 |
input_img = gr.Image(label="Input Picture")
|
86 |
+
model_selector = gr.Dropdown(choices=list(models.keys()), label="Model", value="Qwen/Qwen2-VL-2B-Instruct")
|
87 |
text_input = gr.Textbox(label="Question")
|
88 |
submit_btn = gr.Button(value="Submit")
|
89 |
with gr.Column():
|