Update app-backup1.py
Browse files- app-backup1.py +148 -80
app-backup1.py
CHANGED
@@ -175,89 +175,157 @@ async def try_openai_api(openai_messages):
|
|
175 |
class Demo:
|
176 |
def __init__(self):
|
177 |
pass
|
178 |
-
|
179 |
-
async def generation_code(self, query: Optional[str], _setting: Dict[str, str], _history: Optional[History]):
|
180 |
-
if not query or query.strip() == '':
|
181 |
-
query = random.choice(DEMO_LIST)['description']
|
182 |
-
|
183 |
-
if _history is None:
|
184 |
-
_history = []
|
185 |
-
|
186 |
-
messages = history_to_messages(_history, _setting['system'])
|
187 |
-
system_message = messages[0]['content']
|
188 |
|
189 |
-
|
190 |
-
|
191 |
-
|
192 |
-
|
193 |
-
|
194 |
-
|
195 |
-
|
196 |
-
|
197 |
-
|
198 |
-
|
199 |
-
|
200 |
-
|
201 |
-
|
202 |
-
|
203 |
-
|
204 |
-
|
205 |
-
|
206 |
-
|
207 |
-
|
208 |
-
|
209 |
-
|
210 |
-
|
211 |
-
|
212 |
-
|
213 |
-
|
214 |
-
|
215 |
-
|
216 |
-
yield [
|
217 |
-
content,
|
218 |
-
_history,
|
219 |
-
None,
|
220 |
-
gr.update(active_key="loading"),
|
221 |
-
gr.update(open=True)
|
222 |
-
]
|
223 |
-
await asyncio.sleep(0)
|
224 |
-
collected_content = content
|
225 |
-
|
226 |
-
except Exception as claude_error:
|
227 |
-
print(f"Falling back to OpenAI API due to Claude error: {str(claude_error)}")
|
228 |
-
|
229 |
-
async for content in try_openai_api(openai_messages):
|
230 |
-
yield [
|
231 |
-
content,
|
232 |
-
_history,
|
233 |
-
None,
|
234 |
-
gr.update(active_key="loading"),
|
235 |
-
gr.update(open=True)
|
236 |
-
]
|
237 |
-
await asyncio.sleep(0)
|
238 |
-
collected_content = content
|
239 |
-
|
240 |
-
if collected_content:
|
241 |
-
_history = messages_to_history([
|
242 |
-
{'role': Role.SYSTEM, 'content': system_message}
|
243 |
-
] + claude_messages + [{
|
244 |
-
'role': Role.ASSISTANT,
|
245 |
-
'content': collected_content
|
246 |
-
}])
|
247 |
-
|
248 |
-
yield [
|
249 |
-
collected_content,
|
250 |
_history,
|
251 |
-
|
252 |
-
gr.update(active_key="
|
253 |
gr.update(open=True)
|
254 |
-
|
255 |
-
|
256 |
-
|
257 |
-
|
258 |
-
|
259 |
-
|
260 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
261 |
|
262 |
def clear_history(self):
|
263 |
return []
|
|
|
175 |
class Demo:
|
176 |
def __init__(self):
|
177 |
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
178 |
|
179 |
+
async def generation_code(self, query: Optional[str], _setting: Dict[str, str], _history: Optional[History]):
|
180 |
+
if not query or query.strip() == '':
|
181 |
+
query = random.choice(DEMO_LIST)['description']
|
182 |
+
|
183 |
+
if _history is None:
|
184 |
+
_history = []
|
185 |
+
|
186 |
+
messages = history_to_messages(_history, _setting['system'])
|
187 |
+
system_message = messages[0]['content']
|
188 |
+
|
189 |
+
claude_messages = [
|
190 |
+
{"role": msg["role"] if msg["role"] != "system" else "user", "content": msg["content"]}
|
191 |
+
for msg in messages[1:] + [{'role': Role.USER, 'content': query}]
|
192 |
+
if msg["content"].strip() != ''
|
193 |
+
]
|
194 |
+
|
195 |
+
openai_messages = [{"role": "system", "content": system_message}]
|
196 |
+
for msg in messages[1:]:
|
197 |
+
openai_messages.append({
|
198 |
+
"role": msg["role"],
|
199 |
+
"content": msg["content"]
|
200 |
+
})
|
201 |
+
openai_messages.append({"role": "user", "content": query})
|
202 |
+
|
203 |
+
try:
|
204 |
+
yield [
|
205 |
+
"Generating code...",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
206 |
_history,
|
207 |
+
None,
|
208 |
+
gr.update(active_key="loading"),
|
209 |
gr.update(open=True)
|
210 |
+
]
|
211 |
+
await asyncio.sleep(0)
|
212 |
+
|
213 |
+
collected_content = None
|
214 |
+
try:
|
215 |
+
async for content in try_claude_api(system_message, claude_messages):
|
216 |
+
code = content
|
217 |
+
analysis = analyze_code(code)
|
218 |
+
yield [
|
219 |
+
code,
|
220 |
+
_history,
|
221 |
+
analysis,
|
222 |
+
gr.update(active_key="loading"),
|
223 |
+
gr.update(open=True)
|
224 |
+
]
|
225 |
+
await asyncio.sleep(0)
|
226 |
+
collected_content = code
|
227 |
+
|
228 |
+
except Exception as claude_error:
|
229 |
+
print(f"Falling back to OpenAI API due to Claude error: {str(claude_error)}")
|
230 |
+
|
231 |
+
async for content in try_openai_api(openai_messages):
|
232 |
+
code = content
|
233 |
+
analysis = analyze_code(code)
|
234 |
+
yield [
|
235 |
+
code,
|
236 |
+
_history,
|
237 |
+
analysis,
|
238 |
+
gr.update(active_key="loading"),
|
239 |
+
gr.update(open=True)
|
240 |
+
]
|
241 |
+
await asyncio.sleep(0)
|
242 |
+
collected_content = code
|
243 |
+
|
244 |
+
if collected_content:
|
245 |
+
_history = messages_to_history([
|
246 |
+
{'role': Role.SYSTEM, 'content': system_message}
|
247 |
+
] + claude_messages + [{
|
248 |
+
'role': Role.ASSISTANT,
|
249 |
+
'content': collected_content
|
250 |
+
}])
|
251 |
+
|
252 |
+
yield [
|
253 |
+
collected_content,
|
254 |
+
_history,
|
255 |
+
analyze_code(collected_content),
|
256 |
+
gr.update(active_key="render"),
|
257 |
+
gr.update(open=True)
|
258 |
+
]
|
259 |
+
else:
|
260 |
+
raise ValueError("No content was generated from either API")
|
261 |
+
|
262 |
+
except Exception as e:
|
263 |
+
print(f"Error details: {str(e)}")
|
264 |
+
raise ValueError(f'Error calling APIs: {str(e)}')
|
265 |
+
|
266 |
+
def analyze_code(code: str) -> str:
|
267 |
+
"""์ฝ๋ ๋ถ์ ๊ฒฐ๊ณผ๋ฅผ Markdown ํ์์ผ๋ก ๋ฐํ"""
|
268 |
+
analysis = []
|
269 |
+
|
270 |
+
# 1. ์ฌ์ฉ๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ๋ถ์
|
271 |
+
imports = []
|
272 |
+
for line in code.split('\n'):
|
273 |
+
if line.startswith('import ') or line.startswith('from '):
|
274 |
+
imports.append(line.strip())
|
275 |
+
|
276 |
+
if imports:
|
277 |
+
analysis.append("## ๐ ์ฌ์ฉ๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ")
|
278 |
+
for imp in imports:
|
279 |
+
analysis.append(f"- `{imp}`")
|
280 |
+
analysis.append("")
|
281 |
+
|
282 |
+
# 2. ํจ์ ๋ถ์
|
283 |
+
functions = []
|
284 |
+
current_func = []
|
285 |
+
in_function = False
|
286 |
+
|
287 |
+
for line in code.split('\n'):
|
288 |
+
if line.strip().startswith('def '):
|
289 |
+
if current_func:
|
290 |
+
functions.append('\n'.join(current_func))
|
291 |
+
current_func = []
|
292 |
+
in_function = True
|
293 |
+
if in_function:
|
294 |
+
current_func.append(line)
|
295 |
+
if in_function and not line.strip():
|
296 |
+
in_function = False
|
297 |
+
if current_func:
|
298 |
+
functions.append('\n'.join(current_func))
|
299 |
+
current_func = []
|
300 |
+
|
301 |
+
if functions:
|
302 |
+
analysis.append("## ๐ง ์ฃผ์ ํจ์")
|
303 |
+
for func in functions:
|
304 |
+
func_name = func.split('def ')[1].split('(')[0]
|
305 |
+
analysis.append(f"### `{func_name}`")
|
306 |
+
analysis.append(get_function_description(func))
|
307 |
+
analysis.append("")
|
308 |
+
|
309 |
+
# 3. UI ์ปดํฌ๋ํธ ๋ถ์
|
310 |
+
ui_components = []
|
311 |
+
for line in code.split('\n'):
|
312 |
+
if 'gr.' in line:
|
313 |
+
component = line.split('gr.')[1].split('(')[0]
|
314 |
+
if component not in ui_components:
|
315 |
+
ui_components.append(component)
|
316 |
+
|
317 |
+
if ui_components:
|
318 |
+
analysis.append("## ๐จ UI ์ปดํฌ๋ํธ")
|
319 |
+
for component in ui_components:
|
320 |
+
analysis.append(f"- **{component}**: {get_component_description(component)}")
|
321 |
+
analysis.append("")
|
322 |
+
|
323 |
+
# 4. ์คํ ๋ฐฉ๋ฒ
|
324 |
+
analysis.append("## โถ๏ธ ์คํ ๋ฐฉ๋ฒ")
|
325 |
+
analysis.append("1. '์คํํ๊ธฐ' ๋ฒํผ์ ํด๋ฆญํ์ฌ Hugging Face Space์ ๋ฐฐํฌ")
|
326 |
+
analysis.append("2. ์์ฑ๋ ๋งํฌ๋ฅผ ํด๋ฆญํ์ฌ ์ ํ๋ฆฌ์ผ์ด์
์คํ")
|
327 |
+
|
328 |
+
return "\n".join(analysis)
|
329 |
|
330 |
def clear_history(self):
|
331 |
return []
|