openfree commited on
Commit
d605a74
ยท
verified ยท
1 Parent(s): 37929fa

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +106 -49
app.py CHANGED
@@ -433,25 +433,103 @@ async def try_openai_api(openai_messages):
433
 
434
  class Demo:
435
  def __init__(self):
436
- self.file_state = FileAnalysisState() # ํŒŒ์ผ ์ƒํƒœ ๊ด€๋ฆฌ ์ถ”๊ฐ€
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
437
 
438
  async def generation_code(self, query: Optional[str], _setting: Dict[str, str]):
439
  if not query or query.strip() == '':
440
  query = get_random_placeholder()
441
 
442
- # ํŒŒ์ผ ๋ถ„์„ ๊ฒฐ๊ณผ๊ฐ€ ์žˆ๋Š” ๊ฒฝ์šฐ ์ปจํ…์ŠคํŠธ๋กœ ์ถ”๊ฐ€
443
- context = ""
444
- if self.file_state.last_analysis:
445
- context = "Based on the uploaded files:\n"
446
- for filename, analysis in self.file_state.last_analysis.items():
447
- context += f"\nFile '{filename}':\n{analysis}\n"
448
- query = f"{context}\n\nUser Query: {query}"
449
-
450
- # ์ด๋ฏธ์ง€ ์ƒ์„ฑ์ด ํ•„์š”ํ•œ์ง€ ํ™•์ธ
451
  needs_image = '์ด๋ฏธ์ง€' in query or '๊ทธ๋ฆผ' in query or 'image' in query.lower()
452
  image_prompt = None
453
-
454
- # ์ด๋ฏธ์ง€ ํ”„๋กฌํ”„ํŠธ ์ถ”์ถœ
455
  if needs_image:
456
  for keyword in ['์ด๋ฏธ์ง€:', '๊ทธ๋ฆผ:', 'image:']:
457
  if keyword in query.lower():
@@ -459,18 +537,18 @@ class Demo:
459
  break
460
  if not image_prompt:
461
  image_prompt = query
462
-
463
  messages = [{'role': Role.SYSTEM, 'content': _setting['system']}]
464
  messages.append({'role': Role.USER, 'content': query})
465
 
466
  system_message = messages[0]['content']
467
  claude_messages = [{"role": "user", "content": query}]
468
-
469
- # ์—…๋กœ๋“œ๋œ ์ด๋ฏธ์ง€๊ฐ€ ์žˆ๋Š” ๊ฒฝ์šฐ Claude API ๋ฉ”์‹œ์ง€์— ์ถ”๊ฐ€
470
- for filename, file in self.file_state.uploaded_files.items():
471
- if any(filename.lower().endswith(ext) for ext in ['.jpg', '.jpeg', '.png', '.gif', '.bmp', '.webp']):
472
  try:
473
- image = Image.open(file)
474
  image_bytes = io.BytesIO()
475
  image.save(image_bytes, format=image.format)
476
  image_base64 = base64.b64encode(image_bytes.getvalue()).decode('utf-8')
@@ -490,7 +568,8 @@ class Demo:
490
  }
491
  ]
492
  except Exception as e:
493
- print(f"Error processing uploaded image: {str(e)}")
 
494
 
495
  openai_messages = [
496
  {"role": "system", "content": system_message},
@@ -1207,43 +1286,20 @@ Use the "Generate" button for basic creation, "Enhance" button for prompt improv
1207
  with antd.Tabs.Item(key="render"):
1208
  sandbox = gr.HTML(elem_classes="html_content")
1209
 
1210
- async def handle_file_upload(files):
1211
- if not files:
1212
- return "No files uploaded"
1213
-
1214
- if len(files) > FileProcessor.MAX_FILES:
1215
- return f"Maximum {FileProcessor.MAX_FILES} files allowed"
1216
-
1217
- results = []
1218
- file_state.last_analysis.clear()
1219
- file_state.uploaded_files.clear()
1220
-
1221
- for file in files:
1222
- if not FileProcessor.is_allowed_file(file.name):
1223
- results.append(f"Unsupported file: {file.name}")
1224
- continue
1225
-
1226
- if os.path.getsize(file.name) > FileProcessor.MAX_FILE_SIZE:
1227
- results.append(f"File too large: {file.name}")
1228
- continue
1229
-
1230
- result = await FileProcessor.process_file(file)
1231
- file_state.last_analysis[file.name] = result
1232
- file_state.uploaded_files[file.name] = file
1233
- results.append(f"Analysis for {file.name}:\n{result}\n")
1234
-
1235
- return "\n".join(results)
1236
-
1237
 
1238
- # ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ ์—ฐ๊ฒฐ
 
1239
  analyze_btn.click(
1240
- fn=handle_file_upload,
1241
  inputs=[file_upload],
1242
  outputs=[file_analysis]
1243
  )
1244
 
 
1245
  btn.click(
1246
- demo_instance.generation_code,
1247
  inputs=[input, setting],
1248
  outputs=[code_output, sandbox, state_tab, code_drawer]
1249
  ).then(
@@ -1251,6 +1307,7 @@ Use the "Generate" button for basic creation, "Enhance" button for prompt improv
1251
  inputs=[],
1252
  outputs=[input]
1253
  )
 
1254
 
1255
  boost_btn.click(
1256
  fn=handle_boost,
 
433
 
434
  class Demo:
435
  def __init__(self):
436
+ self.file_analysis = {} # ํŒŒ์ผ ๋ถ„์„ ๊ฒฐ๊ณผ ์ €์žฅ
437
+ self.uploaded_files = {} # ์—…๋กœ๋“œ๋œ ํŒŒ์ผ ๊ฐ์ฒด ์ €์žฅ
438
+
439
+ async def handle_file_upload(self, files):
440
+ """ํŒŒ์ผ ์—…๋กœ๋“œ ์ฒ˜๋ฆฌ ํ•จ์ˆ˜"""
441
+ if not files:
442
+ return "No files uploaded"
443
+
444
+ results = []
445
+ self.file_analysis.clear()
446
+ self.uploaded_files.clear()
447
+
448
+ for file in files:
449
+ try:
450
+ # ์ด๋ฏธ์ง€ ํŒŒ์ผ ์ฒ˜๋ฆฌ
451
+ if file.name.lower().endswith(('.jpg', '.jpeg', '.png', '.gif', '.bmp', '.webp')):
452
+ image = Image.open(file.name)
453
+ image_bytes = io.BytesIO()
454
+ image.save(image_bytes, format=image.format)
455
+ image_base64 = base64.b64encode(image_bytes.getvalue()).decode('utf-8')
456
+
457
+ # Claude API๋กœ ์ด๋ฏธ์ง€ ๋ถ„์„
458
+ response = claude_client.messages.create(
459
+ model="claude-3-opus-20240229",
460
+ max_tokens=1000,
461
+ messages=[{
462
+ "role": "user",
463
+ "content": [
464
+ {
465
+ "type": "image",
466
+ "source": {
467
+ "type": "base64",
468
+ "media_type": f"image/{image.format.lower()}",
469
+ "data": image_base64
470
+ }
471
+ },
472
+ {
473
+ "type": "text",
474
+ "text": "Please analyze this image and provide a detailed description."
475
+ }
476
+ ]
477
+ }]
478
+ )
479
+ analysis = response.content[0].text
480
+
481
+ # ํ…์ŠคํŠธ ํŒŒ์ผ ์ฒ˜๋ฆฌ
482
+ elif file.name.lower().endswith(('.txt', '.pdf', '.docx', '.rtf')):
483
+ if file.name.lower().endswith('.pdf'):
484
+ pdf = fitz.open(file.name)
485
+ text_content = ""
486
+ for page in pdf:
487
+ text_content += page.get_text()
488
+ elif file.name.lower().endswith('.docx'):
489
+ doc = docx.Document(file.name)
490
+ text_content = "\n".join([paragraph.text for paragraph in doc.paragraphs])
491
+ else:
492
+ with open(file.name, 'r', encoding='utf-8') as f:
493
+ text_content = f.read()
494
+
495
+ # Claude API๋กœ ํ…์ŠคํŠธ ๋ถ„์„
496
+ response = claude_client.messages.create(
497
+ model="claude-3-opus-20240229",
498
+ max_tokens=1000,
499
+ messages=[{
500
+ "role": "user",
501
+ "content": f"Please analyze this text and provide a summary:\n\n{text_content[:2000]}..."
502
+ }]
503
+ )
504
+ analysis = response.content[0].text
505
+
506
+ else:
507
+ analysis = f"Unsupported file type: {file.name}"
508
+
509
+ # ๋ถ„์„ ๊ฒฐ๊ณผ ์ €์žฅ
510
+ self.file_analysis[file.name] = analysis
511
+ self.uploaded_files[file.name] = file
512
+ results.append(f"Analysis for {file.name}:\n{analysis}\n")
513
+
514
+ except Exception as e:
515
+ results.append(f"Error processing {file.name}: {str(e)}")
516
+
517
+ return "\n".join(results)
518
 
519
  async def generation_code(self, query: Optional[str], _setting: Dict[str, str]):
520
  if not query or query.strip() == '':
521
  query = get_random_placeholder()
522
 
523
+ # ํŒŒ์ผ ๋ถ„์„ ๊ฒฐ๊ณผ๋ฅผ ์ฟผ๋ฆฌ์— ํฌํ•จ
524
+ if self.file_analysis:
525
+ context = "Based on the analyzed files:\n\n"
526
+ for filename, analysis in self.file_analysis.items():
527
+ context += f"File '{filename}':\n{analysis}\n\n"
528
+ query = f"{context}\nUser Query: {query}"
529
+
530
+ # ์ด๋ฏธ์ง€ ์ƒ์„ฑ ๊ด€๋ จ ์ฝ”๋“œ๋Š” ์œ ์ง€
 
531
  needs_image = '์ด๋ฏธ์ง€' in query or '๊ทธ๋ฆผ' in query or 'image' in query.lower()
532
  image_prompt = None
 
 
533
  if needs_image:
534
  for keyword in ['์ด๋ฏธ์ง€:', '๊ทธ๋ฆผ:', 'image:']:
535
  if keyword in query.lower():
 
537
  break
538
  if not image_prompt:
539
  image_prompt = query
540
+
541
  messages = [{'role': Role.SYSTEM, 'content': _setting['system']}]
542
  messages.append({'role': Role.USER, 'content': query})
543
 
544
  system_message = messages[0]['content']
545
  claude_messages = [{"role": "user", "content": query}]
546
+
547
+ # ์—…๋กœ๋“œ๋œ ์ด๋ฏธ์ง€๊ฐ€ ์žˆ๋Š” ๊ฒฝ์šฐ Claude API ๋ฉ”์‹œ์ง€์— ํฌํ•จ
548
+ for filename, file in self.uploaded_files.items():
549
+ if filename.lower().endswith(('.jpg', '.jpeg', '.png', '.gif', '.bmp', '.webp')):
550
  try:
551
+ image = Image.open(file.name)
552
  image_bytes = io.BytesIO()
553
  image.save(image_bytes, format=image.format)
554
  image_base64 = base64.b64encode(image_bytes.getvalue()).decode('utf-8')
 
568
  }
569
  ]
570
  except Exception as e:
571
+ print(f"Error including image in message: {str(e)}")
572
+
573
 
574
  openai_messages = [
575
  {"role": "system", "content": system_message},
 
1286
  with antd.Tabs.Item(key="render"):
1287
  sandbox = gr.HTML(elem_classes="html_content")
1288
 
1289
+
1290
+ demo_instance = Demo() # Demo ์ธ์Šคํ„ด์Šค ์ƒ์„ฑ
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1291
 
1292
+
1293
+ # ํŒŒ์ผ ์—…๋กœ๋“œ ๋ฒ„ํŠผ ์ด๋ฒคํŠธ ์—ฐ๊ฒฐ
1294
  analyze_btn.click(
1295
+ fn=demo_instance.handle_file_upload, # Demo ์ธ์Šคํ„ด์Šค์˜ ๋ฉ”์„œ๋“œ ์‚ฌ์šฉ
1296
  inputs=[file_upload],
1297
  outputs=[file_analysis]
1298
  )
1299
 
1300
+ # Generate ๋ฒ„ํŠผ ์ด๋ฒคํŠธ ์—ฐ๊ฒฐ
1301
  btn.click(
1302
+ fn=demo_instance.generation_code,
1303
  inputs=[input, setting],
1304
  outputs=[code_output, sandbox, state_tab, code_drawer]
1305
  ).then(
 
1307
  inputs=[],
1308
  outputs=[input]
1309
  )
1310
+
1311
 
1312
  boost_btn.click(
1313
  fn=handle_boost,