Update app-backup1.py
Browse files- app-backup1.py +89 -89
app-backup1.py
CHANGED
@@ -73,14 +73,76 @@ def demo_card_click(e: gr.EventData):
|
|
73 |
return DEMO_CACHE[index]
|
74 |
|
75 |
|
76 |
-
|
77 |
-
|
78 |
with gr.Blocks(css_paths="app.css") as demo:
|
79 |
history = gr.State([])
|
80 |
setting = gr.State({
|
81 |
"system": SystemPrompt,
|
82 |
})
|
83 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
84 |
with ms.Application() as app:
|
85 |
with antd.ConfigProvider():
|
86 |
with antd.Row(gutter=[32, 12]) as layout:
|
@@ -107,43 +169,27 @@ with gr.Blocks(css_paths="app.css") as demo:
|
|
107 |
with antd.Card(hoverable=True, as_item="card") as demoCard:
|
108 |
antd.CardMeta()
|
109 |
demoCard.click(demo_card_click, outputs=[input])
|
110 |
-
|
111 |
-
antd.Divider("setting")
|
112 |
-
|
113 |
-
with antd.Flex(gap="small", wrap=True):
|
114 |
-
settingPromptBtn = antd.Button(
|
115 |
-
"โ๏ธ set system Prompt", type="default")
|
116 |
-
codeBtn = antd.Button("๐งโ๐ป view code", type="default")
|
117 |
-
historyBtn = antd.Button("๐ history", type="default")
|
118 |
|
|
|
119 |
with antd.Modal(open=False, title="set system Prompt", width="800px") as system_prompt_modal:
|
120 |
systemPromptInput = antd.InputTextarea(
|
121 |
SystemPrompt, auto_size=True)
|
122 |
|
123 |
-
settingPromptBtn.click(lambda: gr.update(
|
124 |
-
open=True), inputs=[], outputs=[system_prompt_modal])
|
125 |
-
system_prompt_modal.ok(lambda input: ({"system": input}, gr.update(
|
126 |
-
open=False)), inputs=[systemPromptInput], outputs=[setting, system_prompt_modal])
|
127 |
-
system_prompt_modal.cancel(lambda: gr.update(
|
128 |
-
open=False), outputs=[system_prompt_modal])
|
129 |
-
|
130 |
with antd.Drawer(open=False, title="code", placement="left", width="750px") as code_drawer:
|
131 |
code_output = legacy.Markdown()
|
132 |
|
133 |
-
codeBtn.click(lambda: gr.update(open=True),
|
134 |
-
inputs=[], outputs=[code_drawer])
|
135 |
-
code_drawer.close(lambda: gr.update(
|
136 |
-
open=False), inputs=[], outputs=[code_drawer])
|
137 |
-
|
138 |
with antd.Drawer(open=False, title="history", placement="left", width="900px") as history_drawer:
|
139 |
history_output = legacy.Chatbot(show_label=False, flushing=False, height=960, elem_classes="history_chatbot")
|
140 |
|
141 |
-
historyBtn.click(history_render, inputs=[history], outputs=[history_drawer, history_output])
|
142 |
-
history_drawer.close(lambda: gr.update(
|
143 |
-
open=False), inputs=[], outputs=[history_drawer])
|
144 |
-
|
145 |
with antd.Col(span=24, md=16):
|
146 |
with ms.Div(elem_classes="right_panel"):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
147 |
gr.HTML('<div class="render_header"><span class="header_btn"></span><span class="header_btn"></span><span class="header_btn"></span></div>')
|
148 |
with antd.Tabs(active_key="empty", render_tab_bar="() => null") as state_tab:
|
149 |
with antd.Tabs.Item(key="empty"):
|
@@ -153,69 +199,22 @@ with gr.Blocks(css_paths="app.css") as demo:
|
|
153 |
with antd.Tabs.Item(key="render"):
|
154 |
sandbox = gr.HTML(elem_classes="html_content")
|
155 |
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
yield [
|
173 |
-
"Generating code...", # code_output
|
174 |
-
_history, # history
|
175 |
-
None, # sandbox
|
176 |
-
gr.update(active_key="loading"), # state_tab
|
177 |
-
gr.update(open=True) # code_drawer
|
178 |
-
]
|
179 |
-
|
180 |
-
# ์คํธ๋ฆฌ๋ฐ ์๋ต ์ฌ์ฉ
|
181 |
-
with client.messages.stream(
|
182 |
-
model="claude-3-5-sonnet-20241022",
|
183 |
-
max_tokens=7800,
|
184 |
-
system=system_message,
|
185 |
-
messages=claude_messages
|
186 |
-
) as stream:
|
187 |
-
collected_content = ""
|
188 |
-
for chunk in stream:
|
189 |
-
if chunk.type == "content_block_delta":
|
190 |
-
delta = chunk.delta.text
|
191 |
-
collected_content += delta
|
192 |
-
|
193 |
-
yield [
|
194 |
-
collected_content, # code_output
|
195 |
-
_history, # history
|
196 |
-
None, # sandbox (์์ง ์์ฑ๋์ง ์์์ผ๋ฏ๋ก None)
|
197 |
-
gr.update(active_key="loading"), # state_tab
|
198 |
-
gr.update(open=True) # code_drawer
|
199 |
-
]
|
200 |
-
|
201 |
-
# ์ต์ข
๊ฒฐ๊ณผ ๋ฐํ
|
202 |
-
_history = messages_to_history([
|
203 |
-
{'role': Role.SYSTEM, 'content': system_message}
|
204 |
-
] + claude_messages + [{
|
205 |
-
'role': Role.ASSISTANT,
|
206 |
-
'content': collected_content
|
207 |
-
}])
|
208 |
-
|
209 |
-
yield [
|
210 |
-
collected_content, # code_output
|
211 |
-
_history, # history
|
212 |
-
send_to_sandbox(remove_code_block(collected_content)), # sandbox
|
213 |
-
gr.update(active_key="render"), # state_tab
|
214 |
-
gr.update(open=True) # code_drawer
|
215 |
-
]
|
216 |
-
|
217 |
-
except Exception as e:
|
218 |
-
raise ValueError(f'Error calling Claude API: {str(e)}')
|
219 |
|
220 |
btn.click(
|
221 |
generation_code,
|
@@ -225,5 +224,6 @@ with gr.Blocks(css_paths="app.css") as demo:
|
|
225 |
|
226 |
clear_btn.click(clear_history, inputs=[], outputs=[history])
|
227 |
|
|
|
228 |
if __name__ == "__main__":
|
229 |
demo.queue(default_concurrency_limit=20).launch(ssr_mode=False)
|
|
|
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({
|
79 |
"system": SystemPrompt,
|
80 |
})
|
81 |
|
82 |
+
def generation_code(query: Optional[str], _setting: Dict[str, str], _history: Optional[History]):
|
83 |
+
if query is None:
|
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 |
with ms.Application() as app:
|
147 |
with antd.ConfigProvider():
|
148 |
with antd.Row(gutter=[32, 12]) as layout:
|
|
|
169 |
with antd.Card(hoverable=True, as_item="card") as demoCard:
|
170 |
antd.CardMeta()
|
171 |
demoCard.click(demo_card_click, outputs=[input])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
172 |
|
173 |
+
# Modal๊ณผ Drawer ์ปดํฌ๋ํธ๋ค
|
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)
|
177 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
178 |
with antd.Drawer(open=False, title="code", placement="left", width="750px") as code_drawer:
|
179 |
code_output = legacy.Markdown()
|
180 |
|
|
|
|
|
|
|
|
|
|
|
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 |
+
# ์ฐ์ธก ์๋จ์ setting ๋ฒํผ๋ค ์ถ๊ฐ
|
187 |
+
with antd.Flex(gap="small", elem_classes="setting-buttons"):
|
188 |
+
settingPromptBtn = antd.Button(
|
189 |
+
"โ๏ธ set system Prompt", type="default")
|
190 |
+
codeBtn = antd.Button("๐งโ๐ป view code", type="default")
|
191 |
+
historyBtn = antd.Button("๐ history", type="default")
|
192 |
+
|
193 |
gr.HTML('<div class="render_header"><span class="header_btn"></span><span class="header_btn"></span><span class="header_btn"></span></div>')
|
194 |
with antd.Tabs(active_key="empty", render_tab_bar="() => null") as state_tab:
|
195 |
with antd.Tabs.Item(key="empty"):
|
|
|
199 |
with antd.Tabs.Item(key="render"):
|
200 |
sandbox = gr.HTML(elem_classes="html_content")
|
201 |
|
202 |
+
# ๋ฒํผ ์ด๋ฒคํธ ํธ๋ค๋ฌ
|
203 |
+
settingPromptBtn.click(lambda: gr.update(
|
204 |
+
open=True), inputs=[], outputs=[system_prompt_modal])
|
205 |
+
system_prompt_modal.ok(lambda input: ({"system": input}, gr.update(
|
206 |
+
open=False)), inputs=[systemPromptInput], outputs=[setting, system_prompt_modal])
|
207 |
+
system_prompt_modal.cancel(lambda: gr.update(
|
208 |
+
open=False), outputs=[system_prompt_modal])
|
209 |
+
|
210 |
+
codeBtn.click(lambda: gr.update(open=True),
|
211 |
+
inputs=[], outputs=[code_drawer])
|
212 |
+
code_drawer.close(lambda: gr.update(
|
213 |
+
open=False), inputs=[], outputs=[code_drawer])
|
214 |
+
|
215 |
+
historyBtn.click(history_render, inputs=[history], outputs=[history_drawer, history_output])
|
216 |
+
history_drawer.close(lambda: gr.update(
|
217 |
+
open=False), inputs=[], outputs=[history_drawer])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
218 |
|
219 |
btn.click(
|
220 |
generation_code,
|
|
|
224 |
|
225 |
clear_btn.click(clear_history, inputs=[], outputs=[history])
|
226 |
|
227 |
+
|
228 |
if __name__ == "__main__":
|
229 |
demo.queue(default_concurrency_limit=20).launch(ssr_mode=False)
|