Spaces:
Running
on
Zero
Running
on
Zero
Update app.py (#14)
Browse files- Update app.py (222ed92e2def4cc06102dfb624f32446cb93ebfb)
app.py
CHANGED
|
@@ -5,12 +5,30 @@ from qwen_vl_utils import process_vision_info
|
|
| 5 |
import torch
|
| 6 |
from PIL import Image
|
| 7 |
import subprocess
|
|
|
|
|
|
|
| 8 |
# subprocess.run('pip install flash-attn --no-build-isolation', env={'FLASH_ATTENTION_SKIP_CUDA_BUILD': "TRUE"}, shell=True)
|
| 9 |
|
| 10 |
# models = {
|
| 11 |
# "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()
|
| 12 |
|
| 13 |
# }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
models = {
|
| 15 |
"Qwen/Qwen2-VL-2B-Instruct": Qwen2VLForConditionalGeneration.from_pretrained("Qwen/Qwen2-VL-2B-Instruct", trust_remote_code=True, torch_dtype="auto").cuda().eval()
|
| 16 |
|
|
@@ -31,7 +49,9 @@ prompt_suffix = "<|end|>\n"
|
|
| 31 |
|
| 32 |
@spaces.GPU
|
| 33 |
def run_example(image, text_input=None, model_id="Qwen/Qwen2-VL-2B-Instruct"):
|
| 34 |
-
|
|
|
|
|
|
|
| 35 |
model = models[model_id]
|
| 36 |
processor = processors[model_id]
|
| 37 |
|
|
@@ -43,7 +63,7 @@ def run_example(image, text_input=None, model_id="Qwen/Qwen2-VL-2B-Instruct"):
|
|
| 43 |
"content": [
|
| 44 |
{
|
| 45 |
"type": "image",
|
| 46 |
-
"image":
|
| 47 |
},
|
| 48 |
{"type": "text", "text": text_input},
|
| 49 |
],
|
|
|
|
| 5 |
import torch
|
| 6 |
from PIL import Image
|
| 7 |
import subprocess
|
| 8 |
+
from datetime import datetime
|
| 9 |
+
|
| 10 |
# subprocess.run('pip install flash-attn --no-build-isolation', env={'FLASH_ATTENTION_SKIP_CUDA_BUILD': "TRUE"}, shell=True)
|
| 11 |
|
| 12 |
# models = {
|
| 13 |
# "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()
|
| 14 |
|
| 15 |
# }
|
| 16 |
+
def array_to_image_path(image_array):
|
| 17 |
+
# Convert numpy array to PIL Image
|
| 18 |
+
img = Image.fromarray(np.uint8(image_array))
|
| 19 |
+
|
| 20 |
+
# Generate a unique filename using timestamp
|
| 21 |
+
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
| 22 |
+
filename = f"image_{timestamp}.png"
|
| 23 |
+
|
| 24 |
+
# Save the image
|
| 25 |
+
img.save(filename)
|
| 26 |
+
|
| 27 |
+
# Get the full path of the saved image
|
| 28 |
+
full_path = os.path.abspath(filename)
|
| 29 |
+
|
| 30 |
+
return full_path
|
| 31 |
+
|
| 32 |
models = {
|
| 33 |
"Qwen/Qwen2-VL-2B-Instruct": Qwen2VLForConditionalGeneration.from_pretrained("Qwen/Qwen2-VL-2B-Instruct", trust_remote_code=True, torch_dtype="auto").cuda().eval()
|
| 34 |
|
|
|
|
| 49 |
|
| 50 |
@spaces.GPU
|
| 51 |
def run_example(image, text_input=None, model_id="Qwen/Qwen2-VL-2B-Instruct"):
|
| 52 |
+
image_path = array_to_image_path(image)
|
| 53 |
+
|
| 54 |
+
print(image_path)
|
| 55 |
model = models[model_id]
|
| 56 |
processor = processors[model_id]
|
| 57 |
|
|
|
|
| 63 |
"content": [
|
| 64 |
{
|
| 65 |
"type": "image",
|
| 66 |
+
"image": image_path,
|
| 67 |
},
|
| 68 |
{"type": "text", "text": text_input},
|
| 69 |
],
|