openfree commited on
Commit
9be5bee
ยท
verified ยท
1 Parent(s): d605a74

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +105 -99
app.py CHANGED
@@ -433,103 +433,26 @@ async def try_openai_api(openai_messages):
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,18 +460,18 @@ class Demo:
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,8 +491,7 @@ class Demo:
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},
@@ -703,10 +625,94 @@ class Demo:
703
  print(f"Error details: {str(e)}")
704
  raise ValueError(f'Error calling APIs: {str(e)}')
705
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
706
  def clear_history(self):
707
- self.file_state.last_analysis.clear()
708
- self.file_state.uploaded_files.clear()
709
  return []
 
 
 
 
710
 
711
  def update_file_state(self, analysis_result, files):
712
  """ํŒŒ์ผ ๋ถ„์„ ๊ฒฐ๊ณผ์™€ ํŒŒ์ผ ๊ฐ์ฒด ์—…๋ฐ์ดํŠธ"""
 
433
 
434
  class Demo:
435
  def __init__(self):
436
+ # file_state ๋Œ€์‹  ์ง์ ‘ ์†์„ฑ์œผ๋กœ ์ •์˜
437
+ self.last_analysis = {} # ํŒŒ์ผ ๋ถ„์„ ๊ฒฐ๊ณผ ์ €์žฅ
438
  self.uploaded_files = {} # ์—…๋กœ๋“œ๋œ ํŒŒ์ผ ๊ฐ์ฒด ์ €์žฅ
439
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
440
  async def generation_code(self, query: Optional[str], _setting: Dict[str, str]):
441
  if not query or query.strip() == '':
442
  query = get_random_placeholder()
443
 
444
+ # ํŒŒ์ผ ๋ถ„์„ ๊ฒฐ๊ณผ๊ฐ€ ์žˆ๋Š” ๊ฒฝ์šฐ ์ปจํ…์ŠคํŠธ๋กœ ์ถ”๊ฐ€
445
+ context = ""
446
+ if self.last_analysis: # file_state.last_analysis ๋Œ€์‹  ์ง์ ‘ ์ฐธ์กฐ
447
+ context = "Based on the uploaded files:\n"
448
+ for filename, analysis in self.last_analysis.items():
449
+ context += f"\nFile '{filename}':\n{analysis}\n"
450
+ query = f"{context}\n\nUser Query: {query}"
451
+
452
+ # ์ด๋ฏธ์ง€ ์ƒ์„ฑ์ด ํ•„์š”ํ•œ์ง€ ํ™•์ธ
453
  needs_image = '์ด๋ฏธ์ง€' in query or '๊ทธ๋ฆผ' in query or 'image' in query.lower()
454
  image_prompt = None
455
+
456
  if needs_image:
457
  for keyword in ['์ด๋ฏธ์ง€:', '๊ทธ๋ฆผ:', 'image:']:
458
  if keyword in query.lower():
 
460
  break
461
  if not image_prompt:
462
  image_prompt = query
463
+
464
  messages = [{'role': Role.SYSTEM, 'content': _setting['system']}]
465
  messages.append({'role': Role.USER, 'content': query})
466
 
467
  system_message = messages[0]['content']
468
  claude_messages = [{"role": "user", "content": query}]
469
+
470
+ # ์—…๋กœ๋“œ๋œ ์ด๋ฏธ์ง€๊ฐ€ ์žˆ๋Š” ๊ฒฝ์šฐ Claude API ๋ฉ”์‹œ์ง€์— ์ถ”๊ฐ€
471
+ for filename, file in self.uploaded_files.items(): # file_state.uploaded_files ๋Œ€์‹  ์ง์ ‘ ์ฐธ์กฐ
472
+ if any(filename.lower().endswith(ext) for ext in ['.jpg', '.jpeg', '.png', '.gif', '.bmp', '.webp']):
473
  try:
474
+ image = Image.open(file)
475
  image_bytes = io.BytesIO()
476
  image.save(image_bytes, format=image.format)
477
  image_base64 = base64.b64encode(image_bytes.getvalue()).decode('utf-8')
 
491
  }
492
  ]
493
  except Exception as e:
494
+ print(f"Error processing uploaded image: {str(e)}")
 
495
 
496
  openai_messages = [
497
  {"role": "system", "content": system_message},
 
625
  print(f"Error details: {str(e)}")
626
  raise ValueError(f'Error calling APIs: {str(e)}')
627
 
628
+ async def handle_file_upload(self, files):
629
+ """ํŒŒ์ผ ์—…๋กœ๋“œ ์ฒ˜๋ฆฌ ํ•จ์ˆ˜"""
630
+ if not files:
631
+ return "No files uploaded"
632
+
633
+ results = []
634
+ self.last_analysis.clear() # file_state.last_analysis ๋Œ€์‹  ์ง์ ‘ ์ฐธ์กฐ
635
+ self.uploaded_files.clear() # file_state.uploaded_files ๋Œ€์‹  ์ง์ ‘ ์ฐธ์กฐ
636
+
637
+ for file in files:
638
+ try:
639
+ # ์ด๋ฏธ์ง€ ํŒŒ์ผ ์ฒ˜๋ฆฌ
640
+ if file.name.lower().endswith(('.jpg', '.jpeg', '.png', '.gif', '.bmp', '.webp')):
641
+ image = Image.open(file.name)
642
+ image_bytes = io.BytesIO()
643
+ image.save(image_bytes, format=image.format)
644
+ image_base64 = base64.b64encode(image_bytes.getvalue()).decode('utf-8')
645
+
646
+ # Claude API๋กœ ์ด๋ฏธ์ง€ ๋ถ„์„
647
+ response = claude_client.messages.create(
648
+ model="claude-3-5-sonnet-20241022",
649
+ max_tokens=1000,
650
+ messages=[{
651
+ "role": "user",
652
+ "content": [
653
+ {
654
+ "type": "image",
655
+ "source": {
656
+ "type": "base64",
657
+ "media_type": f"image/{image.format.lower()}",
658
+ "data": image_base64
659
+ }
660
+ },
661
+ {
662
+ "type": "text",
663
+ "text": "Please analyze this image and provide a detailed description."
664
+ }
665
+ ]
666
+ }]
667
+ )
668
+ analysis = response.content[0].text
669
+
670
+ # ํ…์ŠคํŠธ ํŒŒ์ผ ์ฒ˜๋ฆฌ
671
+ elif file.name.lower().endswith(('.txt', '.pdf', '.docx', '.rtf')):
672
+ if file.name.lower().endswith('.pdf'):
673
+ pdf = fitz.open(file.name)
674
+ text_content = ""
675
+ for page in pdf:
676
+ text_content += page.get_text()
677
+ elif file.name.lower().endswith('.docx'):
678
+ doc = docx.Document(file.name)
679
+ text_content = "\n".join([paragraph.text for paragraph in doc.paragraphs])
680
+ else:
681
+ with open(file.name, 'r', encoding='utf-8') as f:
682
+ text_content = f.read()
683
+
684
+ # Claude API๋กœ ํ…์ŠคํŠธ ๋ถ„์„
685
+ response = claude_client.messages.create(
686
+ model="claude-3-opus-20240229",
687
+ max_tokens=1000,
688
+ messages=[{
689
+ "role": "user",
690
+ "content": f"Please analyze this text and provide a summary:\n\n{text_content[:2000]}..."
691
+ }]
692
+ )
693
+ analysis = response.content[0].text
694
+
695
+ else:
696
+ analysis = f"Unsupported file type: {file.name}"
697
+
698
+ # ๋ถ„์„ ๊ฒฐ๊ณผ ์ €์žฅ
699
+ self.last_analysis[file.name] = analysis
700
+ self.uploaded_files[file.name] = file
701
+ results.append(f"Analysis for {file.name}:\n{analysis}\n")
702
+
703
+ except Exception as e:
704
+ results.append(f"Error processing {file.name}: {str(e)}")
705
+
706
+ return "\n".join(results)
707
+
708
  def clear_history(self):
709
+ self.last_analysis.clear()
710
+ self.uploaded_files.clear()
711
  return []
712
+
713
+
714
+
715
+
716
 
717
  def update_file_state(self, analysis_result, files):
718
  """ํŒŒ์ผ ๋ถ„์„ ๊ฒฐ๊ณผ์™€ ํŒŒ์ผ ๊ฐ์ฒด ์—…๋ฐ์ดํŠธ"""