Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -604,622 +604,6 @@ def chatbot_interface():
|
|
604 |
def main():
|
605 |
chatbot_interface()
|
606 |
|
607 |
-
if __name__ == "__main__":
|
608 |
-
# requirements.txt ํ์ผ ์์ฑ
|
609 |
-
with open("requirements.txt", "w") as f:
|
610 |
-
f.write("streamlit>=1.31.0\n")
|
611 |
-
f.write("anthropic>=0.18.1\n")
|
612 |
-
f.write("gradio-client>=1.8.0\n")
|
613 |
-
f.write("requests>=2.32.3\n")
|
614 |
-
f.write("markdown>=3.5.1\n")
|
615 |
-
f.write("pillow>=10.1.0\n")
|
616 |
-
|
617 |
-
main()import os
|
618 |
-
import streamlit as st
|
619 |
-
import json
|
620 |
-
import anthropic
|
621 |
-
import requests
|
622 |
-
import logging
|
623 |
-
from gradio_client import Client
|
624 |
-
import markdown
|
625 |
-
import tempfile
|
626 |
-
import base64
|
627 |
-
from datetime import datetime
|
628 |
-
import re
|
629 |
-
|
630 |
-
# ๋ก๊น
์ค์
|
631 |
-
logging.basicConfig(
|
632 |
-
level=logging.INFO,
|
633 |
-
format='%(asctime)s - %(levelname)s - %(message)s')
|
634 |
-
|
635 |
-
# API ์ค์
|
636 |
-
api_key = os.environ.get("API_KEY")
|
637 |
-
client = anthropic.Anthropic(api_key=api_key)
|
638 |
-
|
639 |
-
# ์ด๋ฏธ์ง ์์ฑ API URL
|
640 |
-
IMAGE_API_URL = "http://211.233.58.201:7896"
|
641 |
-
|
642 |
-
# ์ต๋ ํ ํฐ ์ ์ค์ (Claude-3 Sonnet์ ์ต๋ ํ ํฐ ์)
|
643 |
-
MAX_TOKENS = 7999
|
644 |
-
|
645 |
-
# SerpHouse API Key ์ค์
|
646 |
-
SERPHOUSE_API_KEY = os.environ.get("SERPHOUSE_API_KEY", "")
|
647 |
-
|
648 |
-
def get_system_prompt():
|
649 |
-
return """
|
650 |
-
๋น์ ์ ์ ๋ฌธ ๋ธ๋ก๊ทธ ์์ฑ ์ ๋ฌธ๊ฐ์
๋๋ค. ๋ชจ๋ ๋ธ๋ก๊ทธ ๊ธ ์์ฑ ์์ฒญ์ ๋ํด ๋ค์์ 8๋จ๊ณ ํ๋ ์์ํฌ๋ฅผ ์ฒ ์ ํ ๋ฐ๋ฅด๋, ์์ฐ์ค๋ฝ๊ณ ๋งค๋ ฅ์ ์ธ ๊ธ์ด ๋๋๋ก ์์ฑํด์ผ ํฉ๋๋ค:
|
651 |
-
|
652 |
-
๋
์ ์ฐ๊ฒฐ ๋จ๊ณ 1.1. ๊ณต๊ฐ๋ ํ์ฑ์ ์ํ ์น๊ทผํ ์ธ์ฌ 1.2. ๋
์์ ์ค์ ๊ณ ๋ฏผ์ ๋ฐ์ํ ๋์
์ง๋ฌธ 1.3. ์ฃผ์ ์ ๋ํ ์ฆ๊ฐ์ ๊ด์ฌ ์ ๋
|
653 |
-
|
654 |
-
๋ฌธ์ ์ ์ ๋จ๊ณ 2.1. ๋
์์ ํ์ธํฌ์ธํธ ๊ตฌ์ฒดํ 2.2. ๋ฌธ์ ์ ์๊ธ์ฑ๊ณผ ์ํฅ๋ ๋ถ์ 2.3. ํด๊ฒฐ ํ์์ฑ์ ๋ํ ๊ณต๊ฐ๋ ํ์ฑ
|
655 |
-
|
656 |
-
์ ๋ฌธ์ฑ ์
์ฆ ๋จ๊ณ 3.1. ๊ฐ๊ด์ ๋ฐ์ดํฐ ๊ธฐ๋ฐ ๋ถ์ 3.2. ์ ๋ฌธ๊ฐ ๊ฒฌํด์ ์ฐ๊ตฌ ๊ฒฐ๊ณผ ์ธ์ฉ 3.3. ์ค์ ์ฌ๋ก๋ฅผ ํตํ ๋ฌธ์ ๊ตฌ์ฒดํ
|
657 |
-
|
658 |
-
์๋ฃจ์
์ ๊ณต ๋จ๊ณ 4.1. ๋จ๊ณ๋ณ ์ค์ฒ ๊ฐ์ด๋๋ผ์ธ ์ ์ 4.2. ์ฆ์ ์ ์ฉ ๊ฐ๋ฅํ ๊ตฌ์ฒด์ ํ 4.3. ์์ ์ฅ์ ๋ฌผ๊ณผ ๊ทน๋ณต ๋ฐฉ์ ํฌํจ
|
659 |
-
|
660 |
-
์ ๋ขฐ๋ ๊ฐํ ๋จ๊ณ 5.1. ์ค์ ์ฑ๊ณต ์ฌ๋ก ์ ์ 5.2. ๊ตฌ์ฒด์ ์ฌ์ฉ์ ํ๊ธฐ ์ธ์ฉ 5.3. ๊ฐ๊ด์ ๋ฐ์ดํฐ๋ก ํจ๊ณผ ์
์ฆ
|
661 |
-
|
662 |
-
ํ๋ ์ ๋ ๋จ๊ณ 6.1. ๋ช
ํํ ์ฒซ ์ค์ฒ ๋จ๊ณ ์ ์ 6.2. ์๊ธ์ฑ์ ๊ฐ์กฐํ ํ๋ ์ด๊ตฌ 6.3. ์ค์ฒ ๋๊ธฐ ๋ถ์ฌ ์์ ํฌํจ
|
663 |
-
|
664 |
-
์ง์ ์ฑ ๊ฐํ ๋จ๊ณ 7.1. ์๋ฃจ์
์ ํ๊ณ ํฌ๋ช
ํ๊ฒ ๊ณต๊ฐ 7.2. ๊ฐ์ธ๋ณ ์ฐจ์ด ์กด์ฌ ์ธ์ 7.3. ํ์ ์กฐ๊ฑด๊ณผ ์ฃผ์์ฌํญ ๋ช
์
|
665 |
-
|
666 |
-
๊ด๊ณ ์ง์ ๋จ๊ณ 8.1. ์ง์ ์ฑ ์๋ ๊ฐ์ฌ ์ธ์ฌ 8.2. ๋ค์ ์ปจํ
์ธ ์๊ณ ๋ก ๊ธฐ๋๊ฐ ์กฐ์ฑ 8.3. ์ํต ์ฑ๋ ์๋ด
|
667 |
-
|
668 |
-
์์ฑ ์ ์ค์์ฌํญ 9.1. ๊ธ์ ์: 1500-2000์ ๋ด์ธ 9.2. ๋ฌธ๋จ ๊ธธ์ด: 3-4๋ฌธ์ฅ ์ด๋ด 9.3. ์๊ฐ์ ๊ตฌ๋ถ: ์์ ๋ชฉ, ๊ตฌ๋ถ์ , ๋ฒํธ ๋ชฉ๋ก ํ์ฉ 9.4. ํค์ค๋งค๋: ์น๊ทผํ๊ณ ์ ๋ฌธ์ ์ธ ๋ํ์ฒด 9.5. ๋ฐ์ดํฐ: ๋ชจ๋ ์ ๋ณด์ ์ถ์ฒ ๋ช
์ 9.6. ๊ฐ๋
์ฑ: ๋ช
ํํ ๋จ๋ฝ ๊ตฌ๋ถ๊ณผ ๊ฐ์กฐ์ ์ฌ์ฉ
|
669 |
-
|
670 |
-
์ด๋ฌํ ํ๋ ์์ํฌ๋ฅผ ๋ฐํ์ผ๋ก, ์์ฒญ๋ฐ์ ์ฃผ์ ์ ๋ํด ์ฒด๊ณ์ ์ด๊ณ ๋งค๋ ฅ์ ์ธ ๋ธ๋ก๊ทธ ํฌ์คํธ๋ฅผ ์์ฑํ๊ฒ ์ต๋๋ค.
|
671 |
-
"""
|
672 |
-
|
673 |
-
def test_image_api_connection():
|
674 |
-
"""์ด๋ฏธ์ง API ์๋ฒ ์ฐ๊ฒฐ ํ
์คํธ"""
|
675 |
-
try:
|
676 |
-
client = Client(IMAGE_API_URL)
|
677 |
-
return "์ด๋ฏธ์ง API ์ฐ๊ฒฐ ์ฑ๊ณต: ์ ์ ์๋ ์ค"
|
678 |
-
except Exception as e:
|
679 |
-
logging.error(f"์ด๋ฏธ์ง API ์ฐ๊ฒฐ ํ
์คํธ ์คํจ: {e}")
|
680 |
-
return f"์ด๋ฏธ์ง API ์ฐ๊ฒฐ ์คํจ: {e}"
|
681 |
-
|
682 |
-
def generate_image(prompt, width=768, height=768, guidance=3.5, inference_steps=30, seed=3):
|
683 |
-
"""์ด๋ฏธ์ง ์์ฑ ํจ์"""
|
684 |
-
if not prompt:
|
685 |
-
return None, "์ค๋ฅ: ํ๋กฌํํธ๋ฅผ ์
๋ ฅํด์ฃผ์ธ์"
|
686 |
-
|
687 |
-
try:
|
688 |
-
client = Client(IMAGE_API_URL)
|
689 |
-
result = client.predict(
|
690 |
-
prompt=prompt,
|
691 |
-
width=int(width),
|
692 |
-
height=int(height),
|
693 |
-
guidance=float(guidance),
|
694 |
-
inference_steps=int(inference_steps),
|
695 |
-
seed=int(seed),
|
696 |
-
do_img2img=False,
|
697 |
-
init_image=None,
|
698 |
-
image2image_strength=0.8,
|
699 |
-
resize_img=True,
|
700 |
-
api_name="/generate_image"
|
701 |
-
)
|
702 |
-
logging.info(f"์ด๋ฏธ์ง ์์ฑ ์ฑ๊ณต: {result[1]}")
|
703 |
-
return result[0], f"์ฌ์ฉ๋ ์๋: {result[1]}"
|
704 |
-
except Exception as e:
|
705 |
-
logging.error(f"์ด๋ฏธ์ง ์์ฑ ์คํจ: {str(e)}")
|
706 |
-
return None, f"์ค๋ฅ: {str(e)}"
|
707 |
-
|
708 |
-
def extract_image_prompt(blog_content, blog_topic):
|
709 |
-
"""๋ธ๋ก๊ทธ ๋ด์ฉ์์ ์ด๋ฏธ์ง ์์ฑ์ ์ํ ํ๋กฌํํธ ์ถ์ถ"""
|
710 |
-
image_prompt_system = f"""
|
711 |
-
๋ค์์ '{blog_topic}'์ ๊ดํ ๋ธ๋ก๊ทธ ๊ธ์
๋๋ค. ์ด ๋ธ๋ก๊ทธ ๊ธ์ ๋ด์ฉ์ ๊ธฐ๋ฐ์ผ๋ก ์ ์ ํ ์ด๋ฏธ์ง๋ฅผ ์์ฑํ๊ธฐ ์ํ
|
712 |
-
ํ๋กฌํํธ๋ฅผ ์์ฑํด์ฃผ์ธ์. ํ๋กฌํํธ๋ ์์ด๋ก ์์ฑํ๊ณ , ๊ตฌ์ฒด์ ์ธ ์๊ฐ์ ์์๋ฅผ ๋ด์์ผ ํฉ๋๋ค.
|
713 |
-
ํ๋กฌํํธ๋ง ๋ฐํํ์ธ์(๋ค๋ฅธ ์ค๋ช
์์ด).
|
714 |
-
|
715 |
-
์์ ํ์:
|
716 |
-
"A professional photo of [subject], [specific details], [atmosphere], [lighting], [perspective], high quality, detailed"
|
717 |
-
"""
|
718 |
-
|
719 |
-
try:
|
720 |
-
response = client.messages.create(
|
721 |
-
model="claude-3-7-sonnet-20250219",
|
722 |
-
max_tokens=150,
|
723 |
-
system=image_prompt_system,
|
724 |
-
messages=[{"role": "user", "content": blog_content}]
|
725 |
-
)
|
726 |
-
|
727 |
-
# ์๋ต์์ ํ๋กฌํํธ ์ถ์ถ
|
728 |
-
image_prompt = response.content[0].text.strip()
|
729 |
-
logging.info(f"์์ฑ๋ ์ด๋ฏธ์ง ํ๋กฌํํธ: {image_prompt}")
|
730 |
-
return image_prompt
|
731 |
-
except Exception as e:
|
732 |
-
logging.error(f"์ด๋ฏธ์ง ํ๋กฌํํธ ์์ฑ ์ค๋ฅ: {e}")
|
733 |
-
return f"A professional photo related to {blog_topic}, detailed, high quality"
|
734 |
-
|
735 |
-
# ๋งํฌ๋ค์ด์ HTML๋ก ๋ณํํ๋ ํจ์
|
736 |
-
def convert_md_to_html(md_text, title="Ginigen Blog"):
|
737 |
-
html_content = markdown.markdown(md_text)
|
738 |
-
html_doc = f"""
|
739 |
-
<!DOCTYPE html>
|
740 |
-
<html>
|
741 |
-
<head>
|
742 |
-
<title>{title}</title>
|
743 |
-
<meta charset="utf-8">
|
744 |
-
<style>
|
745 |
-
body {{ font-family: Arial, sans-serif; line-height: 1.6; max-width: 800px; margin: 0 auto; padding: 20px; }}
|
746 |
-
h1 {{ color: #2c3e50; font-size: 2.5em; margin-bottom: 20px; }}
|
747 |
-
h2 {{ color: #3498db; margin-top: 25px; font-size: 1.8em; }}
|
748 |
-
h3 {{ color: #2980b9; font-size: 1.5em; }}
|
749 |
-
p {{ margin-bottom: 15px; font-size: 1.1em; }}
|
750 |
-
blockquote {{ background: #f9f9f9; border-left: 10px solid #ccc; margin: 1.5em 10px; padding: 1em 10px; }}
|
751 |
-
ul, ol {{ margin-bottom: 15px; }}
|
752 |
-
li {{ margin-bottom: 5px; }}
|
753 |
-
hr {{ border: 0; height: 1px; background: #ddd; margin: 20px 0; }}
|
754 |
-
img {{ max-width: 100%; height: auto; display: block; margin: 20px auto; }}
|
755 |
-
</style>
|
756 |
-
</head>
|
757 |
-
<body>
|
758 |
-
{html_content}
|
759 |
-
</body>
|
760 |
-
</html>
|
761 |
-
"""
|
762 |
-
return html_doc
|
763 |
-
|
764 |
-
# ์น ๊ฒ์ ํค์๋ ์ถ์ถ ํจ์
|
765 |
-
def extract_keywords(text: str, top_k: int = 5) -> str:
|
766 |
-
"""
|
767 |
-
1) ํ๊ธ(๊ฐ-ํฃ), ์์ด(a-zA-Z), ์ซ์(0-9), ๊ณต๋ฐฑ๋ง ๋จ๊น
|
768 |
-
2) ๊ณต๋ฐฑ ๊ธฐ์ค ํ ํฐ ๋ถ๋ฆฌ
|
769 |
-
3) ์ต๋ top_k๊ฐ๋ง
|
770 |
-
"""
|
771 |
-
text = re.sub(r"[^a-zA-Z0-9๊ฐ-ํฃ\s]", "", text)
|
772 |
-
tokens = text.split()
|
773 |
-
key_tokens = tokens[:top_k]
|
774 |
-
return " ".join(key_tokens)
|
775 |
-
|
776 |
-
# Mock ๊ฒ์ ๊ฒฐ๊ณผ ์์ฑ ํจ์
|
777 |
-
def generate_mock_search_results(query):
|
778 |
-
"""API ์ฐ๊ฒฐ์ด ์๋ ๋ ์ฌ์ฉํ ๊ฐ์ ๊ฒ์ ๊ฒฐ๊ณผ ์์ฑ"""
|
779 |
-
current_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
780 |
-
mock_results = [
|
781 |
-
{
|
782 |
-
"title": f"{query}์ ๊ดํ ์ต์ ์ ๋ณด",
|
783 |
-
"link": "https://example.com/article1",
|
784 |
-
"snippet": f"{query}์ ๊ดํ ๊ฐ์ ๊ฒ์ ๊ฒฐ๊ณผ์
๋๋ค. ์ด ๊ฒฐ๊ณผ๋ API ์ฐ๊ฒฐ ๋ฌธ์ ๋ก ์ธํด ์์ฑ๋ ๊ฐ์ ๋ฐ์ดํฐ์
๋๋ค. ์ค์ ๊ฒ์ ๊ฒฐ๊ณผ๊ฐ ์๋์ ์ฐธ๊ณ ํ์ธ์. ์์ฑ ์๊ฐ: {current_time}",
|
785 |
-
"displayed_link": "example.com/article1"
|
786 |
-
},
|
787 |
-
{
|
788 |
-
"title": f"{query} ๊ด๋ จ ์ฐ๊ตฌ ๋ํฅ",
|
789 |
-
"link": "https://example.org/research",
|
790 |
-
"snippet": "์ด๊ฒ์ API ์ฐ๊ฒฐ ๋ฌธ์ ๋ก ์ธํ ๊ฐ์ ๊ฒ์ ๊ฒฐ๊ณผ์
๋๋ค. ์ค์ ๊ฒ์ ๊ฒฐ๊ณผ๋ฅผ ๋ณด์ฌ๋๋ฆฌ์ง ๋ชปํด ์ฃ์กํฉ๋๋ค. ๋์ AI์ ๊ธฐ์กด ์ง์์ ํ์ฉํ์ฌ ๋ต๋ณ๋๋ฆฌ๊ฒ ์ต๋๋ค.",
|
791 |
-
"displayed_link": "example.org/research"
|
792 |
-
},
|
793 |
-
{
|
794 |
-
"title": f"{query}์ ์ญ์ฌ์ ๋ฐฐ๊ฒฝ",
|
795 |
-
"link": "https://example.net/history",
|
796 |
-
"snippet": "์ด ๊ฐ์ ๊ฒ์ ๊ฒฐ๊ณผ๋ API ์ฐ๊ฒฐ ๋ฌธ์ ๋ก ์ธํด ์์ฑ๋์์ต๋๋ค. ์ฐธ๊ณ ์ฉ์ผ๋ก๋ง ์ฌ์ฉํด์ฃผ์ธ์.",
|
797 |
-
"displayed_link": "example.net/history"
|
798 |
-
}
|
799 |
-
]
|
800 |
-
|
801 |
-
summary_lines = []
|
802 |
-
for idx, item in enumerate(mock_results, start=1):
|
803 |
-
title = item.get("title", "No title")
|
804 |
-
link = item.get("link", "#")
|
805 |
-
snippet = item.get("snippet", "No description")
|
806 |
-
displayed_link = item.get("displayed_link", link)
|
807 |
-
|
808 |
-
summary_lines.append(
|
809 |
-
f"### Result {idx}: {title}\n\n"
|
810 |
-
f"{snippet}\n\n"
|
811 |
-
f"**์ถ์ฒ**: [{displayed_link}]({link})\n\n"
|
812 |
-
f"---\n"
|
813 |
-
)
|
814 |
-
|
815 |
-
notice = """
|
816 |
-
# ๊ฐ์ ๊ฒ์ ๊ฒฐ๊ณผ (API ์ฐ๊ฒฐ ๋ฌธ์ ๋ก ์ธํด ์์ฑ๋จ)
|
817 |
-
์๋๋ API ์ฐ๊ฒฐ ๋ฌธ์ ๋ก ์ธํด ์์ฑ๋ ๊ฐ์ ๊ฒ์ ๊ฒฐ๊ณผ์
๋๋ค. ์ค์ ๊ฒ์ ๊ฒฐ๊ณผ๊ฐ ์๋์ ์ฐธ๊ณ ํ์ธ์.
|
818 |
-
๋์ AI์ ๊ธฐ์กด ์ง์์ ํ์ฉํ์ฌ ์ต๋ํ ์ ํํ ๋ต๋ณ์ ๋๋ฆฌ๊ฒ ์ต๋๋ค.
|
819 |
-
"""
|
820 |
-
|
821 |
-
return notice + "\n".join(summary_lines)
|
822 |
-
|
823 |
-
# Google ๊ฒ์ ํจ์ (SerpAPI ๋์ ์ง์ ๊ฒ์)
|
824 |
-
def do_google_search(query, num_results=5):
|
825 |
-
try:
|
826 |
-
# ๊ธฐ๋ณธ ํค๋ ์ค์ (๋ธ๋ผ์ฐ์ ์ฒ๋ผ ๋ณด์ด๊ฒ)
|
827 |
-
headers = {
|
828 |
-
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
|
829 |
-
}
|
830 |
-
|
831 |
-
# ๊ฒ์ URL ๋ง๋ค๊ธฐ
|
832 |
-
search_url = f"https://www.google.com/search?q={query}&num={num_results}"
|
833 |
-
logging.info(f"๊ตฌ๊ธ ๊ฒ์ URL: {search_url}")
|
834 |
-
|
835 |
-
# ์์ฒญ ๋ณด๋ด๊ธฐ (์งง์ ํ์์์ ์ค์ )
|
836 |
-
response = requests.get(search_url, headers=headers, timeout=10)
|
837 |
-
|
838 |
-
# ์๋ต์ด ์ฑ๊ณต์ ์ธ์ง ํ์ธ
|
839 |
-
if response.status_code != 200:
|
840 |
-
logging.error(f"Google ๊ฒ์ ์๋ต ์ํ ์ฝ๋: {response.status_code}")
|
841 |
-
return generate_mock_search_results(query)
|
842 |
-
|
843 |
-
# ์ฌ๊ธฐ์๋ HTML ๋ถ์์ด ๋ณต์กํ๋ฏ๋ก, ์ค์ ๋ก๋ BeautifulSoup ๊ฐ์ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ฌ์ฉํ๋ ๊ฒ์ด ์ข์
|
844 |
-
# ๊ฐ๋จํ ๋ฐ๋ชจ ๋ชฉ์ ์ผ๋ก ๋์ Mock ๋ฐ์ดํฐ ๋ฐํ
|
845 |
-
logging.info("Google ๊ฒ์ ์ฑ๊ณตํ์ผ๋ ํ์ฑ ์ ํ์ผ๋ก Mock ๋ฐ์ดํฐ ๋ฐํ")
|
846 |
-
return generate_mock_search_results(query)
|
847 |
-
|
848 |
-
except Exception as e:
|
849 |
-
logging.error(f"Google ๊ฒ์ ์คํจ: {e}")
|
850 |
-
return generate_mock_search_results(query)
|
851 |
-
|
852 |
-
# ์น ๊ฒ์ ํจ์
|
853 |
-
def do_web_search(query: str) -> str:
|
854 |
-
"""
|
855 |
-
์น ๊ฒ์์ ์ํํ๋ ํจ์ - SerpHouse API ๋๋ ์ง์ ๊ตฌ๊ธ ๊ฒ์
|
856 |
-
"""
|
857 |
-
try:
|
858 |
-
# API ํค๊ฐ ์๊ฑฐ๋ 'mock'์ธ ๊ฒฝ์ฐ
|
859 |
-
if not SERPHOUSE_API_KEY or "mock" in SERPHOUSE_API_KEY.lower():
|
860 |
-
logging.warning("API ํค๊ฐ ์๊ฑฐ๋ Mock ๋ชจ๋์
๋๋ค. ๊ฐ์ ๊ฒ์ ๊ฒฐ๊ณผ๋ฅผ ๋ฐํํฉ๋๋ค.")
|
861 |
-
return generate_mock_search_results(query)
|
862 |
-
|
863 |
-
# SerpHouse API ์ฌ์ฉ
|
864 |
-
url = "https://api.serphouse.com/serp/live"
|
865 |
-
params = {
|
866 |
-
"q": query,
|
867 |
-
"domain": "google.com",
|
868 |
-
"serp_type": "web",
|
869 |
-
"device": "desktop",
|
870 |
-
"lang": "ko", # ํ๊ตญ์ด ๊ฒฐ๊ณผ
|
871 |
-
"num": "5" # ๊ฒฐ๊ณผ ์ ์ค์
|
872 |
-
}
|
873 |
-
|
874 |
-
headers = {
|
875 |
-
"Authorization": f"Bearer {SERPHOUSE_API_KEY}"
|
876 |
-
}
|
877 |
-
|
878 |
-
logging.info(f"SerpHouse API ํธ์ถ ์ค... ๊ฒ์์ด: {query}")
|
879 |
-
|
880 |
-
# ์งง์ ํ์์์์ผ๋ก ์์ฒญ ์๋
|
881 |
-
response = requests.get(url, headers=headers, params=params, timeout=15)
|
882 |
-
response.raise_for_status()
|
883 |
-
|
884 |
-
logging.info(f"SerpHouse API ์๋ต ์ํ ์ฝ๋: {response.status_code}")
|
885 |
-
data = response.json()
|
886 |
-
|
887 |
-
# ๋ค์ํ ์๋ต ๊ตฌ์กฐ ์ฒ๋ฆฌ
|
888 |
-
results = data.get("results", {})
|
889 |
-
organic = None
|
890 |
-
|
891 |
-
# ๊ฐ๋ฅํ ์๋ต ๊ตฌ์กฐ 1
|
892 |
-
if isinstance(results, dict) and "organic" in results:
|
893 |
-
organic = results["organic"]
|
894 |
-
# ๊ฐ๋ฅํ ์๋ต ๊ตฌ์กฐ 2
|
895 |
-
elif isinstance(results, dict) and "results" in results:
|
896 |
-
if isinstance(results["results"], dict) and "organic" in results["results"]:
|
897 |
-
organic = results["results"]["organic"]
|
898 |
-
# ๊ฐ๋ฅํ ์๋ต ๊ตฌ์กฐ 3
|
899 |
-
elif "organic" in data:
|
900 |
-
organic = data["organic"]
|
901 |
-
|
902 |
-
if not organic:
|
903 |
-
logging.warning("์๋ต์์ organic ๊ฒฐ๊ณผ๋ฅผ ์ฐพ์ ์ ์์ต๋๋ค. ๊ตฌ๊ธ ์ง์ ๊ฒ์์ผ๋ก ์ ํํฉ๋๋ค.")
|
904 |
-
return do_google_search(query)
|
905 |
-
|
906 |
-
# ๊ฒฐ๊ณผ ์ ์ ํ ๋ฐ ์ปจํ
์คํธ ๊ธธ์ด ์ต์ ํ
|
907 |
-
max_results = min(5, len(organic))
|
908 |
-
limited_organic = organic[:max_results]
|
909 |
-
|
910 |
-
# ๊ฒฐ๊ณผ ํ์ ๊ฐ์
|
911 |
-
summary_lines = []
|
912 |
-
for idx, item in enumerate(limited_organic, start=1):
|
913 |
-
title = item.get("title", "No title")
|
914 |
-
link = item.get("link", "#")
|
915 |
-
snippet = item.get("snippet", "No description")
|
916 |
-
displayed_link = item.get("displayed_link", link)
|
917 |
-
|
918 |
-
summary_lines.append(
|
919 |
-
f"### Result {idx}: {title}\n\n"
|
920 |
-
f"{snippet}\n\n"
|
921 |
-
f"**์ถ์ฒ**: [{displayed_link}]({link})\n\n"
|
922 |
-
f"---\n"
|
923 |
-
)
|
924 |
-
|
925 |
-
# ๋ชจ๋ธ์๊ฒ ๋ช
ํํ ์ง์นจ ์ถ๊ฐ
|
926 |
-
instructions = """
|
927 |
-
# ์น ๊ฒ์ ๊ฒฐ๊ณผ
|
928 |
-
์๋๋ ๊ฒ์ ๊ฒฐ๊ณผ์
๋๋ค. ์ง๋ฌธ์ ๋ต๋ณํ ๋ ์ด ์ ๋ณด๋ฅผ ํ์ฉํ์ธ์:
|
929 |
-
1. ๊ฐ ๊ฒฐ๊ณผ์ ์ ๋ชฉ, ๋ด์ฉ, ์ถ์ฒ ๋งํฌ๋ฅผ ์ฐธ๊ณ ํ์ธ์
|
930 |
-
2. ๋ต๋ณ์ ๊ด๋ จ ์ ๋ณด์ ์ถ์ฒ๋ฅผ ๋ช
์์ ์ผ๋ก ์ธ์ฉํ์ธ์ (์: "X ์ถ์ฒ์ ๋ฐ๋ฅด๋ฉด...")
|
931 |
-
3. ์๋ต์ ์ค์ ์ถ์ฒ ๋งํฌ๋ฅผ ํฌํจํ์ธ์
|
932 |
-
4. ์ฌ๋ฌ ์ถ์ฒ์ ์ ๋ณด๋ฅผ ์ข
ํฉํ์ฌ ๋ต๋ณํ์ธ์
|
933 |
-
"""
|
934 |
-
|
935 |
-
search_results = instructions + "\n".join(summary_lines)
|
936 |
-
logging.info(f"๊ฒ์ ๊ฒฐ๊ณผ {len(limited_organic)}๊ฐ ์ฒ๋ฆฌ ์๋ฃ")
|
937 |
-
return search_results
|
938 |
-
|
939 |
-
except requests.exceptions.Timeout:
|
940 |
-
logging.error("Web search timed out, ์ง์ ๊ตฌ๊ธ ๊ฒ์์ผ๋ก ์ ํํฉ๋๋ค.")
|
941 |
-
return do_google_search(query)
|
942 |
-
except Exception as e:
|
943 |
-
logging.error(f"Web search failed: {e}, ์ง์ ๊ตฌ๊ธ ๊ฒ์์ผ๋ก ์ ํํฉ๋๋ค.")
|
944 |
-
return do_google_search(query)
|
945 |
-
|
946 |
-
def chatbot_interface():
|
947 |
-
st.title("Ginigen Blog")
|
948 |
-
|
949 |
-
# ๋ชจ๋ธ ๊ณ ์ ์ค์
|
950 |
-
if "ai_model" not in st.session_state:
|
951 |
-
st.session_state["ai_model"] = "claude-3-7-sonnet-20250219"
|
952 |
-
|
953 |
-
# ์ธ์
์ํ ์ด๊ธฐํ
|
954 |
-
if "messages" not in st.session_state:
|
955 |
-
st.session_state.messages = []
|
956 |
-
|
957 |
-
# ์๋ ์ ์ฅ ๊ธฐ๋ฅ
|
958 |
-
if "auto_save" not in st.session_state:
|
959 |
-
st.session_state.auto_save = True
|
960 |
-
|
961 |
-
# ์ด๋ฏธ์ง ์์ฑ ํ ๊ธ
|
962 |
-
if "generate_image" not in st.session_state:
|
963 |
-
st.session_state.generate_image = False
|
964 |
-
|
965 |
-
# ์น ๊ฒ์ ํ ๊ธ
|
966 |
-
if "use_web_search" not in st.session_state:
|
967 |
-
st.session_state.use_web_search = False
|
968 |
-
|
969 |
-
# ์ด๋ฏธ์ง API ์ํ
|
970 |
-
if "image_api_status" not in st.session_state:
|
971 |
-
st.session_state.image_api_status = test_image_api_connection()
|
972 |
-
|
973 |
-
# ๋ํ ๊ธฐ๋ก ๊ด๋ฆฌ (์ฌ์ด๋๋ฐ)
|
974 |
-
st.sidebar.title("๋ํ ๊ธฐ๋ก ๊ด๋ฆฌ")
|
975 |
-
|
976 |
-
# ์๋ ์ ์ฅ ํ ๊ธ
|
977 |
-
st.session_state.auto_save = st.sidebar.toggle("์๋ ์ ์ฅ", value=st.session_state.auto_save)
|
978 |
-
|
979 |
-
# ์ด๋ฏธ์ง ์์ฑ ํ ๊ธ
|
980 |
-
st.session_state.generate_image = st.sidebar.toggle("๋ธ๋ก๊ทธ ๊ธ ์์ฑ ํ ์ด๋ฏธ์ง ์๋ ์์ฑ", value=st.session_state.generate_image)
|
981 |
-
|
982 |
-
# ์น ๊ฒ์ ํ ๊ธ
|
983 |
-
st.session_state.use_web_search = st.sidebar.toggle("์ฃผ์ ์น ๊ฒ์ ๋ฐ ๋ถ์", value=st.session_state.use_web_search)
|
984 |
-
|
985 |
-
# ์ด๋ฏธ์ง API ์ํ ํ์
|
986 |
-
st.sidebar.text(st.session_state.image_api_status)
|
987 |
-
|
988 |
-
# ์ด๋ฏธ์ง ์์ฑ ์ค์ (ํ ๊ธ์ด ์ผ์ ธ ์์ ๋๋ง ํ์)
|
989 |
-
if st.session_state.generate_image:
|
990 |
-
st.sidebar.subheader("์ด๋ฏธ์ง ์์ฑ ์ค์ ")
|
991 |
-
width = st.sidebar.slider("๋๋น", 256, 1024, 768, 64)
|
992 |
-
height = st.sidebar.slider("๋์ด", 256, 1024, 768, 64)
|
993 |
-
guidance = st.sidebar.slider("๊ฐ์ด๋์ค ์ค์ผ์ผ", 1.0, 20.0, 3.5, 0.1)
|
994 |
-
inference_steps = st.sidebar.slider("์ธํผ๋ฐ์ค ์คํ
", 1, 50, 30, 1)
|
995 |
-
seed = st.sidebar.number_input("์๋", value=3, min_value=0, step=1)
|
996 |
-
else:
|
997 |
-
# ๊ธฐ๋ณธ๊ฐ ์ค์
|
998 |
-
width, height, guidance, inference_steps, seed = 768, 768, 3.5, 30, 3
|
999 |
-
|
1000 |
-
# ๋ธ๋ก๊ทธ ๋ด์ฉ ๋ค์ด๋ก๋ ์น์
|
1001 |
-
st.sidebar.title("๋ธ๋ก๊ทธ ๋ค์ด๋ก๋")
|
1002 |
-
|
1003 |
-
# ์ต์ ๋ธ๋ก๊ทธ ๋ด์ฉ ๊ฐ์ ธ์ค๊ธฐ
|
1004 |
-
latest_blog = None
|
1005 |
-
latest_blog_title = "๋ธ๋ก๊ทธ ๊ธ"
|
1006 |
-
|
1007 |
-
if len(st.session_state.messages) > 0:
|
1008 |
-
# ๊ฐ์ฅ ์ต๊ทผ assistant ๋ฉ์์ง ์ฐพ๊ธฐ
|
1009 |
-
for msg in reversed(st.session_state.messages):
|
1010 |
-
if msg["role"] == "assistant" and msg["content"].strip():
|
1011 |
-
latest_blog = msg["content"]
|
1012 |
-
|
1013 |
-
# ํ์ดํ ์ถ์ถ ์๋ (์ฒซ ๋ฒ์งธ ์ ๋ชฉ ํ๊ทธ ์ฌ์ฉ)
|
1014 |
-
title_match = re.search(r'# (.*?)(\n|$)', latest_blog)
|
1015 |
-
if title_match:
|
1016 |
-
latest_blog_title = title_match.group(1).strip()
|
1017 |
-
# ์ฌ์ฉ์ ์
๋ ฅ์ ํ์ดํ๋ก ์ฌ์ฉ
|
1018 |
-
elif len(st.session_state.messages) >= 2:
|
1019 |
-
for i in range(len(st.session_state.messages)-1, -1, -1):
|
1020 |
-
if st.session_state.messages[i]["role"] == "user":
|
1021 |
-
latest_blog_title = st.session_state.messages[i]["content"][:30].strip()
|
1022 |
-
if len(st.session_state.messages[i]["content"]) > 30:
|
1023 |
-
latest_blog_title += "..."
|
1024 |
-
break
|
1025 |
-
break
|
1026 |
-
|
1027 |
-
# ๋ค์ด๋ก๋ ๋ฒํผ ๊ทธ๋ฃน
|
1028 |
-
if latest_blog:
|
1029 |
-
st.sidebar.subheader("์ต๊ทผ ๋ธ๋ก๊ทธ ๋ค์ด๋ก๋")
|
1030 |
-
|
1031 |
-
col1, col2 = st.sidebar.columns(2)
|
1032 |
-
|
1033 |
-
# ๋งํฌ๋ค์ด์ผ๋ก ๋ค์ด๋ก๋
|
1034 |
-
with col1:
|
1035 |
-
st.download_button(
|
1036 |
-
label="๋งํฌ๋ค์ด",
|
1037 |
-
data=latest_blog,
|
1038 |
-
file_name=f"{latest_blog_title}.md",
|
1039 |
-
mime="text/markdown"
|
1040 |
-
)
|
1041 |
-
|
1042 |
-
# HTML๋ก ๋ค์ด๋ก๋
|
1043 |
-
with col2:
|
1044 |
-
html_content = convert_md_to_html(latest_blog, latest_blog_title)
|
1045 |
-
st.download_button(
|
1046 |
-
label="HTML",
|
1047 |
-
data=html_content,
|
1048 |
-
file_name=f"{latest_blog_title}.html",
|
1049 |
-
mime="text/html"
|
1050 |
-
)
|
1051 |
-
|
1052 |
-
# ๋ํ ๊ธฐ๋ก ๋ถ๋ฌ์ค๊ธฐ
|
1053 |
-
uploaded_file = st.sidebar.file_uploader("๋ํ ๊ธฐ๋ก ๋ถ๋ฌ์ค๊ธฐ", type=['json'])
|
1054 |
-
if uploaded_file is not None:
|
1055 |
-
try:
|
1056 |
-
content = uploaded_file.getvalue().decode()
|
1057 |
-
if content.strip():
|
1058 |
-
st.session_state.messages = json.loads(content)
|
1059 |
-
st.sidebar.success("๋ํ ๊ธฐ๋ก์ ์ฑ๊ณต์ ์ผ๋ก ๋ถ๋ฌ์์ต๋๋ค!")
|
1060 |
-
else:
|
1061 |
-
st.sidebar.warning("์
๋ก๋๋ ํ์ผ์ด ๋น์ด ์์ต๋๋ค.")
|
1062 |
-
except json.JSONDecodeError:
|
1063 |
-
st.sidebar.error("์ฌ๋ฐ๋ฅธ JSON ํ์์ ํ์ผ์ด ์๋๋๋ค.")
|
1064 |
-
except Exception as e:
|
1065 |
-
st.sidebar.error(f"ํ์ผ ์ฒ๋ฆฌ ์ค ์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค: {str(e)}")
|
1066 |
-
|
1067 |
-
# ๋ํ ๊ธฐ๋ก ์ด๊ธฐํ ๋ฒํผ
|
1068 |
-
if st.sidebar.button("๋ํ ๊ธฐ๋ก ์ด๊ธฐํ"):
|
1069 |
-
st.session_state.messages = []
|
1070 |
-
st.sidebar.success("๋ํ ๊ธฐ๋ก์ด ์ด๊ธฐํ๋์์ต๋๋ค.")
|
1071 |
-
|
1072 |
-
# ๋ฉ์์ง ํ์
|
1073 |
-
for message in st.session_state.messages:
|
1074 |
-
with st.chat_message(message["role"]):
|
1075 |
-
st.markdown(message["content"])
|
1076 |
-
# ์ด๋ฏธ์ง๊ฐ ์๋ ๊ฒฝ์ฐ ํ์
|
1077 |
-
if "image" in message:
|
1078 |
-
st.image(message["image"], caption=message.get("image_caption", "์์ฑ๋ ์ด๋ฏธ์ง"))
|
1079 |
-
|
1080 |
-
# ์ฌ์ฉ์ ์
๋ ฅ
|
1081 |
-
if prompt := st.chat_input("๋ฌด์์ ๋์๋๋ฆด๊น์?"):
|
1082 |
-
st.session_state.messages.append({"role": "user", "content": prompt})
|
1083 |
-
with st.chat_message("user"):
|
1084 |
-
st.markdown(prompt)
|
1085 |
-
|
1086 |
-
# AI ์๋ต ์์ฑ
|
1087 |
-
with st.chat_message("assistant"):
|
1088 |
-
message_placeholder = st.empty()
|
1089 |
-
full_response = ""
|
1090 |
-
|
1091 |
-
# ์น ๊ฒ์ ์ํ (์น ๊ฒ์ ์ต์
์ด ์ผ์ ธ ์์ ๊ฒฝ์ฐ)
|
1092 |
-
system_prompt = get_system_prompt()
|
1093 |
-
if st.session_state.use_web_search:
|
1094 |
-
with st.spinner("์น์์ ๊ด๋ จ ์ ๋ณด๋ฅผ ๊ฒ์ ์ค..."):
|
1095 |
-
try:
|
1096 |
-
search_query = extract_keywords(prompt, top_k=5)
|
1097 |
-
st.info(f"๊ฒ์์ด: {search_query}")
|
1098 |
-
|
1099 |
-
# ๋ ๊ฐ์ง ๋ฐฉ๋ฒ ๋ชจ๋ ์๋ (SerpHouse API์ ์ง์ ๊ฒ์)
|
1100 |
-
search_results = do_web_search(search_query)
|
1101 |
-
|
1102 |
-
if "๊ฐ์ ๊ฒ์ ๊ฒฐ๊ณผ" in search_results:
|
1103 |
-
st.warning("์ค์ ๊ฒ์ ๊ฒฐ๊ณผ๋ฅผ ๊ฐ์ ธ์ฌ ์ ์์ด ๊ธฐ์กด ์ง์์ ํ์ฉํฉ๋๋ค.")
|
1104 |
-
else:
|
1105 |
-
st.success(f"๊ฒ์ ์๋ฃ: '{search_query}'์ ๋ํ ์ ๋ณด๋ฅผ ์์งํ์ต๋๋ค.")
|
1106 |
-
|
1107 |
-
# ์์คํ
ํ๋กฌํํธ์ ๊ฒ์ ๊ฒฐ๊ณผ ์ถ๊ฐ
|
1108 |
-
system_prompt += f"\n\n๊ฒ์ ๊ฒฐ๊ณผ:\n{search_results}\n"
|
1109 |
-
except Exception as e:
|
1110 |
-
st.error(f"์น ๊ฒ์ ์ค ์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค: {str(e)}")
|
1111 |
-
logging.error(f"์น ๊ฒ์ ์ค๋ฅ: {str(e)}")
|
1112 |
-
system_prompt += "\n\n์น ๊ฒ์์ด ์คํจํ์ต๋๋ค. ๊ธฐ์กด ์ง์์ ๋ฐํ์ผ๋ก ๋ต๋ณํ์ธ์."
|
1113 |
-
|
1114 |
-
# API ํธ์ถ
|
1115 |
-
with client.messages.stream(
|
1116 |
-
max_tokens=MAX_TOKENS,
|
1117 |
-
system=system_prompt,
|
1118 |
-
messages=[{"role": m["role"], "content": m["content"]} for m in st.session_state.messages],
|
1119 |
-
model=st.session_state["ai_model"]
|
1120 |
-
) as stream:
|
1121 |
-
for text in stream.text_stream:
|
1122 |
-
full_response += str(text) if text is not None else ""
|
1123 |
-
message_placeholder.markdown(full_response + "โ")
|
1124 |
-
|
1125 |
-
message_placeholder.markdown(full_response)
|
1126 |
-
|
1127 |
-
# ์ด๋ฏธ์ง ์์ฑ ์ต์
์ด ์ผ์ ธ ์๋ ๊ฒฝ์ฐ
|
1128 |
-
if st.session_state.generate_image:
|
1129 |
-
with st.spinner("๋ธ๋ก๊ทธ์ ๋ง๋ ์ด๋ฏธ์ง ์์ฑ ์ค..."):
|
1130 |
-
# ์ด๋ฏธ์ง ํ๋กฌํํธ ์์ฑ
|
1131 |
-
image_prompt = extract_image_prompt(full_response, prompt)
|
1132 |
-
|
1133 |
-
# ์ด๋ฏธ์ง ์์ฑ
|
1134 |
-
image, image_caption = generate_image(
|
1135 |
-
image_prompt,
|
1136 |
-
width=width,
|
1137 |
-
height=height,
|
1138 |
-
guidance=guidance,
|
1139 |
-
inference_steps=inference_steps,
|
1140 |
-
seed=seed
|
1141 |
-
)
|
1142 |
-
|
1143 |
-
if image:
|
1144 |
-
st.image(image, caption=image_caption)
|
1145 |
-
# ์ด๋ฏธ์ง ์ ๋ณด๋ฅผ ์๋ต์ ํฌํจ
|
1146 |
-
st.session_state.messages.append({
|
1147 |
-
"role": "assistant",
|
1148 |
-
"content": full_response,
|
1149 |
-
"image": image,
|
1150 |
-
"image_caption": image_caption
|
1151 |
-
})
|
1152 |
-
else:
|
1153 |
-
st.error(f"์ด๋ฏธ์ง ์์ฑ ์คํจ: {image_caption}")
|
1154 |
-
st.session_state.messages.append({
|
1155 |
-
"role": "assistant",
|
1156 |
-
"content": full_response
|
1157 |
-
})
|
1158 |
-
else:
|
1159 |
-
# ์ด๋ฏธ์ง ์์ฑ ์์ด ์๋ต๋ง ์ ์ฅ
|
1160 |
-
st.session_state.messages.append({
|
1161 |
-
"role": "assistant",
|
1162 |
-
"content": full_response
|
1163 |
-
})
|
1164 |
-
|
1165 |
-
# ๋ธ๋ก๊ทธ ๋ค์ด๋ก๋ ๋ฒํผ ํ์ (์๋ต ๋ฐ๋ก ์๋์)
|
1166 |
-
st.subheader("์ด ๋ธ๋ก๊ทธ ๋ค์ด๋ก๋:")
|
1167 |
-
col1, col2 = st.columns(2)
|
1168 |
-
|
1169 |
-
with col1:
|
1170 |
-
st.download_button(
|
1171 |
-
label="๋งํฌ๋ค์ด์ผ๋ก ์ ์ฅ",
|
1172 |
-
data=full_response,
|
1173 |
-
file_name=f"{prompt[:30]}.md",
|
1174 |
-
mime="text/markdown"
|
1175 |
-
)
|
1176 |
-
|
1177 |
-
with col2:
|
1178 |
-
html_content = convert_md_to_html(full_response, prompt[:30])
|
1179 |
-
st.download_button(
|
1180 |
-
label="HTML๋ก ์ ์ฅ",
|
1181 |
-
data=html_content,
|
1182 |
-
file_name=f"{prompt[:30]}.html",
|
1183 |
-
mime="text/html"
|
1184 |
-
)
|
1185 |
-
|
1186 |
-
# ์๋ ์ ์ฅ ๊ธฐ๋ฅ
|
1187 |
-
if st.session_state.auto_save:
|
1188 |
-
try:
|
1189 |
-
# ์ด๋ฏธ์ง ์ ๋ณด๋ ์ ์ฅํ์ง ์์ (JSON์๋ ๋ฐ์ด๋๋ฆฌ ๋ฐ์ดํฐ๋ฅผ ์ง์ ์ ์ฅํ ์ ์์)
|
1190 |
-
save_messages = []
|
1191 |
-
for msg in st.session_state.messages:
|
1192 |
-
save_msg = {"role": msg["role"], "content": msg["content"]}
|
1193 |
-
save_messages.append(save_msg)
|
1194 |
-
|
1195 |
-
# ํ์ฌ ์๊ฐ์ ํฌํจํ ํ์ผ๋ช
์์ฑ
|
1196 |
-
current_time = datetime.now().strftime("%Y%m%d_%H%M%S")
|
1197 |
-
filename = f'chat_history_auto_save_{current_time}.json'
|
1198 |
-
|
1199 |
-
with open(filename, 'w', encoding='utf-8') as f:
|
1200 |
-
json.dump(save_messages, f, ensure_ascii=False, indent=4)
|
1201 |
-
except Exception as e:
|
1202 |
-
st.sidebar.error(f"์๋ ์ ์ฅ ์ค ์ค๋ฅ ๋ฐ์: {str(e)}")
|
1203 |
-
|
1204 |
-
# ๋ํ ๊ธฐ๋ก ๋ค์ด๋ก๋
|
1205 |
-
if st.sidebar.button("๋ํ ๊ธฐ๋ก ๋ค์ด๋ก๋"):
|
1206 |
-
# ์ด๋ฏธ์ง ์ ๋ณด๋ ์ ์ฅํ์ง ์์
|
1207 |
-
save_messages = []
|
1208 |
-
for msg in st.session_state.messages:
|
1209 |
-
save_msg = {"role": msg["role"], "content": msg["content"]}
|
1210 |
-
save_messages.append(save_msg)
|
1211 |
-
|
1212 |
-
json_history = json.dumps(save_messages, indent=4, ensure_ascii=False)
|
1213 |
-
st.sidebar.download_button(
|
1214 |
-
label="๋ํ ๊ธฐ๋ก ์ ์ฅํ๊ธฐ",
|
1215 |
-
data=json_history,
|
1216 |
-
file_name="chat_history.json",
|
1217 |
-
mime="application/json"
|
1218 |
-
)
|
1219 |
-
|
1220 |
-
def main():
|
1221 |
-
chatbot_interface()
|
1222 |
-
|
1223 |
if __name__ == "__main__":
|
1224 |
# requirements.txt ํ์ผ ์์ฑ
|
1225 |
with open("requirements.txt", "w") as f:
|
|
|
604 |
def main():
|
605 |
chatbot_interface()
|
606 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
607 |
if __name__ == "__main__":
|
608 |
# requirements.txt ํ์ผ ์์ฑ
|
609 |
with open("requirements.txt", "w") as f:
|