openfree commited on
Commit
126ab5a
Β·
verified Β·
1 Parent(s): 08c822b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +65 -54
app.py CHANGED
@@ -175,12 +175,13 @@ class Demo:
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
  messages = history_to_messages(_history, _setting['system'])
185
  system_message = messages[0]['content']
186
 
@@ -198,33 +199,36 @@ class Demo:
198
  })
199
  openai_messages.append({"role": "user", "content": query})
200
 
 
 
 
 
 
 
 
 
 
 
 
 
201
  try:
202
- yield [
203
- "Generating code...",
204
- _history,
205
- None,
206
- gr.update(active_key="loading"),
207
- gr.update(open=True)
208
- ]
209
- await asyncio.sleep(0)
210
-
211
- collected_content = None
212
- try:
213
- async for content in try_claude_api(system_message, claude_messages):
214
- yield [
215
- content,
216
- _history,
217
- None,
218
- gr.update(active_key="loading"),
219
- gr.update(open=True)
220
- ]
221
- await asyncio.sleep(0)
222
  collected_content = content
223
-
224
- except Exception as claude_error:
225
- print(f"Falling back to OpenAI API due to Claude error: {str(claude_error)}")
226
-
 
 
 
 
 
 
 
 
 
227
  async for content in try_openai_api(openai_messages):
 
228
  yield [
229
  content,
230
  _history,
@@ -232,30 +236,37 @@ class Demo:
232
  gr.update(active_key="loading"),
233
  gr.update(open=True)
234
  ]
235
- await asyncio.sleep(0)
236
- collected_content = content
237
-
238
- if collected_content:
239
- _history = messages_to_history([
240
- {'role': Role.SYSTEM, 'content': system_message}
241
- ] + claude_messages + [{
242
- 'role': Role.ASSISTANT,
243
- 'content': collected_content
244
- }])
245
-
246
- yield [
247
- collected_content,
248
- _history,
249
- send_to_sandbox(remove_code_block(collected_content)),
250
- gr.update(active_key="render"),
251
- gr.update(open=True)
252
- ]
253
- else:
254
- raise ValueError("No content was generated from either API")
255
-
256
- except Exception as e:
257
- print(f"Error details: {str(e)}")
258
- raise ValueError(f'Error calling APIs: {str(e)}')
 
 
 
 
 
 
 
259
 
260
  def clear_history(self):
261
  return []
 
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
 
 
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,
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
+ # OpenAI API μ‹œλ„
229
+ try:
230
  async for content in try_openai_api(openai_messages):
231
+ collected_content = content
232
  yield [
233
  content,
234
  _history,
 
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 []