Spaces:
Paused
Paused
Update app-backup1.py
Browse files- app-backup1.py +51 -61
app-backup1.py
CHANGED
|
@@ -1,5 +1,6 @@
|
|
| 1 |
import os
|
| 2 |
import re
|
|
|
|
| 3 |
from http import HTTPStatus
|
| 4 |
from typing import Dict, List, Optional, Tuple
|
| 5 |
import base64
|
|
@@ -12,7 +13,6 @@ import modelscope_studio.components.legacy as legacy
|
|
| 12 |
import modelscope_studio.components.antd as antd
|
| 13 |
from config import DEMO_LIST, SystemPrompt
|
| 14 |
|
| 15 |
-
# ํ์ผ ์๋จ์ import ๋ฌธ ์๋์ ์ถ๊ฐ
|
| 16 |
def get_image_base64(image_path):
|
| 17 |
with open(image_path, "rb") as image_file:
|
| 18 |
encoded_string = base64.b64encode(image_file.read()).decode()
|
|
@@ -62,17 +62,6 @@ def send_to_sandbox(code):
|
|
| 62 |
data_uri = f"data:text/html;charset=utf-8;base64,{encoded_html}"
|
| 63 |
return f"<iframe src=\"{data_uri}\" width=\"100%\" height=\"920px\"></iframe>"
|
| 64 |
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
DEMO_CACHE = {}
|
| 68 |
-
|
| 69 |
-
def demo_card_click(e: gr.EventData):
|
| 70 |
-
index = e._data['component']['index']
|
| 71 |
-
if index not in DEMO_CACHE:
|
| 72 |
-
DEMO_CACHE[index] = DEMO_LIST[index]['description']
|
| 73 |
-
return DEMO_CACHE[index]
|
| 74 |
-
|
| 75 |
-
|
| 76 |
with gr.Blocks(css_paths="app.css") as demo:
|
| 77 |
history = gr.State([])
|
| 78 |
setting = gr.State({
|
|
@@ -80,8 +69,9 @@ with gr.Blocks(css_paths="app.css") as demo:
|
|
| 80 |
})
|
| 81 |
|
| 82 |
def generation_code(query: Optional[str], _setting: Dict[str, str], _history: Optional[History]):
|
| 83 |
-
if query
|
| 84 |
-
query = ''
|
|
|
|
| 85 |
if _history is None:
|
| 86 |
_history = []
|
| 87 |
|
|
@@ -91,19 +81,18 @@ with gr.Blocks(css_paths="app.css") as demo:
|
|
| 91 |
claude_messages = [
|
| 92 |
{"role": msg["role"] if msg["role"] != "system" else "user", "content": msg["content"]}
|
| 93 |
for msg in messages[1:] + [{'role': Role.USER, 'content': query}]
|
|
|
|
| 94 |
]
|
| 95 |
|
| 96 |
try:
|
| 97 |
-
# ์ค๊ฐ ์ํ ํ์
|
| 98 |
yield [
|
| 99 |
-
"Generating code...",
|
| 100 |
-
_history,
|
| 101 |
-
None,
|
| 102 |
-
gr.update(active_key="loading"),
|
| 103 |
-
gr.update(open=True)
|
| 104 |
]
|
| 105 |
-
|
| 106 |
-
# ์คํธ๋ฆฌ๋ฐ ์๋ต ์ฌ์ฉ
|
| 107 |
with client.messages.stream(
|
| 108 |
model="claude-3-5-sonnet-20241022",
|
| 109 |
max_tokens=7800,
|
|
@@ -117,38 +106,39 @@ with gr.Blocks(css_paths="app.css") as demo:
|
|
| 117 |
collected_content += delta
|
| 118 |
|
| 119 |
yield [
|
| 120 |
-
collected_content,
|
| 121 |
-
_history,
|
| 122 |
-
None,
|
| 123 |
-
gr.update(active_key="loading"),
|
| 124 |
-
gr.update(open=True)
|
| 125 |
]
|
| 126 |
-
|
| 127 |
-
# ์ต์ข
๊ฒฐ๊ณผ ๋ฐํ
|
| 128 |
_history = messages_to_history([
|
| 129 |
{'role': Role.SYSTEM, 'content': system_message}
|
| 130 |
] + claude_messages + [{
|
| 131 |
'role': Role.ASSISTANT,
|
| 132 |
-
|
| 133 |
}])
|
| 134 |
-
|
| 135 |
yield [
|
| 136 |
-
collected_content,
|
| 137 |
-
_history,
|
| 138 |
-
send_to_sandbox(remove_code_block(collected_content)),
|
| 139 |
-
gr.update(active_key="render"),
|
| 140 |
-
gr.update(open=True)
|
| 141 |
]
|
| 142 |
-
|
| 143 |
except Exception as e:
|
|
|
|
| 144 |
raise ValueError(f'Error calling Claude API: {str(e)}')
|
| 145 |
-
|
|
|
|
| 146 |
|
| 147 |
with ms.Application() as app:
|
| 148 |
with antd.ConfigProvider():
|
| 149 |
-
# ๋ฉ์ธ ์ปจํ
์ธ ๋ฅผ ์ํ Row
|
| 150 |
with antd.Row(gutter=[32, 12]) as layout:
|
| 151 |
-
|
|
|
|
| 152 |
with antd.Col(span=24, md=8):
|
| 153 |
with antd.Flex(vertical=True, gap="middle", wrap=True):
|
| 154 |
header = gr.HTML(f"""
|
|
@@ -160,13 +150,27 @@ with gr.Blocks(css_paths="app.css") as demo:
|
|
| 160 |
input = antd.InputTextarea(
|
| 161 |
size="large",
|
| 162 |
allow_clear=True,
|
| 163 |
-
placeholder=
|
| 164 |
)
|
| 165 |
|
| 166 |
btn = antd.Button("send", type="primary", size="large")
|
| 167 |
clear_btn = antd.Button("clear history", type="default", size="large")
|
| 168 |
|
| 169 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 170 |
with antd.Modal(open=False, title="set system Prompt", width="800px") as system_prompt_modal:
|
| 171 |
systemPromptInput = antd.InputTextarea(
|
| 172 |
SystemPrompt, auto_size=True)
|
|
@@ -177,7 +181,6 @@ with gr.Blocks(css_paths="app.css") as demo:
|
|
| 177 |
with antd.Drawer(open=False, title="history", placement="left", width="900px") as history_drawer:
|
| 178 |
history_output = legacy.Chatbot(show_label=False, flushing=False, height=960, elem_classes="history_chatbot")
|
| 179 |
|
| 180 |
-
# ์ฐ์ธก ํจ๋
|
| 181 |
with antd.Col(span=24, md=16):
|
| 182 |
with ms.Div(elem_classes="right_panel"):
|
| 183 |
with antd.Flex(gap="small", elem_classes="setting-buttons"):
|
|
@@ -190,23 +193,11 @@ with gr.Blocks(css_paths="app.css") as demo:
|
|
| 190 |
with antd.Tabs(active_key="empty", render_tab_bar="() => null") as state_tab:
|
| 191 |
with antd.Tabs.Item(key="empty"):
|
| 192 |
empty = antd.Empty(description="empty input", elem_classes="right_content")
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
# Examples ์น์
์ ๋ณ๋์ Row๋ก ์ถ๊ฐ (์ ์ฒด ํ๋ฉด ํ๋จ)
|
| 199 |
-
with antd.Row(gutter=[0, 24], elem_classes="examples-section"):
|
| 200 |
-
with antd.Col(span=24):
|
| 201 |
-
antd.Divider("examples")
|
| 202 |
-
with antd.Row(gutter=[16, 16]):
|
| 203 |
-
with ms.Each(DEMO_LIST):
|
| 204 |
-
with antd.Col(span=8): # ํ ์ค์ 3๊ฐ์ฉ (24/3 = 8)
|
| 205 |
-
with antd.Card(hoverable=True, as_item="card") as demoCard:
|
| 206 |
-
antd.CardMeta()
|
| 207 |
-
demoCard.click(demo_card_click, outputs=[input])
|
| 208 |
-
|
| 209 |
-
# ๋ฒํผ ์ด๋ฒคํธ ํธ๋ค๋ฌ
|
| 210 |
settingPromptBtn.click(lambda: gr.update(
|
| 211 |
open=True), inputs=[], outputs=[system_prompt_modal])
|
| 212 |
system_prompt_modal.ok(lambda input: ({"system": input}, gr.update(
|
|
@@ -231,6 +222,5 @@ with gr.Blocks(css_paths="app.css") as demo:
|
|
| 231 |
|
| 232 |
clear_btn.click(clear_history, inputs=[], outputs=[history])
|
| 233 |
|
| 234 |
-
|
| 235 |
if __name__ == "__main__":
|
| 236 |
demo.queue(default_concurrency_limit=20).launch(ssr_mode=False)
|
|
|
|
| 1 |
import os
|
| 2 |
import re
|
| 3 |
+
import random
|
| 4 |
from http import HTTPStatus
|
| 5 |
from typing import Dict, List, Optional, Tuple
|
| 6 |
import base64
|
|
|
|
| 13 |
import modelscope_studio.components.antd as antd
|
| 14 |
from config import DEMO_LIST, SystemPrompt
|
| 15 |
|
|
|
|
| 16 |
def get_image_base64(image_path):
|
| 17 |
with open(image_path, "rb") as image_file:
|
| 18 |
encoded_string = base64.b64encode(image_file.read()).decode()
|
|
|
|
| 62 |
data_uri = f"data:text/html;charset=utf-8;base64,{encoded_html}"
|
| 63 |
return f"<iframe src=\"{data_uri}\" width=\"100%\" height=\"920px\"></iframe>"
|
| 64 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
with gr.Blocks(css_paths="app.css") as demo:
|
| 66 |
history = gr.State([])
|
| 67 |
setting = gr.State({
|
|
|
|
| 69 |
})
|
| 70 |
|
| 71 |
def generation_code(query: Optional[str], _setting: Dict[str, str], _history: Optional[History]):
|
| 72 |
+
if not query or query.strip() == '':
|
| 73 |
+
query = random.choice(DEMO_LIST)['description'] # ๋น ์ฟผ๋ฆฌ์ผ ๊ฒฝ์ฐ ๋๋ค ์์ ์ฌ์ฉ
|
| 74 |
+
|
| 75 |
if _history is None:
|
| 76 |
_history = []
|
| 77 |
|
|
|
|
| 81 |
claude_messages = [
|
| 82 |
{"role": msg["role"] if msg["role"] != "system" else "user", "content": msg["content"]}
|
| 83 |
for msg in messages[1:] + [{'role': Role.USER, 'content': query}]
|
| 84 |
+
if msg["content"].strip() != '' # ๋น ๋ฉ์์ง ํํฐ๋ง
|
| 85 |
]
|
| 86 |
|
| 87 |
try:
|
|
|
|
| 88 |
yield [
|
| 89 |
+
"Generating code...",
|
| 90 |
+
_history,
|
| 91 |
+
None,
|
| 92 |
+
gr.update(active_key="loading"),
|
| 93 |
+
gr.update(open=True)
|
| 94 |
]
|
| 95 |
+
|
|
|
|
| 96 |
with client.messages.stream(
|
| 97 |
model="claude-3-5-sonnet-20241022",
|
| 98 |
max_tokens=7800,
|
|
|
|
| 106 |
collected_content += delta
|
| 107 |
|
| 108 |
yield [
|
| 109 |
+
collected_content,
|
| 110 |
+
_history,
|
| 111 |
+
None,
|
| 112 |
+
gr.update(active_key="loading"),
|
| 113 |
+
gr.update(open=True)
|
| 114 |
]
|
| 115 |
+
|
|
|
|
| 116 |
_history = messages_to_history([
|
| 117 |
{'role': Role.SYSTEM, 'content': system_message}
|
| 118 |
] + claude_messages + [{
|
| 119 |
'role': Role.ASSISTANT,
|
| 120 |
+
'content': collected_content
|
| 121 |
}])
|
| 122 |
+
|
| 123 |
yield [
|
| 124 |
+
collected_content,
|
| 125 |
+
_history,
|
| 126 |
+
send_to_sandbox(remove_code_block(collected_content)),
|
| 127 |
+
gr.update(active_key="render"),
|
| 128 |
+
gr.update(open=True)
|
| 129 |
]
|
| 130 |
+
|
| 131 |
except Exception as e:
|
| 132 |
+
print(f"Error details: {str(e)}") # ๋๋ฒ๊น
์ ์ํ ์๋ฌ ์ถ๋ ฅ
|
| 133 |
raise ValueError(f'Error calling Claude API: {str(e)}')
|
| 134 |
+
|
| 135 |
+
|
| 136 |
|
| 137 |
with ms.Application() as app:
|
| 138 |
with antd.ConfigProvider():
|
|
|
|
| 139 |
with antd.Row(gutter=[32, 12]) as layout:
|
| 140 |
+
|
| 141 |
+
# ์ข์ธก ํจ๋ ๋ถ๋ถ ์์
|
| 142 |
with antd.Col(span=24, md=8):
|
| 143 |
with antd.Flex(vertical=True, gap="middle", wrap=True):
|
| 144 |
header = gr.HTML(f"""
|
|
|
|
| 150 |
input = antd.InputTextarea(
|
| 151 |
size="large",
|
| 152 |
allow_clear=True,
|
| 153 |
+
placeholder=random.choice(DEMO_LIST)['description']
|
| 154 |
)
|
| 155 |
|
| 156 |
btn = antd.Button("send", type="primary", size="large")
|
| 157 |
clear_btn = antd.Button("clear history", type="default", size="large")
|
| 158 |
|
| 159 |
+
# Examples ๋ฒํผ ์ถ๊ฐ
|
| 160 |
+
antd.Divider("Try this example")
|
| 161 |
+
example_btn = antd.Button(
|
| 162 |
+
"๋ค์ด๋๋ฏน ์ฐจํธ ๋์ฌ๋ณด๋",
|
| 163 |
+
type="default",
|
| 164 |
+
block=True, # ์ ์ฒด ๋๋น ์ฌ์ฉ
|
| 165 |
+
size="large"
|
| 166 |
+
)
|
| 167 |
+
example_btn.click(
|
| 168 |
+
fn=lambda: "Create an interactive dashboard with Chart.js showing different types of charts (line, bar, pie) with smooth animations. Include buttons to switch between different data views.",
|
| 169 |
+
inputs=[],
|
| 170 |
+
outputs=[input]
|
| 171 |
+
)
|
| 172 |
+
|
| 173 |
+
|
| 174 |
with antd.Modal(open=False, title="set system Prompt", width="800px") as system_prompt_modal:
|
| 175 |
systemPromptInput = antd.InputTextarea(
|
| 176 |
SystemPrompt, auto_size=True)
|
|
|
|
| 181 |
with antd.Drawer(open=False, title="history", placement="left", width="900px") as history_drawer:
|
| 182 |
history_output = legacy.Chatbot(show_label=False, flushing=False, height=960, elem_classes="history_chatbot")
|
| 183 |
|
|
|
|
| 184 |
with antd.Col(span=24, md=16):
|
| 185 |
with ms.Div(elem_classes="right_panel"):
|
| 186 |
with antd.Flex(gap="small", elem_classes="setting-buttons"):
|
|
|
|
| 193 |
with antd.Tabs(active_key="empty", render_tab_bar="() => null") as state_tab:
|
| 194 |
with antd.Tabs.Item(key="empty"):
|
| 195 |
empty = antd.Empty(description="empty input", elem_classes="right_content")
|
| 196 |
+
with antd.Tabs.Item(key="loading"):
|
| 197 |
+
loading = antd.Spin(True, tip="coding...", size="large", elem_classes="right_content")
|
| 198 |
+
with antd.Tabs.Item(key="render"):
|
| 199 |
+
sandbox = gr.HTML(elem_classes="html_content")
|
| 200 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 201 |
settingPromptBtn.click(lambda: gr.update(
|
| 202 |
open=True), inputs=[], outputs=[system_prompt_modal])
|
| 203 |
system_prompt_modal.ok(lambda input: ({"system": input}, gr.update(
|
|
|
|
| 222 |
|
| 223 |
clear_btn.click(clear_history, inputs=[], outputs=[history])
|
| 224 |
|
|
|
|
| 225 |
if __name__ == "__main__":
|
| 226 |
demo.queue(default_concurrency_limit=20).launch(ssr_mode=False)
|