Upload 3 files
Browse files- app.py +68 -0
- gitattributes +35 -0
- requirements.txt +7 -0
app.py
ADDED
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import spaces
|
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 |
+
models = {
|
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("microsoft/Phi-3.5-vision-instruct", trust_remote_code=True)
|
16 |
+
}
|
17 |
+
|
18 |
+
DESCRIPTION = "[Phi-3.5-vision Demo](https://huggingface.co/microsoft/Phi-3.5-vision-instruct)"
|
19 |
+
|
20 |
+
kwargs = {}
|
21 |
+
kwargs['torch_dtype'] = torch.bfloat16
|
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 |
+
inputs = processor(prompt, image, return_tensors="pt").to("cuda:0")
|
36 |
+
generate_ids = model.generate(**inputs,
|
37 |
+
max_new_tokens=1000,
|
38 |
+
eos_token_id=processor.tokenizer.eos_token_id,
|
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 |
+
css = """
|
47 |
+
#output {
|
48 |
+
height: 500px;
|
49 |
+
overflow: auto;
|
50 |
+
border: 1px solid #ccc;
|
51 |
+
}
|
52 |
+
"""
|
53 |
+
|
54 |
+
with gr.Blocks(css=css) as demo:
|
55 |
+
gr.Markdown(DESCRIPTION)
|
56 |
+
with gr.Tab(label="Phi-3.5 Input"):
|
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="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 |
+
submit_btn.click(run_example, [input_img, text_input, model_selector], [output_text])
|
67 |
+
|
68 |
+
demo.launch(debug=True, show_error=True)
|
gitattributes
ADDED
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
*.7z filter=lfs diff=lfs merge=lfs -text
|
2 |
+
*.arrow filter=lfs diff=lfs merge=lfs -text
|
3 |
+
*.bin filter=lfs diff=lfs merge=lfs -text
|
4 |
+
*.bz2 filter=lfs diff=lfs merge=lfs -text
|
5 |
+
*.ckpt filter=lfs diff=lfs merge=lfs -text
|
6 |
+
*.ftz filter=lfs diff=lfs merge=lfs -text
|
7 |
+
*.gz filter=lfs diff=lfs merge=lfs -text
|
8 |
+
*.h5 filter=lfs diff=lfs merge=lfs -text
|
9 |
+
*.joblib filter=lfs diff=lfs merge=lfs -text
|
10 |
+
*.lfs.* filter=lfs diff=lfs merge=lfs -text
|
11 |
+
*.mlmodel filter=lfs diff=lfs merge=lfs -text
|
12 |
+
*.model filter=lfs diff=lfs merge=lfs -text
|
13 |
+
*.msgpack filter=lfs diff=lfs merge=lfs -text
|
14 |
+
*.npy filter=lfs diff=lfs merge=lfs -text
|
15 |
+
*.npz filter=lfs diff=lfs merge=lfs -text
|
16 |
+
*.onnx filter=lfs diff=lfs merge=lfs -text
|
17 |
+
*.ot filter=lfs diff=lfs merge=lfs -text
|
18 |
+
*.parquet filter=lfs diff=lfs merge=lfs -text
|
19 |
+
*.pb filter=lfs diff=lfs merge=lfs -text
|
20 |
+
*.pickle filter=lfs diff=lfs merge=lfs -text
|
21 |
+
*.pkl filter=lfs diff=lfs merge=lfs -text
|
22 |
+
*.pt filter=lfs diff=lfs merge=lfs -text
|
23 |
+
*.pth filter=lfs diff=lfs merge=lfs -text
|
24 |
+
*.rar filter=lfs diff=lfs merge=lfs -text
|
25 |
+
*.safetensors filter=lfs diff=lfs merge=lfs -text
|
26 |
+
saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
27 |
+
*.tar.* filter=lfs diff=lfs merge=lfs -text
|
28 |
+
*.tar filter=lfs diff=lfs merge=lfs -text
|
29 |
+
*.tflite filter=lfs diff=lfs merge=lfs -text
|
30 |
+
*.tgz filter=lfs diff=lfs merge=lfs -text
|
31 |
+
*.wasm filter=lfs diff=lfs merge=lfs -text
|
32 |
+
*.xz filter=lfs diff=lfs merge=lfs -text
|
33 |
+
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
+
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
+
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
requirements.txt
ADDED
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
numpy==1.24.4
|
2 |
+
Pillow==10.3.0
|
3 |
+
Requests==2.31.0
|
4 |
+
torch
|
5 |
+
torchvision
|
6 |
+
transformers==4.43.0
|
7 |
+
accelerate==0.30.0
|