Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
Update app.py
Browse files
app.py
CHANGED
@@ -174,60 +174,45 @@ class Demo:
|
|
174 |
def __init__(self):
|
175 |
pass
|
176 |
|
177 |
-
async def generation_code(self, query: Optional[str], _setting: Dict[str, str], _history: Optional[History]):
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
-
if _history is None:
|
182 |
-
_history = []
|
183 |
-
|
184 |
-
try:
|
185 |
-
messages = history_to_messages(_history, _setting['system'])
|
186 |
-
system_message = messages[0]['content']
|
187 |
-
|
188 |
-
claude_messages = [
|
189 |
-
{"role": msg["role"] if msg["role"] != "system" else "user", "content": msg["content"]}
|
190 |
-
for msg in messages[1:] + [{'role': Role.USER, 'content': query}]
|
191 |
-
if msg["content"].strip() != ''
|
192 |
-
]
|
193 |
-
|
194 |
-
openai_messages = [{"role": "system", "content": system_message}]
|
195 |
-
for msg in messages[1:]:
|
196 |
-
openai_messages.append({
|
197 |
-
"role": msg["role"],
|
198 |
-
"content": msg["content"]
|
199 |
-
})
|
200 |
-
openai_messages.append({"role": "user", "content": query})
|
201 |
-
|
202 |
-
yield [
|
203 |
-
"Generating code...",
|
204 |
-
_history,
|
205 |
-
None,
|
206 |
-
gr.update(active_key="loading"),
|
207 |
-
gr.update(open=True)
|
208 |
-
]
|
209 |
|
210 |
-
|
211 |
-
|
212 |
|
213 |
-
# Claude API ์๋
|
214 |
try:
|
215 |
-
|
216 |
-
|
217 |
-
yield [
|
218 |
-
content,
|
219 |
-
_history,
|
220 |
-
None,
|
221 |
-
gr.update(active_key="loading"),
|
222 |
-
gr.update(open=True)
|
223 |
-
]
|
224 |
-
except Exception as claude_error:
|
225 |
-
print(f"Claude API error: {str(claude_error)}")
|
226 |
-
error_message = str(claude_error)
|
227 |
|
228 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
229 |
try:
|
230 |
-
async for content in
|
231 |
collected_content = content
|
232 |
yield [
|
233 |
content,
|
@@ -236,37 +221,52 @@ async def generation_code(self, query: Optional[str], _setting: Dict[str, str],
|
|
236 |
gr.update(active_key="loading"),
|
237 |
gr.update(open=True)
|
238 |
]
|
239 |
-
except Exception as
|
240 |
-
print(f"
|
241 |
-
error_message =
|
242 |
-
|
243 |
-
|
244 |
-
|
245 |
-
|
246 |
-
|
247 |
-
|
248 |
-
|
249 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
250 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
251 |
yield [
|
252 |
-
|
253 |
_history,
|
254 |
-
|
255 |
-
gr.update(active_key="
|
256 |
-
gr.update(open=
|
257 |
]
|
258 |
-
else:
|
259 |
-
raise ValueError(error_message or "No content was generated")
|
260 |
-
|
261 |
-
except Exception as e:
|
262 |
-
print(f"Error details: {str(e)}")
|
263 |
-
yield [
|
264 |
-
f"Error: {str(e)}",
|
265 |
-
_history,
|
266 |
-
None,
|
267 |
-
gr.update(active_key="empty"),
|
268 |
-
gr.update(open=False)
|
269 |
-
]
|
270 |
|
271 |
def clear_history(self):
|
272 |
return []
|
|
|
174 |
def __init__(self):
|
175 |
pass
|
176 |
|
177 |
+
async def generation_code(self, query: Optional[str], _setting: Dict[str, str], _history: Optional[History]):
|
178 |
+
if not query or query.strip() == '':
|
179 |
+
query = random.choice(DEMO_LIST)['description']
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
180 |
|
181 |
+
if _history is None:
|
182 |
+
_history = []
|
183 |
|
|
|
184 |
try:
|
185 |
+
messages = history_to_messages(_history, _setting['system'])
|
186 |
+
system_message = messages[0]['content']
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
187 |
|
188 |
+
claude_messages = [
|
189 |
+
{"role": msg["role"] if msg["role"] != "system" else "user", "content": msg["content"]}
|
190 |
+
for msg in messages[1:] + [{'role': Role.USER, 'content': query}]
|
191 |
+
if msg["content"].strip() != ''
|
192 |
+
]
|
193 |
+
|
194 |
+
openai_messages = [{"role": "system", "content": system_message}]
|
195 |
+
for msg in messages[1:]:
|
196 |
+
openai_messages.append({
|
197 |
+
"role": msg["role"],
|
198 |
+
"content": msg["content"]
|
199 |
+
})
|
200 |
+
openai_messages.append({"role": "user", "content": query})
|
201 |
+
|
202 |
+
yield [
|
203 |
+
"Generating code...",
|
204 |
+
_history,
|
205 |
+
None,
|
206 |
+
gr.update(active_key="loading"),
|
207 |
+
gr.update(open=True)
|
208 |
+
]
|
209 |
+
|
210 |
+
collected_content = None
|
211 |
+
error_message = None
|
212 |
+
|
213 |
+
# Claude API ์๋
|
214 |
try:
|
215 |
+
async for content in try_claude_api(system_message, claude_messages):
|
216 |
collected_content = content
|
217 |
yield [
|
218 |
content,
|
|
|
221 |
gr.update(active_key="loading"),
|
222 |
gr.update(open=True)
|
223 |
]
|
224 |
+
except Exception as claude_error:
|
225 |
+
print(f"Claude API error: {str(claude_error)}")
|
226 |
+
error_message = str(claude_error)
|
227 |
+
|
228 |
+
# OpenAI API ์๋
|
229 |
+
try:
|
230 |
+
async for content in try_openai_api(openai_messages):
|
231 |
+
collected_content = content
|
232 |
+
yield [
|
233 |
+
content,
|
234 |
+
_history,
|
235 |
+
None,
|
236 |
+
gr.update(active_key="loading"),
|
237 |
+
gr.update(open=True)
|
238 |
+
]
|
239 |
+
except Exception as openai_error:
|
240 |
+
print(f"OpenAI API error: {str(openai_error)}")
|
241 |
+
error_message = f"Both APIs failed: Claude - {str(claude_error)}, OpenAI - {str(openai_error)}"
|
242 |
|
243 |
+
if collected_content:
|
244 |
+
_history = messages_to_history([
|
245 |
+
{'role': Role.SYSTEM, 'content': system_message}
|
246 |
+
] + claude_messages + [{
|
247 |
+
'role': Role.ASSISTANT,
|
248 |
+
'content': collected_content
|
249 |
+
}])
|
250 |
+
|
251 |
+
yield [
|
252 |
+
collected_content,
|
253 |
+
_history,
|
254 |
+
send_to_sandbox(remove_code_block(collected_content)),
|
255 |
+
gr.update(active_key="render"),
|
256 |
+
gr.update(open=True)
|
257 |
+
]
|
258 |
+
else:
|
259 |
+
raise ValueError(error_message or "No content was generated")
|
260 |
+
|
261 |
+
except Exception as e:
|
262 |
+
print(f"Error details: {str(e)}")
|
263 |
yield [
|
264 |
+
f"Error: {str(e)}",
|
265 |
_history,
|
266 |
+
None,
|
267 |
+
gr.update(active_key="empty"),
|
268 |
+
gr.update(open=False)
|
269 |
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
270 |
|
271 |
def clear_history(self):
|
272 |
return []
|