Spaces:
Running
on
Zero
Running
on
Zero
Update Gradio app with multiple files
Browse files
app.py
CHANGED
|
@@ -1,7 +1,7 @@
|
|
| 1 |
import spaces
|
| 2 |
import gradio as gr
|
| 3 |
import torch
|
| 4 |
-
from transformers import
|
| 5 |
from PIL import Image
|
| 6 |
import numpy as np
|
| 7 |
from typing import List, Dict, Any, Optional, Tuple
|
|
@@ -12,9 +12,9 @@ import base64
|
|
| 12 |
model_id = "Qwen/Qwen3-VL-4B-Instruct"
|
| 13 |
|
| 14 |
# Load model with optimizations for inference
|
| 15 |
-
model =
|
| 16 |
model_id,
|
| 17 |
-
|
| 18 |
device_map="auto"
|
| 19 |
)
|
| 20 |
processor = AutoProcessor.from_pretrained(model_id)
|
|
@@ -72,24 +72,14 @@ def process_chat_message(
|
|
| 72 |
})
|
| 73 |
|
| 74 |
# Prepare inputs for the model
|
| 75 |
-
|
| 76 |
messages,
|
| 77 |
-
tokenize=
|
| 78 |
-
add_generation_prompt=True
|
|
|
|
|
|
|
| 79 |
)
|
| 80 |
|
| 81 |
-
if image is not None:
|
| 82 |
-
inputs = processor(
|
| 83 |
-
text=[text],
|
| 84 |
-
images=[image],
|
| 85 |
-
return_tensors="pt"
|
| 86 |
-
).to(model.device)
|
| 87 |
-
else:
|
| 88 |
-
inputs = processor(
|
| 89 |
-
text=[text],
|
| 90 |
-
return_tensors="pt"
|
| 91 |
-
).to(model.device)
|
| 92 |
-
|
| 93 |
# Generate response
|
| 94 |
with torch.no_grad():
|
| 95 |
generated_ids = model.generate(
|
|
|
|
| 1 |
import spaces
|
| 2 |
import gradio as gr
|
| 3 |
import torch
|
| 4 |
+
from transformers import Qwen3VLForConditionalGeneration, AutoProcessor
|
| 5 |
from PIL import Image
|
| 6 |
import numpy as np
|
| 7 |
from typing import List, Dict, Any, Optional, Tuple
|
|
|
|
| 12 |
model_id = "Qwen/Qwen3-VL-4B-Instruct"
|
| 13 |
|
| 14 |
# Load model with optimizations for inference
|
| 15 |
+
model = Qwen3VLForConditionalGeneration.from_pretrained(
|
| 16 |
model_id,
|
| 17 |
+
dtype="auto",
|
| 18 |
device_map="auto"
|
| 19 |
)
|
| 20 |
processor = AutoProcessor.from_pretrained(model_id)
|
|
|
|
| 72 |
})
|
| 73 |
|
| 74 |
# Prepare inputs for the model
|
| 75 |
+
inputs = processor.apply_chat_template(
|
| 76 |
messages,
|
| 77 |
+
tokenize=True,
|
| 78 |
+
add_generation_prompt=True,
|
| 79 |
+
return_dict=True,
|
| 80 |
+
return_tensors="pt"
|
| 81 |
)
|
| 82 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
# Generate response
|
| 84 |
with torch.no_grad():
|
| 85 |
generated_ids = model.generate(
|