Spaces:
Running
Running
File size: 19,555 Bytes
f18a2c3 6c3eb98 f18a2c3 6c3eb98 f18a2c3 33cf5a0 f18a2c3 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 |
import base64
import os
import re
import gradio as gr
import modelscope_studio.components.antd as antd
import modelscope_studio.components.base as ms
from openai import OpenAI
# =========== Configuration
# API KEY
MODELSCOPE_ACCESS_TOKEN = os.getenv('MODELSCOPE_ACCESS_TOKEN')
client = OpenAI(api_key=MODELSCOPE_ACCESS_TOKEN,
base_url="https://api-inference.modelscope.cn/v1")
model = "Qwen/Qwen2.5-Coder-32B-Instruct"
# =========== Configuration
DEFAULT_SYSTEM_PROMPT = """You are a web development engineer, writing web pages according to the instructions below. You are a powerful code editing assistant capable of writing code and creating artifacts in conversations with users, or modifying and updating existing artifacts as requested by users.
All code is written in a single code block to form a complete code file for display, without separating HTML and JavaScript code. An artifact refers to a runnable complete code snippet, you prefer to integrate and output such complete runnable code rather than breaking it down into several code blocks. For certain types of code, they can render graphical interfaces in a UI window. After generation, please check the code execution again to ensure there are no errors in the output.
Output only the HTML, without any additional descriptive text."""
EXAMPLES = [
{
"title":
"Qwen,Start!",
"description":
"Help me design an interface with a purple button that says 'Qwen, Start!'. When the button is clicked, display a countdown from 5 in a very large font for 5 seconds.",
},
{
"title":
"Spam with emojis!",
"description":
"Write code in a single HTML file: Capture the click event, place a random number of emojis at the click position, and add gravity and collision effects to each emoji."
},
{
"title":
"TODO list",
"description":
"I want a TODO list that allows me to add tasks, delete tasks, and I would like the overall color theme to be purple."
},
]
DEFAULT_LOCALE = 'en_US'
DEFAULT_THEME = {
"token": {
"colorPrimary": "#6A57FF",
}
}
class GradioEvents:
@staticmethod
def generate_code(input_value, system_prompt_input_value, state_value):
# Define your code here
def remove_code_block(text):
pattern = r'```html\n(.+?)\n```'
match = re.search(pattern, text, re.DOTALL)
if match:
return match.group(1).strip()
else:
return text.strip()
def send_to_sandbox(code):
encoded_html = base64.b64encode(
code.encode('utf-8')).decode('utf-8')
data_uri = f"data:text/html;charset=utf-8;base64,{encoded_html}"
return f"<iframe src=\"{data_uri}\" width=\"100%\" height=\"100%\"></iframe>"
yield {
output_loading: gr.update(spinning=True),
state_tab: gr.update(active_key="loading"),
output: gr.update(value=None)
}
if input_value is None:
input_value = ''
messages = [{
'role': "system",
'content': system_prompt_input_value
}] + state_value["history"]
messages.append({'role': "user", 'content': input_value})
generator = client.chat.completions.create(model=model,
messages=messages,
stream=True)
response = ""
for chunk in generator:
content = chunk.choices[0].delta.content
response += content
if chunk.choices[0].finish_reason == 'stop':
state_value["history"] = messages + [{
'role': "assistant",
'content': response
}]
# Completed
yield {
output:
gr.update(value=response),
download_content:
gr.update(value=remove_code_block(response)),
state_tab:
gr.update(active_key="render"),
output_loading:
gr.update(spinning=False),
sandbox:
gr.update(
value=send_to_sandbox(remove_code_block(response))),
state:
gr.update(value=state_value)
}
else:
# Generating
yield {
output: gr.update(value=response),
output_loading: gr.update(spinning=False),
}
@staticmethod
def select_example(example: dict):
return lambda: gr.update(value=example["description"])
@staticmethod
def close_modal():
return gr.update(open=False)
@staticmethod
def open_modal():
return gr.update(open=True)
@staticmethod
def disable_btns(btns: list):
return lambda: [gr.update(disabled=True) for _ in btns]
@staticmethod
def enable_btns(btns: list):
return lambda: [gr.update(disabled=False) for _ in btns]
@staticmethod
def update_system_prompt(system_prompt_input_value, state_value):
state_value["system_prompt"] = system_prompt_input_value
return gr.update(value=state_value)
@staticmethod
def reset_system_prompt(state_value):
return gr.update(value=state_value["system_prompt"])
@staticmethod
def render_history(state):
return gr.update(value=state["history"])
@staticmethod
def clear_history(e: gr.EventData, state_value):
item = e._data["payload"][0]["key"]
if item == "clear":
gr.Success("History Cleared.")
state_value["history"] = []
return gr.update(value=state_value)
return gr.skip()
css = """
#coder-artifacts .output-empty,.output-loading {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
width: 100%;
min-height: 680px;
}
#coder-artifacts .output-html {
display: flex;
flex-direction: column;
width: 100%;
min-height: 680px;
}
#coder-artifacts .output-html > iframe {
flex: 1;
}
#code-artifacts-code-drawer .output-code {
flex:1;
}
#code-artifacts-code-drawer .output-code .ms-gr-ant-spin-nested-loading {
min-height: 100%;
}
"""
with gr.Blocks(css=css) as demo:
# Global State
state = gr.State({"system_prompt": DEFAULT_SYSTEM_PROMPT, "history": []})
with ms.Application(elem_id="coder-artifacts") as app:
with antd.ConfigProvider(theme=DEFAULT_THEME, locale=DEFAULT_LOCALE):
# Header
with antd.Flex(justify="center", align="center", gap="middle"):
antd.Typography.Title("Coder-Artifacts",
level=1,
elem_style=dict(fontSize=24))
with ms.AutoLoading():
with antd.Row(gutter=[32, 12],
elem_style=dict(marginTop=20),
align="stretch"):
# Left Column
with antd.Col(span=24, md=8):
with antd.Flex(vertical=True, gap="middle", wrap=True):
# Input
input = antd.Input.Textarea(
size="large",
allow_clear=True,
auto_size=dict(minRows=2, maxRows=6),
placeholder=
"Describe the web application you want to create",
elem_id="input-container")
# Input Notes
with antd.Flex(align="center",
justify="space-between"):
antd.Typography.Text(
"Note: The model supports multi-round dialogue.",
strong=True,
type="warning")
tour_btn = antd.Button("Usage Tour",
variant="filled",
color="default")
# Submit Button
submit_btn = antd.Button("Submit",
type="primary",
block=True,
size="large",
elem_id="submit-btn")
antd.Divider("Settings")
# Settings Area
with antd.Space(size="small",
wrap=True,
elem_id="settings-area"):
system_prompt_btn = antd.Button(
"⚙️ Set System Prompt", type="default")
history_btn = antd.Dropdown.Button(
"📜 History",
type="default",
elem_id="history-btn",
menu=dict(items=[{
"key": "clear",
"label": "Clear History",
"danger": True
}]))
antd.Divider("Examples")
# Examples
with antd.Flex(gap="small", wrap=True):
for example in EXAMPLES:
with antd.Card(
elem_style=dict(
flex="1 1 fit-content"),
hoverable=True) as example_card:
antd.Card.Meta(
title=example['title'],
description=example['description'])
example_card.click(
fn=GradioEvents.select_example(
example),
outputs=[input])
# Right Column
with antd.Col(span=24, md=16):
with antd.Card(title="Output",
elem_style=dict(height="100%"),
styles=dict(body=dict(height="100%")),
elem_id="output-container"):
# Output Container Extra
with ms.Slot("extra"):
with ms.Div(elem_id="output-container-extra"):
with antd.Button(
"Download HTML",
type="link",
href_target="_blank",
disabled=True,
) as download_btn:
with ms.Slot("icon"):
antd.Icon("DownloadOutlined")
download_content = gr.Text(visible=False)
view_code_btn = antd.Button(
"🧑💻 View Code", type="primary")
# Output Content
with antd.Tabs(
active_key="empty",
render_tab_bar="() => null") as state_tab:
with antd.Tabs.Item(key="empty"):
antd.Empty(
description=
"Enter your request to generate code",
elem_classes="output-empty")
with antd.Tabs.Item(key="loading"):
with antd.Spin(
tip="Generating code...",
size="large",
elem_classes="output-loading"):
# placeholder
ms.Div()
with antd.Tabs.Item(key="render"):
sandbox = gr.HTML(
elem_classes="output-html")
# Modals and Drawers
with antd.Modal(open=False,
title="System Prompt",
width="800px") as system_prompt_modal:
system_prompt_input = antd.Input.Textarea(
DEFAULT_SYSTEM_PROMPT,
size="large",
placeholder="Enter your system prompt here",
allow_clear=True,
auto_size=dict(minRows=4, maxRows=14))
with antd.Drawer(
open=False,
title="Output Code",
placement="right",
get_container=
"() => document.querySelector('.gradio-container')",
elem_id="code-artifacts-code-drawer",
styles=dict(
body=dict(display="flex",
flexDirection="column-reverse")),
width="750px") as output_code_drawer:
with ms.Div(elem_classes="output-code"):
with antd.Spin(spinning=False) as output_loading:
output = ms.Markdown()
with antd.Drawer(
open=False,
title="Chat History",
placement="left",
get_container=
"() => document.querySelector('.gradio-container')",
width="750px") as history_drawer:
history_output = gr.Chatbot(
show_label=False,
type="messages",
height='100%',
elem_classes="history_chatbot")
# Tour
with antd.Tour(open=False) as usage_tour:
antd.Tour.Step(
title="Step 1",
description=
"Describe the web application you want to create.",
get_target=
"() => document.querySelector('#input-container')")
antd.Tour.Step(
title="Step 2",
description="Click the submit button.",
get_target=
"() => document.querySelector('#submit-btn')")
antd.Tour.Step(
title="Step 3",
description="Wait for the result.",
get_target=
"() => document.querySelector('#output-container')"
)
antd.Tour.Step(
title="Step 4",
description=
"Download the generated HTML here or view the code.",
get_target=
"() => document.querySelector('#output-container-extra')"
)
antd.Tour.Step(
title="Additional Settings",
description=
"You can change the system prompt or chat history here.",
get_target=
"() => document.querySelector('#settings-area')")
# Event Handler
gr.on(fn=GradioEvents.close_modal,
triggers=[usage_tour.close, usage_tour.finish],
outputs=[usage_tour])
tour_btn.click(fn=GradioEvents.open_modal, outputs=[usage_tour])
system_prompt_btn.click(fn=GradioEvents.open_modal,
outputs=[system_prompt_modal])
system_prompt_modal.ok(GradioEvents.update_system_prompt,
inputs=[system_prompt_input, state],
outputs=[state]).then(fn=GradioEvents.close_modal,
outputs=[system_prompt_modal])
system_prompt_modal.cancel(GradioEvents.close_modal,
outputs=[system_prompt_modal]).then(
fn=GradioEvents.reset_system_prompt,
inputs=[state],
outputs=[system_prompt_input])
output_code_drawer.close(fn=GradioEvents.close_modal,
outputs=[output_code_drawer])
history_btn.menu_click(fn=GradioEvents.clear_history,
inputs=[state],
outputs=[state])
history_btn.click(fn=GradioEvents.open_modal,
outputs=[history_drawer
]).then(fn=GradioEvents.render_history,
inputs=[state],
outputs=[history_output])
history_drawer.close(fn=GradioEvents.close_modal, outputs=[history_drawer])
download_btn.click(fn=None,
inputs=[download_content],
js="""(content) => {
const blob = new Blob([content], { type: 'text/html' })
const url = URL.createObjectURL(blob)
const a = document.createElement('a')
a.href = url
a.download = 'output.html'
a.click()
}""")
view_code_btn.click(fn=GradioEvents.open_modal,
outputs=[output_code_drawer])
submit_btn.click(
fn=GradioEvents.open_modal,
outputs=[output_code_drawer],
).then(fn=GradioEvents.disable_btns([submit_btn, download_btn]),
outputs=[submit_btn, download_btn]).then(
fn=GradioEvents.generate_code,
inputs=[input, system_prompt_input, state],
outputs=[
output, state_tab, sandbox, download_content,
output_loading, state
]).then(fn=GradioEvents.enable_btns([submit_btn, download_btn]),
outputs=[submit_btn, download_btn
]).then(fn=GradioEvents.close_modal,
outputs=[output_code_drawer])
if __name__ == "__main__":
demo.queue().launch(ssr_mode=False)
|