Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -4,77 +4,84 @@ from transformers import AutoModelForCausalLM, AutoProcessor
|
|
4 |
import torch
|
5 |
from PIL import Image
|
6 |
import subprocess
|
7 |
-
|
|
|
8 |
subprocess.run(
|
9 |
-
'pip install flash-attn --no-build-isolation',
|
10 |
-
env={'FLASH_ATTENTION_SKIP_CUDA_BUILD': "TRUE"},
|
11 |
shell=True
|
12 |
)
|
13 |
|
14 |
-
models = {
|
15 |
-
"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()
|
16 |
-
|
17 |
-
}
|
18 |
-
|
19 |
-
processors = {
|
20 |
-
"microsoft/Phi-3.5-vision-instruct": AutoProcessor.from_pretrained("microsoft/Phi-3.5-vision-instruct", trust_remote_code=True)
|
21 |
-
}
|
22 |
-
|
23 |
-
kwargs = {}
|
24 |
-
kwargs['torch_dtype'] = torch.bfloat16
|
25 |
-
|
26 |
user_prompt = '<|user|>\n'
|
27 |
assistant_prompt = '<|assistant|>\n'
|
28 |
prompt_suffix = "<|end|>\n"
|
29 |
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
|
|
|
|
|
|
|
35 |
prompt = f"{user_prompt}<|image_1|>\n{text_input}{prompt_suffix}{assistant_prompt}"
|
36 |
image = Image.fromarray(image).convert("RGB")
|
37 |
|
38 |
inputs = processor(prompt, image, return_tensors="pt").to("cuda:0")
|
39 |
-
generate_ids = model.generate(
|
40 |
-
|
41 |
-
|
42 |
-
|
|
|
43 |
generate_ids = generate_ids[:, inputs['input_ids'].shape[1]:]
|
44 |
-
response = processor.batch_decode(
|
45 |
-
|
46 |
-
|
|
|
|
|
47 |
return response
|
48 |
|
49 |
css = """
|
50 |
#output {
|
51 |
-
height: 500px;
|
52 |
-
overflow: auto;
|
53 |
-
border: 1px solid #ccc;
|
54 |
}
|
55 |
"""
|
56 |
|
57 |
with gr.Blocks(css=css) as demo:
|
58 |
gr.Markdown("## Phi-3.5 Vision Instruct Demo with Example Inputs")
|
59 |
-
|
60 |
with gr.Tab(label="Phi-3.5 Input"):
|
61 |
with gr.Row():
|
62 |
with gr.Column():
|
63 |
input_img = gr.Image(label="Input Picture")
|
64 |
-
model_selector = gr.Dropdown(
|
|
|
|
|
|
|
|
|
65 |
text_input = gr.Textbox(label="Question")
|
66 |
submit_btn = gr.Button(value="Submit")
|
67 |
with gr.Column():
|
68 |
output_text = gr.Textbox(label="Output Text")
|
69 |
|
70 |
-
# Example images and text
|
71 |
examples = [
|
72 |
["image1.jpeg", "What does this painting tell us explain in detail?"],
|
73 |
["image2.jpg", "What does this painting tell us explain in detail?"],
|
74 |
["image3.jpg", "Describe the scene in this picture."]
|
75 |
]
|
76 |
|
77 |
-
# Adding Examples
|
78 |
gr.Examples(
|
79 |
examples=examples,
|
80 |
inputs=[input_img, text_input],
|
@@ -83,6 +90,5 @@ with gr.Blocks(css=css) as demo:
|
|
83 |
|
84 |
submit_btn.click(run_example, [input_img, text_input, model_selector], [output_text])
|
85 |
|
86 |
-
# Launch the demo
|
87 |
demo.queue(api_open=False)
|
88 |
-
demo.launch(debug=True, show_api=False)
|
|
|
4 |
import torch
|
5 |
from PIL import Image
|
6 |
import subprocess
|
7 |
+
|
8 |
+
# Install flash-attn but skip CUDA build (to avoid 'flash_attn_2_cuda' error)
|
9 |
subprocess.run(
|
10 |
+
'pip install flash-attn --no-build-isolation',
|
11 |
+
env={'FLASH_ATTENTION_SKIP_CUDA_BUILD': "TRUE"},
|
12 |
shell=True
|
13 |
)
|
14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
user_prompt = '<|user|>\n'
|
16 |
assistant_prompt = '<|assistant|>\n'
|
17 |
prompt_suffix = "<|end|>\n"
|
18 |
|
19 |
+
model_name = "microsoft/Phi-3.5-vision-instruct"
|
20 |
+
|
21 |
+
# Lazy-load the model and processor at runtime
|
22 |
+
def get_model_and_processor(model_id):
|
23 |
+
model = AutoModelForCausalLM.from_pretrained(
|
24 |
+
model_id,
|
25 |
+
trust_remote_code=True,
|
26 |
+
torch_dtype=torch.bfloat16 # safer than 'auto'
|
27 |
+
).cuda().eval()
|
28 |
+
processor = AutoProcessor.from_pretrained(
|
29 |
+
model_id,
|
30 |
+
trust_remote_code=True
|
31 |
+
)
|
32 |
+
return model, processor
|
33 |
|
34 |
+
@spaces.GPU
|
35 |
+
def run_example(image, text_input=None, model_id=model_name):
|
36 |
+
model, processor = get_model_and_processor(model_id)
|
37 |
prompt = f"{user_prompt}<|image_1|>\n{text_input}{prompt_suffix}{assistant_prompt}"
|
38 |
image = Image.fromarray(image).convert("RGB")
|
39 |
|
40 |
inputs = processor(prompt, image, return_tensors="pt").to("cuda:0")
|
41 |
+
generate_ids = model.generate(
|
42 |
+
**inputs,
|
43 |
+
max_new_tokens=1000,
|
44 |
+
eos_token_id=processor.tokenizer.eos_token_id
|
45 |
+
)
|
46 |
generate_ids = generate_ids[:, inputs['input_ids'].shape[1]:]
|
47 |
+
response = processor.batch_decode(
|
48 |
+
generate_ids,
|
49 |
+
skip_special_tokens=True,
|
50 |
+
clean_up_tokenization_spaces=False
|
51 |
+
)[0]
|
52 |
return response
|
53 |
|
54 |
css = """
|
55 |
#output {
|
56 |
+
height: 500px;
|
57 |
+
overflow: auto;
|
58 |
+
border: 1px solid #ccc;
|
59 |
}
|
60 |
"""
|
61 |
|
62 |
with gr.Blocks(css=css) as demo:
|
63 |
gr.Markdown("## Phi-3.5 Vision Instruct Demo with Example Inputs")
|
64 |
+
|
65 |
with gr.Tab(label="Phi-3.5 Input"):
|
66 |
with gr.Row():
|
67 |
with gr.Column():
|
68 |
input_img = gr.Image(label="Input Picture")
|
69 |
+
model_selector = gr.Dropdown(
|
70 |
+
choices=[model_name],
|
71 |
+
label="Model",
|
72 |
+
value=model_name
|
73 |
+
)
|
74 |
text_input = gr.Textbox(label="Question")
|
75 |
submit_btn = gr.Button(value="Submit")
|
76 |
with gr.Column():
|
77 |
output_text = gr.Textbox(label="Output Text")
|
78 |
|
|
|
79 |
examples = [
|
80 |
["image1.jpeg", "What does this painting tell us explain in detail?"],
|
81 |
["image2.jpg", "What does this painting tell us explain in detail?"],
|
82 |
["image3.jpg", "Describe the scene in this picture."]
|
83 |
]
|
84 |
|
|
|
85 |
gr.Examples(
|
86 |
examples=examples,
|
87 |
inputs=[input_img, text_input],
|
|
|
90 |
|
91 |
submit_btn.click(run_example, [input_img, text_input, model_selector], [output_text])
|
92 |
|
|
|
93 |
demo.queue(api_open=False)
|
94 |
+
demo.launch(debug=True, show_api=False)
|