Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -5,10 +5,14 @@ from qwen_vl_utils import process_vision_info
|
|
5 |
import torch
|
6 |
import time
|
7 |
|
|
|
|
|
|
|
8 |
local_path = "Fancy-MLLM/R1-OneVision-7B"
|
9 |
|
|
|
10 |
model = Qwen2_5_VLForConditionalGeneration.from_pretrained(
|
11 |
-
local_path, torch_dtype="auto", device_map=
|
12 |
)
|
13 |
processor = AutoProcessor.from_pretrained(local_path)
|
14 |
|
@@ -26,8 +30,6 @@ def generate_output(image, text, button_click):
|
|
26 |
|
27 |
# Prepare inputs for the model
|
28 |
text_input = processor.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
|
29 |
-
# print(text_input)
|
30 |
-
# import pdb; pdb.set_trace()
|
31 |
image_inputs, video_inputs = process_vision_info(messages)
|
32 |
inputs = processor(
|
33 |
text=[text_input],
|
@@ -36,6 +38,8 @@ def generate_output(image, text, button_click):
|
|
36 |
padding=True,
|
37 |
return_tensors="pt",
|
38 |
)
|
|
|
|
|
39 |
inputs = inputs.to(model.device)
|
40 |
|
41 |
streamer = TextIteratorStreamer(processor, skip_prompt=True, skip_special_tokens=True)
|
@@ -48,6 +52,7 @@ def generate_output(image, text, button_click):
|
|
48 |
temperature=0.01,
|
49 |
repetition_penalty=1.0,
|
50 |
)
|
|
|
51 |
thread = Thread(target=model.generate, kwargs=generation_kwargs)
|
52 |
thread.start()
|
53 |
generated_text = ''
|
@@ -56,8 +61,6 @@ def generate_output(image, text, button_click):
|
|
56 |
for new_text in streamer:
|
57 |
generated_text += new_text
|
58 |
yield f"{generated_text}"
|
59 |
-
# print(f"Current text: {generated_text}") # 调试输出
|
60 |
-
# yield generated_text # 直接输出原始文本
|
61 |
except Exception as e:
|
62 |
print(f"Error: {e}")
|
63 |
yield f"Error occurred: {str(e)}"
|
@@ -68,7 +71,6 @@ Css = """
|
|
68 |
white-space: pre-wrap;
|
69 |
word-wrap: break-word;
|
70 |
}
|
71 |
-
|
72 |
#output-markdown .math {
|
73 |
overflow-x: auto;
|
74 |
max-width: 100%;
|
@@ -87,7 +89,6 @@ Css = """
|
|
87 |
#qwen-md .katex-display>.katex>.katex-html { display: inline; }
|
88 |
"""
|
89 |
|
90 |
-
|
91 |
with gr.Blocks(css=Css) as demo:
|
92 |
gr.HTML("""<center><font size=8>🦖 R1-OneVision Demo</center>""")
|
93 |
|
@@ -105,84 +106,3 @@ with gr.Blocks(css=Css) as demo:
|
|
105 |
submit_btn.click(fn=generate_output, inputs=[input_image, input_text], outputs=output_text)
|
106 |
|
107 |
demo.launch(share=True)
|
108 |
-
|
109 |
-
# Css = """
|
110 |
-
# #output-markdown {
|
111 |
-
# overflow-y: auto;
|
112 |
-
# white-space: pre-wrap;
|
113 |
-
# word-wrap: break-word;
|
114 |
-
# }
|
115 |
-
|
116 |
-
# #output-markdown .math {
|
117 |
-
# overflow-x: auto;
|
118 |
-
# max-width: 100%;
|
119 |
-
# }
|
120 |
-
# .markdown-text {
|
121 |
-
# white-space: pre-wrap;
|
122 |
-
# word-wrap: break-word;
|
123 |
-
# }
|
124 |
-
# #qwen-md .katex-display { display: inline; }
|
125 |
-
# #qwen-md .katex-display>.katex { display: inline; }
|
126 |
-
# #qwen-md .katex-display>.katex>.katex-html { display: inline; }
|
127 |
-
# """
|
128 |
-
|
129 |
-
# # UI 组件
|
130 |
-
# with gr.Blocks(css=Css) as demo:
|
131 |
-
# gr.HTML("""<center><font size=8>🦖 R1-OneVision Demo</center>""")
|
132 |
-
|
133 |
-
# with gr.Row():
|
134 |
-
# with gr.Column():
|
135 |
-
# input_image = gr.Image(type="pil", label="Upload")
|
136 |
-
# input_text = gr.Textbox(label="input your question")
|
137 |
-
# with gr.Row():
|
138 |
-
# with gr.Column():
|
139 |
-
# clear_btn = gr.ClearButton([input_image, input_text])
|
140 |
-
# with gr.Column():
|
141 |
-
# submit_btn = gr.Button("Submit", variant="primary")
|
142 |
-
|
143 |
-
# with gr.Column():
|
144 |
-
# output_text = gr.Markdown(
|
145 |
-
# label="Generated Response",
|
146 |
-
# max_height="80vh",
|
147 |
-
# min_height="50vh",
|
148 |
-
# container=True,
|
149 |
-
# latex_delimiters=[{
|
150 |
-
# "left": "\\(",
|
151 |
-
# "right": "\\)",
|
152 |
-
# "display": True
|
153 |
-
# }, {
|
154 |
-
# "left": "\\begin\{equation\}",
|
155 |
-
# "right": "\\end\{equation\}",
|
156 |
-
# "display": True
|
157 |
-
# }, {
|
158 |
-
# "left": "\\begin\{align\}",
|
159 |
-
# "right": "\\end\{align\}",
|
160 |
-
# "display": True
|
161 |
-
# }, {
|
162 |
-
# "left": "\\begin\{alignat\}",
|
163 |
-
# "right": "\\end\{alignat\}",
|
164 |
-
# "display": True
|
165 |
-
# }, {
|
166 |
-
# "left": "\\begin\{gather\}",
|
167 |
-
# "right": "\\end\{gather\}",
|
168 |
-
# "display": True
|
169 |
-
# }, {
|
170 |
-
# "left": "\\begin\{CD\}",
|
171 |
-
# "right": "\\end\{CD\}",
|
172 |
-
# "display": True
|
173 |
-
# }, {
|
174 |
-
# "left": "\\[",
|
175 |
-
# "right": "\\]",
|
176 |
-
# "display": True
|
177 |
-
# }],
|
178 |
-
# elem_id="qwen-md")
|
179 |
-
|
180 |
-
|
181 |
-
|
182 |
-
# submit_btn.click(
|
183 |
-
# fn=generate_output,
|
184 |
-
# inputs=[input_image, input_text],
|
185 |
-
# outputs=output_text,
|
186 |
-
# queue=True
|
187 |
-
# )
|
188 |
-
# demo.launch(share=True)
|
|
|
5 |
import torch
|
6 |
import time
|
7 |
|
8 |
+
# Check if a GPU is available
|
9 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
10 |
+
|
11 |
local_path = "Fancy-MLLM/R1-OneVision-7B"
|
12 |
|
13 |
+
# Load the model on the appropriate device (GPU if available, otherwise CPU)
|
14 |
model = Qwen2_5_VLForConditionalGeneration.from_pretrained(
|
15 |
+
local_path, torch_dtype="auto", device_map=device
|
16 |
)
|
17 |
processor = AutoProcessor.from_pretrained(local_path)
|
18 |
|
|
|
30 |
|
31 |
# Prepare inputs for the model
|
32 |
text_input = processor.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
|
|
|
|
|
33 |
image_inputs, video_inputs = process_vision_info(messages)
|
34 |
inputs = processor(
|
35 |
text=[text_input],
|
|
|
38 |
padding=True,
|
39 |
return_tensors="pt",
|
40 |
)
|
41 |
+
|
42 |
+
# Move inputs to the same device as the model
|
43 |
inputs = inputs.to(model.device)
|
44 |
|
45 |
streamer = TextIteratorStreamer(processor, skip_prompt=True, skip_special_tokens=True)
|
|
|
52 |
temperature=0.01,
|
53 |
repetition_penalty=1.0,
|
54 |
)
|
55 |
+
|
56 |
thread = Thread(target=model.generate, kwargs=generation_kwargs)
|
57 |
thread.start()
|
58 |
generated_text = ''
|
|
|
61 |
for new_text in streamer:
|
62 |
generated_text += new_text
|
63 |
yield f"{generated_text}"
|
|
|
|
|
64 |
except Exception as e:
|
65 |
print(f"Error: {e}")
|
66 |
yield f"Error occurred: {str(e)}"
|
|
|
71 |
white-space: pre-wrap;
|
72 |
word-wrap: break-word;
|
73 |
}
|
|
|
74 |
#output-markdown .math {
|
75 |
overflow-x: auto;
|
76 |
max-width: 100%;
|
|
|
89 |
#qwen-md .katex-display>.katex>.katex-html { display: inline; }
|
90 |
"""
|
91 |
|
|
|
92 |
with gr.Blocks(css=Css) as demo:
|
93 |
gr.HTML("""<center><font size=8>🦖 R1-OneVision Demo</center>""")
|
94 |
|
|
|
106 |
submit_btn.click(fn=generate_output, inputs=[input_image, input_text], outputs=output_text)
|
107 |
|
108 |
demo.launch(share=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|