Spaces:
Runtime error
Runtime error
Afrinetwork7
commited on
Commit
•
0867e96
1
Parent(s):
afe3b5d
Update app.py
Browse files
app.py
CHANGED
@@ -1,68 +1,66 @@
|
|
1 |
-
import
|
2 |
-
import
|
|
|
|
|
3 |
from transformers import AutoModelForCausalLM, AutoProcessor
|
4 |
import torch
|
5 |
from PIL import Image
|
6 |
import subprocess
|
|
|
|
|
7 |
subprocess.run('pip install flash-attn --no-build-isolation', env={'FLASH_ATTENTION_SKIP_CUDA_BUILD': "TRUE"}, shell=True)
|
8 |
|
9 |
-
|
10 |
-
"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()
|
11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
}
|
13 |
|
14 |
processors = {
|
15 |
-
"microsoft/Phi-3.5-vision-instruct": AutoProcessor.from_pretrained(
|
|
|
|
|
|
|
16 |
}
|
17 |
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
user_prompt = '<|user|>\n'
|
24 |
-
assistant_prompt = '<|assistant|>\n'
|
25 |
-
prompt_suffix = "<|end|>\n"
|
26 |
-
|
27 |
-
@spaces.GPU
|
28 |
-
def run_example(image, text_input=None, model_id="microsoft/Phi-3.5-vision-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 |
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
generate_ids = generate_ids[:, inputs['input_ids'].shape[1]:]
|
41 |
-
response = processor.batch_decode(generate_ids,
|
42 |
-
skip_special_tokens=True,
|
43 |
-
clean_up_tokenization_spaces=False)[0]
|
44 |
-
return response
|
45 |
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
overflow: auto;
|
50 |
-
border: 1px solid #ccc;
|
51 |
-
}
|
52 |
-
"""
|
53 |
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
with gr.Column():
|
59 |
-
input_img = gr.Image(label="Input Picture")
|
60 |
-
model_selector = gr.Dropdown(choices=list(models.keys()), label="Model", value="microsoft/Phi-3.5-vision-instruct")
|
61 |
-
text_input = gr.Textbox(label="Question")
|
62 |
-
submit_btn = gr.Button(value="Submit")
|
63 |
-
with gr.Column():
|
64 |
-
output_text = gr.Textbox(label="Output Text")
|
65 |
|
66 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
67 |
|
68 |
-
|
|
|
|
|
|
1 |
+
import base64
|
2 |
+
from io import BytesIO
|
3 |
+
from fastapi import FastAPI, HTTPException
|
4 |
+
from pydantic import BaseModel
|
5 |
from transformers import AutoModelForCausalLM, AutoProcessor
|
6 |
import torch
|
7 |
from PIL import Image
|
8 |
import subprocess
|
9 |
+
|
10 |
+
# Install flash-attn
|
11 |
subprocess.run('pip install flash-attn --no-build-isolation', env={'FLASH_ATTENTION_SKIP_CUDA_BUILD': "TRUE"}, shell=True)
|
12 |
|
13 |
+
app = FastAPI()
|
|
|
14 |
|
15 |
+
models = {
|
16 |
+
"microsoft/Phi-3.5-vision-instruct": AutoModelForCausalLM.from_pretrained(
|
17 |
+
"microsoft/Phi-3.5-vision-instruct",
|
18 |
+
trust_remote_code=True,
|
19 |
+
torch_dtype="auto",
|
20 |
+
attn_implementation="flash_attention_2"
|
21 |
+
).cuda().eval()
|
22 |
}
|
23 |
|
24 |
processors = {
|
25 |
+
"microsoft/Phi-3.5-vision-instruct": AutoProcessor.from_pretrained(
|
26 |
+
"microsoft/Phi-3.5-vision-instruct",
|
27 |
+
trust_remote_code=True
|
28 |
+
)
|
29 |
}
|
30 |
|
31 |
+
class InputData(BaseModel):
|
32 |
+
image: str
|
33 |
+
text_input: str
|
34 |
+
model_id: str = "microsoft/Phi-3.5-vision-instruct"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
35 |
|
36 |
+
@app.post("/run_example")
|
37 |
+
async def run_example(input_data: InputData):
|
38 |
+
try:
|
39 |
+
model = models[input_data.model_id]
|
40 |
+
processor = processors[input_data.model_id]
|
|
|
|
|
|
|
|
|
|
|
41 |
|
42 |
+
# Decode base64 image
|
43 |
+
image_data = base64.b64decode(input_data.image)
|
44 |
+
image = Image.open(BytesIO(image_data)).convert("RGB")
|
|
|
|
|
|
|
|
|
45 |
|
46 |
+
user_prompt = '<|user|>\n'
|
47 |
+
assistant_prompt = '<|assistant|>\n'
|
48 |
+
prompt_suffix = "<|end|>\n"
|
49 |
+
prompt = f"{user_prompt}<|image_1|>\n{input_data.text_input}{prompt_suffix}{assistant_prompt}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
50 |
|
51 |
+
inputs = processor(prompt, image, return_tensors="pt").to("cuda:0")
|
52 |
+
generate_ids = model.generate(
|
53 |
+
**inputs,
|
54 |
+
max_new_tokens=1000,
|
55 |
+
eos_token_id=processor.tokenizer.eos_token_id,
|
56 |
+
)
|
57 |
+
generate_ids = generate_ids[:, inputs['input_ids'].shape[1]:]
|
58 |
+
response = processor.batch_decode(
|
59 |
+
generate_ids,
|
60 |
+
skip_special_tokens=True,
|
61 |
+
clean_up_tokenization_spaces=False
|
62 |
+
)[0]
|
63 |
|
64 |
+
return {"response": response}
|
65 |
+
except Exception as e:
|
66 |
+
raise HTTPException(status_code=500, detail=str(e))
|