Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,18 +4,10 @@ from transformers import Qwen2VLForConditionalGeneration, AutoProcessor
|
|
| 4 |
from qwen_vl_utils import process_vision_info
|
| 5 |
import torch
|
| 6 |
from PIL import Image
|
| 7 |
-
import subprocess
|
| 8 |
from datetime import datetime
|
| 9 |
import numpy as np
|
| 10 |
import os
|
| 11 |
|
| 12 |
-
|
| 13 |
-
# subprocess.run('pip install flash-attn --no-build-isolation', env={'FLASH_ATTENTION_SKIP_CUDA_BUILD': "TRUE"}, shell=True)
|
| 14 |
-
|
| 15 |
-
# models = {
|
| 16 |
-
# "Qwen/Qwen2-VL-7B-Instruct": AutoModelForCausalLM.from_pretrained("Qwen/Qwen2-VL-7B-Instruct", trust_remote_code=True, torch_dtype="auto", _attn_implementation="flash_attention_2").cuda().eval()
|
| 17 |
-
|
| 18 |
-
# }
|
| 19 |
def array_to_image_path(image_array):
|
| 20 |
if image_array is None:
|
| 21 |
raise ValueError("No image provided. Please upload an image before submitting.")
|
|
@@ -35,8 +27,7 @@ def array_to_image_path(image_array):
|
|
| 35 |
return full_path
|
| 36 |
|
| 37 |
models = {
|
| 38 |
-
"Qwen/Qwen2-VL-7B-Instruct": Qwen2VLForConditionalGeneration.from_pretrained("Qwen/Qwen2-VL-7B-Instruct", trust_remote_code=True, torch_dtype="auto").
|
| 39 |
-
|
| 40 |
}
|
| 41 |
|
| 42 |
processors = {
|
|
@@ -45,14 +36,10 @@ processors = {
|
|
| 45 |
|
| 46 |
DESCRIPTION = "[Qwen2-VL-7B Demo](https://huggingface.co/Qwen/Qwen2-VL-7B-Instruct)"
|
| 47 |
|
| 48 |
-
kwargs = {}
|
| 49 |
-
kwargs['torch_dtype'] = torch.bfloat16
|
| 50 |
-
|
| 51 |
user_prompt = '<|user|>\n'
|
| 52 |
assistant_prompt = '<|assistant|>\n'
|
| 53 |
prompt_suffix = "<|end|>\n"
|
| 54 |
|
| 55 |
-
@spaces.GPU
|
| 56 |
def run_example(image, text_input=None, model_id="Qwen/Qwen2-VL-7B-Instruct"):
|
| 57 |
image_path = array_to_image_path(image)
|
| 58 |
|
|
@@ -87,7 +74,8 @@ def run_example(image, text_input=None, model_id="Qwen/Qwen2-VL-7B-Instruct"):
|
|
| 87 |
padding=True,
|
| 88 |
return_tensors="pt",
|
| 89 |
)
|
| 90 |
-
|
|
|
|
| 91 |
|
| 92 |
# Inference: Generation of the output
|
| 93 |
generated_ids = model.generate(**inputs, max_new_tokens=1024)
|
|
@@ -123,4 +111,4 @@ with gr.Blocks(css=css) as demo:
|
|
| 123 |
submit_btn.click(run_example, [input_img, text_input, model_selector], [output_text])
|
| 124 |
|
| 125 |
demo.queue(api_open=False)
|
| 126 |
-
demo.launch(debug=True)
|
|
|
|
| 4 |
from qwen_vl_utils import process_vision_info
|
| 5 |
import torch
|
| 6 |
from PIL import Image
|
|
|
|
| 7 |
from datetime import datetime
|
| 8 |
import numpy as np
|
| 9 |
import os
|
| 10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
def array_to_image_path(image_array):
|
| 12 |
if image_array is None:
|
| 13 |
raise ValueError("No image provided. Please upload an image before submitting.")
|
|
|
|
| 27 |
return full_path
|
| 28 |
|
| 29 |
models = {
|
| 30 |
+
"Qwen/Qwen2-VL-7B-Instruct": Qwen2VLForConditionalGeneration.from_pretrained("Qwen/Qwen2-VL-7B-Instruct", trust_remote_code=True, torch_dtype="auto").eval()
|
|
|
|
| 31 |
}
|
| 32 |
|
| 33 |
processors = {
|
|
|
|
| 36 |
|
| 37 |
DESCRIPTION = "[Qwen2-VL-7B Demo](https://huggingface.co/Qwen/Qwen2-VL-7B-Instruct)"
|
| 38 |
|
|
|
|
|
|
|
|
|
|
| 39 |
user_prompt = '<|user|>\n'
|
| 40 |
assistant_prompt = '<|assistant|>\n'
|
| 41 |
prompt_suffix = "<|end|>\n"
|
| 42 |
|
|
|
|
| 43 |
def run_example(image, text_input=None, model_id="Qwen/Qwen2-VL-7B-Instruct"):
|
| 44 |
image_path = array_to_image_path(image)
|
| 45 |
|
|
|
|
| 74 |
padding=True,
|
| 75 |
return_tensors="pt",
|
| 76 |
)
|
| 77 |
+
# Move inputs to CPU
|
| 78 |
+
inputs = inputs.to("cpu")
|
| 79 |
|
| 80 |
# Inference: Generation of the output
|
| 81 |
generated_ids = model.generate(**inputs, max_new_tokens=1024)
|
|
|
|
| 111 |
submit_btn.click(run_example, [input_img, text_input, model_selector], [output_text])
|
| 112 |
|
| 113 |
demo.queue(api_open=False)
|
| 114 |
+
demo.launch(debug=True)
|