Update app.py
Browse files
app.py
CHANGED
@@ -11,6 +11,7 @@ import modelscope_studio.components.base as ms
|
|
11 |
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):
|
@@ -73,6 +74,7 @@ def demo_card_click(e: gr.EventData):
|
|
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,69 +82,7 @@ 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 |
-
|
84 |
-
query = ''
|
85 |
-
if _history is None:
|
86 |
-
_history = []
|
87 |
-
|
88 |
-
messages = history_to_messages(_history, _setting['system'])
|
89 |
-
system_message = messages[0]['content']
|
90 |
-
|
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...", # code_output
|
100 |
-
_history, # history
|
101 |
-
None, # sandbox
|
102 |
-
gr.update(active_key="loading"), # state_tab
|
103 |
-
gr.update(open=True) # code_drawer
|
104 |
-
]
|
105 |
-
|
106 |
-
# ์คํธ๋ฆฌ๋ฐ ์๋ต ์ฌ์ฉ
|
107 |
-
with client.messages.stream(
|
108 |
-
model="claude-3-5-sonnet-20241022",
|
109 |
-
max_tokens=7800,
|
110 |
-
system=system_message,
|
111 |
-
messages=claude_messages
|
112 |
-
) as stream:
|
113 |
-
collected_content = ""
|
114 |
-
for chunk in stream:
|
115 |
-
if chunk.type == "content_block_delta":
|
116 |
-
delta = chunk.delta.text
|
117 |
-
collected_content += delta
|
118 |
-
|
119 |
-
yield [
|
120 |
-
collected_content, # code_output
|
121 |
-
_history, # history
|
122 |
-
None, # sandbox (์์ง ์์ฑ๋์ง ์์์ผ๋ฏ๋ก None)
|
123 |
-
gr.update(active_key="loading"), # state_tab
|
124 |
-
gr.update(open=True) # code_drawer
|
125 |
-
]
|
126 |
-
|
127 |
-
# ์ต์ข
๊ฒฐ๊ณผ ๋ฐํ
|
128 |
-
_history = messages_to_history([
|
129 |
-
{'role': Role.SYSTEM, 'content': system_message}
|
130 |
-
] + claude_messages + [{
|
131 |
-
'role': Role.ASSISTANT,
|
132 |
-
'content': collected_content
|
133 |
-
}])
|
134 |
-
|
135 |
-
yield [
|
136 |
-
collected_content, # code_output
|
137 |
-
_history, # history
|
138 |
-
send_to_sandbox(remove_code_block(collected_content)), # sandbox
|
139 |
-
gr.update(active_key="render"), # state_tab
|
140 |
-
gr.update(open=True) # code_drawer
|
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():
|
@@ -157,10 +97,11 @@ with gr.Blocks(css_paths="app.css") as demo:
|
|
157 |
<h1 style="font-size: 20px;">AI ์ฝ๋ฉ ์ฝํ์ผ๋ฟ: MOUSE(WEB)</h2>
|
158 |
</div>
|
159 |
""")
|
|
|
160 |
input = antd.InputTextarea(
|
161 |
size="large",
|
162 |
allow_clear=True,
|
163 |
-
placeholder=
|
164 |
)
|
165 |
|
166 |
btn = antd.Button("send", type="primary", size="large")
|
@@ -190,22 +131,13 @@ 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
|
199 |
-
|
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])
|
|
|
11 |
import modelscope_studio.components.legacy as legacy
|
12 |
import modelscope_studio.components.antd as antd
|
13 |
from config import DEMO_LIST, SystemPrompt
|
14 |
+
import random # ์ถ๊ฐ
|
15 |
|
16 |
# ํ์ผ ์๋จ์ import ๋ฌธ ์๋์ ์ถ๊ฐ
|
17 |
def get_image_base64(image_path):
|
|
|
74 |
return DEMO_CACHE[index]
|
75 |
|
76 |
|
77 |
+
|
78 |
with gr.Blocks(css_paths="app.css") as demo:
|
79 |
history = gr.State([])
|
80 |
setting = gr.State({
|
|
|
82 |
})
|
83 |
|
84 |
def generation_code(query: Optional[str], _setting: Dict[str, str], _history: Optional[History]):
|
85 |
+
[... generation_code ํจ์ ๋ด์ฉ ๋์ผ ...]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
86 |
|
87 |
with ms.Application() as app:
|
88 |
with antd.ConfigProvider():
|
|
|
97 |
<h1 style="font-size: 20px;">AI ์ฝ๋ฉ ์ฝํ์ผ๋ฟ: MOUSE(WEB)</h2>
|
98 |
</div>
|
99 |
""")
|
100 |
+
# ๋๋ค ์์ ๋ฅผ placeholder๋ก ์ฌ์ฉ
|
101 |
input = antd.InputTextarea(
|
102 |
size="large",
|
103 |
allow_clear=True,
|
104 |
+
placeholder=random.choice(DEMO_LIST)['description']
|
105 |
)
|
106 |
|
107 |
btn = antd.Button("send", type="primary", size="large")
|
|
|
131 |
with antd.Tabs(active_key="empty", render_tab_bar="() => null") as state_tab:
|
132 |
with antd.Tabs.Item(key="empty"):
|
133 |
empty = antd.Empty(description="empty input", elem_classes="right_content")
|
134 |
+
with antd.Tabs.Item(key="loading"):
|
135 |
+
loading = antd.Spin(True, tip="coding...", size="large", elem_classes="right_content")
|
136 |
+
with antd.Tabs.Item(key="render"):
|
137 |
+
sandbox = gr.HTML(elem_classes="html_content")
|
138 |
+
|
139 |
+
# Examples ์น์
์ ๊ฑฐ
|
140 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
141 |
# ๋ฒํผ ์ด๋ฒคํธ ํธ๋ค๋ฌ
|
142 |
settingPromptBtn.click(lambda: gr.update(
|
143 |
open=True), inputs=[], outputs=[system_prompt_modal])
|