Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -433,103 +433,26 @@ async def try_openai_api(openai_messages):
|
|
| 433 |
|
| 434 |
class Demo:
|
| 435 |
def __init__(self):
|
| 436 |
-
|
|
|
|
| 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 |
-
|
| 525 |
-
|
| 526 |
-
|
| 527 |
-
|
| 528 |
-
|
| 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(
|
| 550 |
try:
|
| 551 |
-
image = Image.open(file
|
| 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
|
| 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.
|
| 708 |
-
self.
|
| 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 |
"""ํ์ผ ๋ถ์ ๊ฒฐ๊ณผ์ ํ์ผ ๊ฐ์ฒด ์
๋ฐ์ดํธ"""
|