Update app.py
Browse files
app.py
CHANGED
@@ -79,6 +79,70 @@ with gr.Blocks(css_paths="app.css") as demo:
|
|
79 |
"system": SystemPrompt,
|
80 |
})
|
81 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
82 |
with ms.Application() as app:
|
83 |
with antd.ConfigProvider():
|
84 |
with antd.Row(gutter=[32, 12]) as layout:
|
@@ -160,70 +224,6 @@ with gr.Blocks(css_paths="app.css") as demo:
|
|
160 |
|
161 |
clear_btn.click(clear_history, inputs=[], outputs=[history])
|
162 |
|
163 |
-
def generation_code(query: Optional[str], _setting: Dict[str, str], _history: Optional[History]):
|
164 |
-
if query is None:
|
165 |
-
query = ''
|
166 |
-
if _history is None:
|
167 |
-
_history = []
|
168 |
-
|
169 |
-
messages = history_to_messages(_history, _setting['system'])
|
170 |
-
system_message = messages[0]['content']
|
171 |
-
|
172 |
-
claude_messages = [
|
173 |
-
{"role": msg["role"] if msg["role"] != "system" else "user", "content": msg["content"]}
|
174 |
-
for msg in messages[1:] + [{'role': Role.USER, 'content': query}]
|
175 |
-
]
|
176 |
-
|
177 |
-
try:
|
178 |
-
# ์ค๊ฐ ์ํ ํ์
|
179 |
-
yield [
|
180 |
-
"Generating code...", # code_output
|
181 |
-
_history, # history
|
182 |
-
None, # sandbox
|
183 |
-
gr.update(active_key="loading"), # state_tab
|
184 |
-
gr.update(open=True) # code_drawer
|
185 |
-
]
|
186 |
-
|
187 |
-
# ์คํธ๋ฆฌ๋ฐ ์๋ต ์ฌ์ฉ
|
188 |
-
with client.messages.stream(
|
189 |
-
model="claude-3-5-sonnet-20241022",
|
190 |
-
max_tokens=7800,
|
191 |
-
system=system_message,
|
192 |
-
messages=claude_messages
|
193 |
-
) as stream:
|
194 |
-
collected_content = ""
|
195 |
-
for chunk in stream:
|
196 |
-
if chunk.type == "content_block_delta":
|
197 |
-
delta = chunk.delta.text
|
198 |
-
collected_content += delta
|
199 |
-
|
200 |
-
yield [
|
201 |
-
collected_content, # code_output
|
202 |
-
_history, # history
|
203 |
-
None, # sandbox (์์ง ์์ฑ๋์ง ์์์ผ๋ฏ๋ก None)
|
204 |
-
gr.update(active_key="loading"), # state_tab
|
205 |
-
gr.update(open=True) # code_drawer
|
206 |
-
]
|
207 |
-
|
208 |
-
# ์ต์ข
๊ฒฐ๊ณผ ๋ฐํ
|
209 |
-
_history = messages_to_history([
|
210 |
-
{'role': Role.SYSTEM, 'content': system_message}
|
211 |
-
] + claude_messages + [{
|
212 |
-
'role': Role.ASSISTANT,
|
213 |
-
'content': collected_content
|
214 |
-
}])
|
215 |
-
|
216 |
-
yield [
|
217 |
-
collected_content, # code_output
|
218 |
-
_history, # history
|
219 |
-
send_to_sandbox(remove_code_block(collected_content)), # sandbox
|
220 |
-
gr.update(active_key="render"), # state_tab
|
221 |
-
gr.update(open=True) # code_drawer
|
222 |
-
]
|
223 |
-
|
224 |
-
except Exception as e:
|
225 |
-
raise ValueError(f'Error calling Claude API: {str(e)}')
|
226 |
-
|
227 |
|
228 |
if __name__ == "__main__":
|
229 |
demo.queue(default_concurrency_limit=20).launch(ssr_mode=False)
|
|
|
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:
|
|
|
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)
|