Update app.py
Browse files
app.py
CHANGED
@@ -189,8 +189,6 @@ async def try_openai_api(openai_messages):
|
|
189 |
print(f"OpenAI API error: {str(e)}")
|
190 |
raise e
|
191 |
|
192 |
-
|
193 |
-
|
194 |
class Demo:
|
195 |
def __init__(self):
|
196 |
init_db()
|
@@ -199,99 +197,96 @@ class Demo:
|
|
199 |
async def generation_code(self, query: Optional[str], _setting: Dict[str, str], _history: Optional[History]):
|
200 |
if not query or query.strip() == '':
|
201 |
query = random.choice(DEMO_LIST)['description']
|
202 |
-
|
203 |
-
|
204 |
-
|
205 |
-
|
206 |
-
|
207 |
-
|
208 |
-
|
209 |
-
|
210 |
-
|
211 |
-
|
212 |
-
|
213 |
-
|
214 |
-
for msg in messages[1:] + [{'role': Role.USER, 'content': query}]
|
215 |
-
if msg["content"].strip() != ''
|
216 |
-
]
|
217 |
-
|
218 |
-
# OpenAI 메시지 포맷
|
219 |
-
openai_messages = [{"role": "system", "content": system_message}]
|
220 |
-
for msg in messages[1:]:
|
221 |
-
openai_messages.append({
|
222 |
-
"role": msg["role"],
|
223 |
-
"content": msg["content"]
|
224 |
-
})
|
225 |
-
openai_messages.append({"role": "user", "content": query})
|
226 |
-
|
227 |
-
try:
|
228 |
-
# 먼저 코드 뷰어를 열기
|
229 |
-
yield [
|
230 |
-
"Generating code...",
|
231 |
-
_history,
|
232 |
-
None,
|
233 |
-
gr.update(active_key="loading"),
|
234 |
-
gr.update(open=True)
|
235 |
]
|
236 |
-
await asyncio.sleep(0) # UI 업데이트를 위한 양보
|
237 |
-
|
238 |
-
collected_content = None
|
239 |
-
# Claude API 시도
|
240 |
-
try:
|
241 |
-
async for content in try_claude_api(system_message, claude_messages):
|
242 |
-
yield [
|
243 |
-
content,
|
244 |
-
_history,
|
245 |
-
None,
|
246 |
-
gr.update(active_key="loading"),
|
247 |
-
gr.update(open=True)
|
248 |
-
]
|
249 |
-
await asyncio.sleep(0) # UI 업데이트를 위한 양보
|
250 |
-
collected_content = content
|
251 |
-
|
252 |
-
except Exception as claude_error:
|
253 |
-
print(f"Falling back to OpenAI API due to Claude error: {str(claude_error)}")
|
254 |
-
|
255 |
-
# OpenAI API로 폴백
|
256 |
-
async for content in try_openai_api(openai_messages):
|
257 |
-
yield [
|
258 |
-
content,
|
259 |
-
_history,
|
260 |
-
None,
|
261 |
-
gr.update(active_key="loading"),
|
262 |
-
gr.update(open=True)
|
263 |
-
]
|
264 |
-
await asyncio.sleep(0) # UI 업데이트를 위한 양보
|
265 |
-
collected_content = content
|
266 |
-
|
267 |
-
|
268 |
-
if collected_content:
|
269 |
-
# 채팅 내용 저장
|
270 |
-
save_chat(self.current_session, query, collected_content)
|
271 |
-
_history = messages_to_history([...])
|
272 |
|
273 |
-
|
274 |
-
|
275 |
-
|
276 |
-
|
277 |
-
|
278 |
-
|
|
|
|
|
|
|
|
|
|
|
279 |
yield [
|
280 |
-
|
281 |
_history,
|
282 |
-
|
283 |
-
gr.update(active_key="
|
284 |
gr.update(open=True)
|
285 |
]
|
286 |
-
|
287 |
-
|
288 |
-
|
289 |
-
|
290 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
291 |
except Exception as e:
|
292 |
print(f"Error details: {str(e)}")
|
293 |
raise ValueError(f'Error calling APIs: {str(e)}')
|
294 |
|
|
|
|
|
295 |
def clear_history(self):
|
296 |
self.current_session = create_session()
|
297 |
return []
|
|
|
189 |
print(f"OpenAI API error: {str(e)}")
|
190 |
raise e
|
191 |
|
|
|
|
|
192 |
class Demo:
|
193 |
def __init__(self):
|
194 |
init_db()
|
|
|
197 |
async def generation_code(self, query: Optional[str], _setting: Dict[str, str], _history: Optional[History]):
|
198 |
if not query or query.strip() == '':
|
199 |
query = random.choice(DEMO_LIST)['description']
|
200 |
+
|
201 |
+
if _history is None:
|
202 |
+
_history = []
|
203 |
+
|
204 |
+
messages = history_to_messages(_history, _setting['system'])
|
205 |
+
system_message = messages[0]['content']
|
206 |
+
|
207 |
+
# Claude 메시지 포맷
|
208 |
+
claude_messages = [
|
209 |
+
{"role": msg["role"] if msg["role"] != "system" else "user", "content": msg["content"]}
|
210 |
+
for msg in messages[1:] + [{'role': Role.USER, 'content': query}]
|
211 |
+
if msg["content"].strip() != ''
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
212 |
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
213 |
|
214 |
+
# OpenAI 메시지 포맷
|
215 |
+
openai_messages = [{"role": "system", "content": system_message}]
|
216 |
+
for msg in messages[1:]:
|
217 |
+
openai_messages.append({
|
218 |
+
"role": msg["role"],
|
219 |
+
"content": msg["content"]
|
220 |
+
})
|
221 |
+
openai_messages.append({"role": "user", "content": query})
|
222 |
+
|
223 |
+
try:
|
224 |
+
# 먼저 코드 뷰어를 열기
|
225 |
yield [
|
226 |
+
"Generating code...",
|
227 |
_history,
|
228 |
+
None,
|
229 |
+
gr.update(active_key="loading"),
|
230 |
gr.update(open=True)
|
231 |
]
|
232 |
+
await asyncio.sleep(0) # UI 업데이트를 위한 양보
|
233 |
+
|
234 |
+
collected_content = None
|
235 |
+
# Claude API 시도
|
236 |
+
try:
|
237 |
+
async for content in try_claude_api(system_message, claude_messages):
|
238 |
+
yield [
|
239 |
+
content,
|
240 |
+
_history,
|
241 |
+
None,
|
242 |
+
gr.update(active_key="loading"),
|
243 |
+
gr.update(open=True)
|
244 |
+
]
|
245 |
+
await asyncio.sleep(0)
|
246 |
+
collected_content = content
|
247 |
+
|
248 |
+
except Exception as claude_error:
|
249 |
+
print(f"Falling back to OpenAI API due to Claude error: {str(claude_error)}")
|
250 |
+
|
251 |
+
# OpenAI API로 폴백
|
252 |
+
async for content in try_openai_api(openai_messages):
|
253 |
+
yield [
|
254 |
+
content,
|
255 |
+
_history,
|
256 |
+
None,
|
257 |
+
gr.update(active_key="loading"),
|
258 |
+
gr.update(open=True)
|
259 |
+
]
|
260 |
+
await asyncio.sleep(0)
|
261 |
+
collected_content = content
|
262 |
+
|
263 |
+
if collected_content:
|
264 |
+
# 채팅 내용 저장
|
265 |
+
save_chat(self.current_session, query, collected_content)
|
266 |
+
|
267 |
+
_history = messages_to_history([
|
268 |
+
{'role': Role.SYSTEM, 'content': system_message}
|
269 |
+
] + claude_messages + [{
|
270 |
+
'role': Role.ASSISTANT,
|
271 |
+
'content': collected_content
|
272 |
+
}])
|
273 |
+
|
274 |
+
yield [
|
275 |
+
collected_content,
|
276 |
+
_history,
|
277 |
+
send_to_sandbox(remove_code_block(collected_content)),
|
278 |
+
gr.update(active_key="render"),
|
279 |
+
gr.update(open=True)
|
280 |
+
]
|
281 |
+
else:
|
282 |
+
raise ValueError("No content was generated from either API")
|
283 |
+
|
284 |
except Exception as e:
|
285 |
print(f"Error details: {str(e)}")
|
286 |
raise ValueError(f'Error calling APIs: {str(e)}')
|
287 |
|
288 |
+
|
289 |
+
|
290 |
def clear_history(self):
|
291 |
self.current_session = create_session()
|
292 |
return []
|