Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,13 +2,17 @@ import gradio as gr
|
|
| 2 |
from transformers import AutoProcessor, Qwen2_5_VLForConditionalGeneration, TextIteratorStreamer
|
| 3 |
from qwen_vl_utils import process_vision_info
|
| 4 |
from threading import Thread
|
| 5 |
-
import torch
|
| 6 |
import spaces
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
model = Qwen2_5_VLForConditionalGeneration.from_pretrained(
|
| 11 |
-
|
| 12 |
)
|
| 13 |
|
| 14 |
@spaces.GPU
|
|
@@ -28,6 +32,8 @@ def respond(
|
|
| 28 |
"content": system_message
|
| 29 |
}]
|
| 30 |
|
|
|
|
|
|
|
| 31 |
for message in chat_history:
|
| 32 |
if isinstance(message["content"], str):
|
| 33 |
messages.append({
|
|
@@ -45,15 +51,22 @@ def respond(
|
|
| 45 |
]
|
| 46 |
})
|
| 47 |
|
| 48 |
-
|
| 49 |
-
{
|
| 50 |
"role": "user",
|
| 51 |
"content": [
|
| 52 |
{ "type": "text", "text": text },
|
| 53 |
-
*[{"type": "image", "image": image} for image in files]
|
| 54 |
]
|
| 55 |
-
}
|
| 56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
image_inputs, video_inputs = process_vision_info(messages)
|
| 58 |
prompt = processor.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
|
| 59 |
inputs = processor(
|
|
@@ -65,7 +78,7 @@ def respond(
|
|
| 65 |
).to(model.device)
|
| 66 |
|
| 67 |
streamer = TextIteratorStreamer(processor, skip_prompt=True, skip_special_tokens=True)
|
| 68 |
-
generation_kwargs = dict(inputs, streamer=streamer, max_new_tokens=max_tokens, temperature=temperature, top_p=top_p)
|
| 69 |
|
| 70 |
thread = Thread(target=model.generate, kwargs=generation_kwargs)
|
| 71 |
thread.start()
|
|
@@ -93,4 +106,4 @@ demo = gr.ChatInterface(
|
|
| 93 |
],
|
| 94 |
)
|
| 95 |
|
| 96 |
-
demo.launch(debug=True)
|
|
|
|
| 2 |
from transformers import AutoProcessor, Qwen2_5_VLForConditionalGeneration, TextIteratorStreamer
|
| 3 |
from qwen_vl_utils import process_vision_info
|
| 4 |
from threading import Thread
|
|
|
|
| 5 |
import spaces
|
| 6 |
|
| 7 |
+
file_path = "csfufu/Revisual-R1-final"
|
| 8 |
+
|
| 9 |
+
processor = AutoProcessor.from_pretrained(
|
| 10 |
+
file_path,
|
| 11 |
+
min_pixels=256*28*28,
|
| 12 |
+
max_pixels=1280*28*28
|
| 13 |
+
)
|
| 14 |
model = Qwen2_5_VLForConditionalGeneration.from_pretrained(
|
| 15 |
+
file_path, torch_dtype="auto", device_map="auto"
|
| 16 |
)
|
| 17 |
|
| 18 |
@spaces.GPU
|
|
|
|
| 32 |
"content": system_message
|
| 33 |
}]
|
| 34 |
|
| 35 |
+
print(chat_history)
|
| 36 |
+
|
| 37 |
for message in chat_history:
|
| 38 |
if isinstance(message["content"], str):
|
| 39 |
messages.append({
|
|
|
|
| 51 |
]
|
| 52 |
})
|
| 53 |
|
| 54 |
+
if text:
|
| 55 |
+
messages.append({
|
| 56 |
"role": "user",
|
| 57 |
"content": [
|
| 58 |
{ "type": "text", "text": text },
|
|
|
|
| 59 |
]
|
| 60 |
+
})
|
| 61 |
+
|
| 62 |
+
for file in files:
|
| 63 |
+
messages.append({
|
| 64 |
+
"role": "user",
|
| 65 |
+
"content": [
|
| 66 |
+
{ "type": "image", "image": file }
|
| 67 |
+
]
|
| 68 |
+
})
|
| 69 |
+
|
| 70 |
image_inputs, video_inputs = process_vision_info(messages)
|
| 71 |
prompt = processor.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
|
| 72 |
inputs = processor(
|
|
|
|
| 78 |
).to(model.device)
|
| 79 |
|
| 80 |
streamer = TextIteratorStreamer(processor, skip_prompt=True, skip_special_tokens=True)
|
| 81 |
+
generation_kwargs = dict(**inputs, streamer=streamer, max_new_tokens=max_tokens, temperature=temperature, top_p=top_p)
|
| 82 |
|
| 83 |
thread = Thread(target=model.generate, kwargs=generation_kwargs)
|
| 84 |
thread.start()
|
|
|
|
| 106 |
],
|
| 107 |
)
|
| 108 |
|
| 109 |
+
demo.launch(debug=True)
|