Update app.py
Browse files
app.py
CHANGED
@@ -1,7 +1,6 @@
|
|
1 |
import subprocess
|
2 |
subprocess.run('pip install flash-attn==2.7.0.post2 --no-build-isolation', env={'FLASH_ATTENTION_SKIP_CUDA_BUILD': "TRUE"}, shell=True)
|
3 |
|
4 |
-
import spaces
|
5 |
import os
|
6 |
import re
|
7 |
import logging
|
@@ -12,14 +11,17 @@ import torch
|
|
12 |
import gradio as gr
|
13 |
from transformers import AutoModelForCausalLM, TextIteratorStreamer
|
14 |
|
|
|
15 |
model_name = 'AIDC-AI/Ovis2-8B'
|
16 |
use_thread = False
|
17 |
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
|
|
|
|
23 |
text_tokenizer = model.get_text_tokenizer()
|
24 |
visual_tokenizer = model.get_visual_tokenizer()
|
25 |
streamer = TextIteratorStreamer(text_tokenizer, skip_prompt=True, skip_special_tokens=True)
|
@@ -45,9 +47,10 @@ def initialize_gen_kwargs():
|
|
45 |
def submit_chat(chatbot, text_input):
|
46 |
response = ''
|
47 |
chatbot.append((text_input, response))
|
48 |
-
return chatbot
|
49 |
|
50 |
-
@
|
|
|
51 |
def ovis_chat(chatbot: List[List[str]], image_input: Any):
|
52 |
conversations, model_inputs = prepare_inputs(chatbot, image_input)
|
53 |
gen_kwargs = initialize_gen_kwargs()
|
@@ -72,14 +75,8 @@ def ovis_chat(chatbot: List[List[str]], image_input: Any):
|
|
72 |
|
73 |
log_conversation(chatbot)
|
74 |
|
75 |
-
|
76 |
def prepare_inputs(chatbot: List[List[str]], image_input: Any):
|
77 |
-
|
78 |
-
# "from": "system",
|
79 |
-
# "value": "You are a helpful assistant, and your task is to provide reliable and structured responses to users."
|
80 |
-
# }]
|
81 |
-
conversations= []
|
82 |
-
|
83 |
for query, response in chatbot[:-1]:
|
84 |
conversations.extend([
|
85 |
{"from": "human", "value": query},
|
@@ -90,6 +87,7 @@ def prepare_inputs(chatbot: List[List[str]], image_input: Any):
|
|
90 |
conversations.append({"from": "human", "value": last_query})
|
91 |
|
92 |
if image_input is not None:
|
|
|
93 |
for conv in conversations:
|
94 |
if conv["from"] == "human":
|
95 |
conv["value"] = f'{image_placeholder}\n{conv["value"]}'
|
@@ -116,6 +114,7 @@ def log_conversation(chatbot):
|
|
116 |
def clear_chat():
|
117 |
return [], None, ""
|
118 |
|
|
|
119 |
with open(f"{cur_dir}/resource/logo.svg", "r", encoding="utf-8") as svg_file:
|
120 |
svg_content = svg_file.read()
|
121 |
font_size = "2.5em"
|
@@ -125,7 +124,14 @@ html = f"""
|
|
125 |
<span style="display: inline-block; vertical-align: middle;">{svg_content}</span>
|
126 |
<span style="display: inline-block; vertical-align: middle;">{model_name.split('/')[-1]}</span>
|
127 |
</p>
|
128 |
-
<center
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
129 |
"""
|
130 |
|
131 |
latex_delimiters_set = [{
|
@@ -158,12 +164,68 @@ latex_delimiters_set = [{
|
|
158 |
"display": True
|
159 |
}]
|
160 |
|
161 |
-
text_input = gr.Textbox(label="
|
162 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
163 |
gr.HTML(html)
|
164 |
with gr.Row():
|
165 |
with gr.Column(scale=3):
|
166 |
-
image_input = gr.Image(label="
|
167 |
gr.Examples(
|
168 |
examples=[
|
169 |
[f"{cur_dir}/examples/ovis2_math0.jpg", "Each face of the polyhedron shown is either a triangle or a square. Each square borders 4 triangles, and each triangle borders 3 squares. The polyhedron has 6 squares. How many triangles does it have?\n\nProvide a step-by-step solution to the problem, and conclude with 'the answer is' followed by the final solution."],
|
@@ -178,11 +240,27 @@ with gr.Blocks(title=model_name.split('/')[-1], theme=gr.themes.Ocean()) as demo
|
|
178 |
chatbot = gr.Chatbot(label="Ovis", layout="panel", height=600, show_copy_button=True, latex_delimiters=latex_delimiters_set)
|
179 |
text_input.render()
|
180 |
with gr.Row():
|
181 |
-
send_btn = gr.Button("Send"
|
182 |
-
clear_btn = gr.Button("Clear"
|
183 |
|
184 |
-
send_click_event = send_btn.click(
|
185 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
186 |
clear_btn.click(clear_chat, outputs=[chatbot, image_input, text_input])
|
187 |
|
188 |
demo.launch()
|
|
|
1 |
import subprocess
|
2 |
subprocess.run('pip install flash-attn==2.7.0.post2 --no-build-isolation', env={'FLASH_ATTENTION_SKIP_CUDA_BUILD': "TRUE"}, shell=True)
|
3 |
|
|
|
4 |
import os
|
5 |
import re
|
6 |
import logging
|
|
|
11 |
import gradio as gr
|
12 |
from transformers import AutoModelForCausalLM, TextIteratorStreamer
|
13 |
|
14 |
+
# λͺ¨λΈ λ° ν ν¬λμ΄μ λ‘λ©
|
15 |
model_name = 'AIDC-AI/Ovis2-8B'
|
16 |
use_thread = False
|
17 |
|
18 |
+
model = AutoModelForCausalLM.from_pretrained(
|
19 |
+
model_name,
|
20 |
+
torch_dtype=torch.bfloat16,
|
21 |
+
multimodal_max_length=8192,
|
22 |
+
trust_remote_code=True
|
23 |
+
).to(device='cuda')
|
24 |
+
|
25 |
text_tokenizer = model.get_text_tokenizer()
|
26 |
visual_tokenizer = model.get_visual_tokenizer()
|
27 |
streamer = TextIteratorStreamer(text_tokenizer, skip_prompt=True, skip_special_tokens=True)
|
|
|
47 |
def submit_chat(chatbot, text_input):
|
48 |
response = ''
|
49 |
chatbot.append((text_input, response))
|
50 |
+
return chatbot, ''
|
51 |
|
52 |
+
@gradio.routes.no_temp_folder()
|
53 |
+
@gradio.gpu()
|
54 |
def ovis_chat(chatbot: List[List[str]], image_input: Any):
|
55 |
conversations, model_inputs = prepare_inputs(chatbot, image_input)
|
56 |
gen_kwargs = initialize_gen_kwargs()
|
|
|
75 |
|
76 |
log_conversation(chatbot)
|
77 |
|
|
|
78 |
def prepare_inputs(chatbot: List[List[str]], image_input: Any):
|
79 |
+
conversations = []
|
|
|
|
|
|
|
|
|
|
|
80 |
for query, response in chatbot[:-1]:
|
81 |
conversations.extend([
|
82 |
{"from": "human", "value": query},
|
|
|
87 |
conversations.append({"from": "human", "value": last_query})
|
88 |
|
89 |
if image_input is not None:
|
90 |
+
# μ΄λ―Έμ§κ° ν¬ν¨λλ©΄ 첫 λ²μ§Έ human λ©μμ§μ μ΄λ―Έμ§ νκ·Έ μΆκ°
|
91 |
for conv in conversations:
|
92 |
if conv["from"] == "human":
|
93 |
conv["value"] = f'{image_placeholder}\n{conv["value"]}'
|
|
|
114 |
def clear_chat():
|
115 |
return [], None, ""
|
116 |
|
117 |
+
# λ‘κ³ SVG λ‘λ λ° μ€νμΌ μμ
|
118 |
with open(f"{cur_dir}/resource/logo.svg", "r", encoding="utf-8") as svg_file:
|
119 |
svg_content = svg_file.read()
|
120 |
font_size = "2.5em"
|
|
|
124 |
<span style="display: inline-block; vertical-align: middle;">{svg_content}</span>
|
125 |
<span style="display: inline-block; vertical-align: middle;">{model_name.split('/')[-1]}</span>
|
126 |
</p>
|
127 |
+
<center>
|
128 |
+
<font size=3>
|
129 |
+
<b>Ovis</b> has been open-sourced on
|
130 |
+
<a href='https://huggingface.co/{model_name}'>π Huggingface</a> and
|
131 |
+
<a href='https://github.com/AIDC-AI/Ovis'>π GitHub</a>.
|
132 |
+
If you find Ovis useful, a likeβ€οΈ or a starπ would be appreciated.
|
133 |
+
</font>
|
134 |
+
</center>
|
135 |
"""
|
136 |
|
137 |
latex_delimiters_set = [{
|
|
|
164 |
"display": True
|
165 |
}]
|
166 |
|
167 |
+
text_input = gr.Textbox(label="Prompt", placeholder="Enter your text here...", lines=1, container=False)
|
168 |
+
|
169 |
+
# 컀μ€ν
CSS (λ°°κ²½ κ·ΈλΌλ°μ΄μ
, λ°ν¬λͺ
컨ν
μ΄λ, λ²νΌ μ λλ©μ΄μ
λ±)
|
170 |
+
custom_css = """
|
171 |
+
body {
|
172 |
+
background: linear-gradient(135deg, #667eea, #764ba2);
|
173 |
+
font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;
|
174 |
+
color: #333;
|
175 |
+
margin: 0;
|
176 |
+
padding: 0;
|
177 |
+
}
|
178 |
+
.gradio-container {
|
179 |
+
background: rgba(255, 255, 255, 0.95);
|
180 |
+
border-radius: 15px;
|
181 |
+
padding: 30px 40px;
|
182 |
+
box-shadow: 0 8px 30px rgba(0, 0, 0, 0.3);
|
183 |
+
margin: 40px auto;
|
184 |
+
max-width: 1200px;
|
185 |
+
}
|
186 |
+
.gradio-container h1 {
|
187 |
+
color: #333;
|
188 |
+
text-shadow: 1px 1px 2px rgba(0, 0, 0, 0.2);
|
189 |
+
}
|
190 |
+
.fillable {
|
191 |
+
width: 95% !important;
|
192 |
+
max-width: unset !important;
|
193 |
+
}
|
194 |
+
#examples_container {
|
195 |
+
margin: auto;
|
196 |
+
width: 90%;
|
197 |
+
}
|
198 |
+
#examples_row {
|
199 |
+
justify-content: center;
|
200 |
+
}
|
201 |
+
.sidebar {
|
202 |
+
background: rgba(255, 255, 255, 0.98);
|
203 |
+
border-radius: 10px;
|
204 |
+
padding: 20px;
|
205 |
+
box-shadow: 0 4px 15px rgba(0, 0, 0, 0.2);
|
206 |
+
}
|
207 |
+
button, .btn {
|
208 |
+
background: linear-gradient(90deg, #ff8a00, #e52e71);
|
209 |
+
border: none;
|
210 |
+
color: #fff;
|
211 |
+
padding: 12px 24px;
|
212 |
+
text-transform: uppercase;
|
213 |
+
font-weight: bold;
|
214 |
+
letter-spacing: 1px;
|
215 |
+
border-radius: 5px;
|
216 |
+
cursor: pointer;
|
217 |
+
transition: transform 0.2s ease-in-out;
|
218 |
+
}
|
219 |
+
button:hover, .btn:hover {
|
220 |
+
transform: scale(1.05);
|
221 |
+
}
|
222 |
+
"""
|
223 |
+
|
224 |
+
with gr.Blocks(css=custom_css, title=model_name.split('/')[-1]) as demo:
|
225 |
gr.HTML(html)
|
226 |
with gr.Row():
|
227 |
with gr.Column(scale=3):
|
228 |
+
image_input = gr.Image(label="Image", height=350, type="pil")
|
229 |
gr.Examples(
|
230 |
examples=[
|
231 |
[f"{cur_dir}/examples/ovis2_math0.jpg", "Each face of the polyhedron shown is either a triangle or a square. Each square borders 4 triangles, and each triangle borders 3 squares. The polyhedron has 6 squares. How many triangles does it have?\n\nProvide a step-by-step solution to the problem, and conclude with 'the answer is' followed by the final solution."],
|
|
|
240 |
chatbot = gr.Chatbot(label="Ovis", layout="panel", height=600, show_copy_button=True, latex_delimiters=latex_delimiters_set)
|
241 |
text_input.render()
|
242 |
with gr.Row():
|
243 |
+
send_btn = gr.Button("Send")
|
244 |
+
clear_btn = gr.Button("Clear")
|
245 |
|
246 |
+
send_click_event = send_btn.click(
|
247 |
+
submit_chat,
|
248 |
+
inputs=[chatbot, text_input],
|
249 |
+
outputs=[chatbot, text_input]
|
250 |
+
).then(
|
251 |
+
ovis_chat,
|
252 |
+
inputs=[chatbot, image_input],
|
253 |
+
outputs=chatbot
|
254 |
+
)
|
255 |
+
submit_event = text_input.submit(
|
256 |
+
submit_chat,
|
257 |
+
inputs=[chatbot, text_input],
|
258 |
+
outputs=[chatbot, text_input]
|
259 |
+
).then(
|
260 |
+
ovis_chat,
|
261 |
+
inputs=[chatbot, image_input],
|
262 |
+
outputs=chatbot
|
263 |
+
)
|
264 |
clear_btn.click(clear_chat, outputs=[chatbot, image_input, text_input])
|
265 |
|
266 |
demo.launch()
|