Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,452 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import streamlit as st
|
3 |
+
import json
|
4 |
+
import anthropic
|
5 |
+
import requests
|
6 |
+
import logging
|
7 |
+
from gradio_client import Client
|
8 |
+
import markdown
|
9 |
+
import tempfile
|
10 |
+
import base64
|
11 |
+
from weasyprint import HTML
|
12 |
+
|
13 |
+
# ๋ก๊น
์ค์
|
14 |
+
logging.basicConfig(
|
15 |
+
level=logging.INFO,
|
16 |
+
format='%(asctime)s - %(levelname)s - %(message)s')
|
17 |
+
|
18 |
+
# API ์ค์
|
19 |
+
api_key = os.environ.get("API_KEY")
|
20 |
+
client = anthropic.Anthropic(api_key=api_key)
|
21 |
+
|
22 |
+
# ์ด๋ฏธ์ง ์์ฑ API URL
|
23 |
+
IMAGE_API_URL = "http://211.233.58.201:7896"
|
24 |
+
|
25 |
+
# ์ต๋ ํ ํฐ ์ ์ค์ (Claude-3 Sonnet์ ์ต๋ ํ ํฐ ์)
|
26 |
+
MAX_TOKENS = 7999
|
27 |
+
|
28 |
+
def get_system_prompt():
|
29 |
+
return """
|
30 |
+
๋น์ ์ ์ ๋ฌธ ๋ธ๋ก๊ทธ ์์ฑ ์ ๋ฌธ๊ฐ์
๋๋ค. ๋ชจ๋ ๋ธ๋ก๊ทธ ๊ธ ์์ฑ ์์ฒญ์ ๋ํด ๋ค์์ 8๋จ๊ณ ํ๋ ์์ํฌ๋ฅผ ์ฒ ์ ํ ๋ฐ๋ฅด๋, ์์ฐ์ค๋ฝ๊ณ ๋งค๋ ฅ์ ์ธ ๊ธ์ด ๋๋๋ก ์์ฑํด์ผ ํฉ๋๋ค:
|
31 |
+
|
32 |
+
๋
์ ์ฐ๊ฒฐ ๋จ๊ณ 1.1. ๊ณต๊ฐ๋ ํ์ฑ์ ์ํ ์น๊ทผํ ์ธ์ฌ 1.2. ๋
์์ ์ค์ ๊ณ ๋ฏผ์ ๋ฐ์ํ ๋์
์ง๋ฌธ 1.3. ์ฃผ์ ์ ๋ํ ์ฆ๊ฐ์ ๊ด์ฌ ์ ๋
|
33 |
+
|
34 |
+
๋ฌธ์ ์ ์ ๋จ๊ณ 2.1. ๋
์์ ํ์ธํฌ์ธํธ ๊ตฌ์ฒดํ 2.2. ๋ฌธ์ ์ ์๊ธ์ฑ๊ณผ ์ํฅ๋ ๋ถ์ 2.3. ํด๊ฒฐ ํ์์ฑ์ ๋ํ ๊ณต๊ฐ๋ ํ์ฑ
|
35 |
+
|
36 |
+
์ ๋ฌธ์ฑ ์
์ฆ ๋จ๊ณ 3.1. ๊ฐ๊ด์ ๋ฐ์ดํฐ ๊ธฐ๋ฐ ๋ถ์ 3.2. ์ ๋ฌธ๊ฐ ๊ฒฌํด์ ์ฐ๊ตฌ ๊ฒฐ๊ณผ ์ธ์ฉ 3.3. ์ค์ ์ฌ๋ก๋ฅผ ํตํ ๋ฌธ์ ๊ตฌ์ฒดํ
|
37 |
+
|
38 |
+
์๋ฃจ์
์ ๊ณต ๋จ๊ณ 4.1. ๋จ๊ณ๋ณ ์ค์ฒ ๊ฐ์ด๋๋ผ์ธ ์ ์ 4.2. ์ฆ์ ์ ์ฉ ๊ฐ๋ฅํ ๊ตฌ์ฒด์ ํ 4.3. ์์ ์ฅ์ ๋ฌผ๊ณผ ๊ทน๋ณต ๋ฐฉ์ ํฌํจ
|
39 |
+
|
40 |
+
์ ๋ขฐ๋ ๊ฐํ ๋จ๊ณ 5.1. ์ค์ ์ฑ๊ณต ์ฌ๋ก ์ ์ 5.2. ๊ตฌ์ฒด์ ์ฌ์ฉ์ ํ๊ธฐ ์ธ์ฉ 5.3. ๊ฐ๊ด์ ๋ฐ์ดํฐ๋ก ํจ๊ณผ ์
์ฆ
|
41 |
+
|
42 |
+
ํ๋ ์ ๋ ๋จ๊ณ 6.1. ๋ช
ํํ ์ฒซ ์ค์ฒ ๋จ๊ณ ์ ์ 6.2. ์๊ธ์ฑ์ ๊ฐ์กฐํ ํ๋ ์ด๊ตฌ 6.3. ์ค์ฒ ๋๊ธฐ ๋ถ์ฌ ์์ ํฌํจ
|
43 |
+
|
44 |
+
์ง์ ์ฑ ๊ฐํ ๋จ๊ณ 7.1. ์๋ฃจ์
์ ํ๊ณ ํฌ๋ช
ํ๊ฒ ๊ณต๊ฐ 7.2. ๊ฐ์ธ๋ณ ์ฐจ์ด ์กด์ฌ ์ธ์ 7.3. ํ์ ์กฐ๊ฑด๊ณผ ์ฃผ์์ฌํญ ๋ช
์
|
45 |
+
|
46 |
+
๊ด๊ณ ์ง์ ๋จ๊ณ 8.1. ์ง์ ์ฑ ์๋ ๊ฐ์ฌ ์ธ์ฌ 8.2. ๋ค์ ์ปจํ
์ธ ์๊ณ ๋ก ๊ธฐ๋๊ฐ ์กฐ์ฑ 8.3. ์ํต ์ฑ๋ ์๋ด
|
47 |
+
|
48 |
+
์์ฑ ์ ์ค์์ฌํญ 9.1. ๊ธ์ ์: 1500-2000์ ๋ด์ธ 9.2. ๋ฌธ๋จ ๊ธธ์ด: 3-4๋ฌธ์ฅ ์ด๋ด 9.3. ์๊ฐ์ ๊ตฌ๋ถ: ์์ ๋ชฉ, ๊ตฌ๋ถ์ , ๋ฒํธ ๋ชฉ๋ก ํ์ฉ 9.4. ํค์ค๋งค๋: ์น๊ทผํ๊ณ ์ ๋ฌธ์ ์ธ ๋ํ์ฒด 9.5. ๋ฐ์ดํฐ: ๋ชจ๋ ์ ๋ณด์ ์ถ์ฒ ๋ช
์ 9.6. ๊ฐ๋
์ฑ: ๋ช
ํํ ๋จ๋ฝ ๊ตฌ๋ถ๊ณผ ๊ฐ์กฐ์ ์ฌ์ฉ
|
49 |
+
|
50 |
+
์ด๋ฌํ ํ๋ ์์ํฌ๋ฅผ ๋ฐํ์ผ๋ก, ์์ฒญ๋ฐ์ ์ฃผ์ ์ ๋ํด ์ฒด๊ณ์ ์ด๊ณ ๋งค๋ ฅ์ ์ธ ๋ธ๋ก๊ทธ ํฌ์คํธ๋ฅผ ์์ฑํ๊ฒ ์ต๋๋ค.
|
51 |
+
"""
|
52 |
+
|
53 |
+
def test_image_api_connection():
|
54 |
+
"""์ด๋ฏธ์ง API ์๋ฒ ์ฐ๊ฒฐ ํ
์คํธ"""
|
55 |
+
try:
|
56 |
+
client = Client(IMAGE_API_URL)
|
57 |
+
return "์ด๋ฏธ์ง API ์ฐ๊ฒฐ ์ฑ๊ณต: ์ ์ ์๋ ์ค"
|
58 |
+
except Exception as e:
|
59 |
+
logging.error(f"์ด๋ฏธ์ง API ์ฐ๊ฒฐ ํ
์คํธ ์คํจ: {e}")
|
60 |
+
return f"์ด๋ฏธ์ง API ์ฐ๊ฒฐ ์คํจ: {e}"
|
61 |
+
|
62 |
+
def generate_image(prompt, width=768, height=768, guidance=3.5, inference_steps=30, seed=3):
|
63 |
+
"""์ด๋ฏธ์ง ์์ฑ ํจ์"""
|
64 |
+
if not prompt:
|
65 |
+
return None, "์ค๋ฅ: ํ๋กฌํํธ๋ฅผ ์
๋ ฅํด์ฃผ์ธ์"
|
66 |
+
|
67 |
+
try:
|
68 |
+
client = Client(IMAGE_API_URL)
|
69 |
+
result = client.predict(
|
70 |
+
prompt=prompt,
|
71 |
+
width=int(width),
|
72 |
+
height=int(height),
|
73 |
+
guidance=float(guidance),
|
74 |
+
inference_steps=int(inference_steps),
|
75 |
+
seed=int(seed),
|
76 |
+
do_img2img=False,
|
77 |
+
init_image=None,
|
78 |
+
image2image_strength=0.8,
|
79 |
+
resize_img=True,
|
80 |
+
api_name="/generate_image"
|
81 |
+
)
|
82 |
+
logging.info(f"์ด๋ฏธ์ง ์์ฑ ์ฑ๊ณต: {result[1]}")
|
83 |
+
return result[0], f"์ฌ์ฉ๋ ์๋: {result[1]}"
|
84 |
+
except Exception as e:
|
85 |
+
logging.error(f"์ด๋ฏธ์ง ์์ฑ ์คํจ: {str(e)}")
|
86 |
+
return None, f"์ค๋ฅ: {str(e)}"
|
87 |
+
|
88 |
+
def extract_image_prompt(blog_content, blog_topic):
|
89 |
+
"""๋ธ๋ก๊ทธ ๋ด์ฉ์์ ์ด๋ฏธ์ง ์์ฑ์ ์ํ ํ๋กฌํํธ ์ถ์ถ"""
|
90 |
+
image_prompt_system = f"""
|
91 |
+
๋ค์์ '{blog_topic}'์ ๊ดํ ๋ธ๋ก๊ทธ ๊ธ์
๋๋ค. ์ด ๋ธ๋ก๊ทธ ๊ธ์ ๋ด์ฉ์ ๊ธฐ๋ฐ์ผ๋ก ์ ์ ํ ์ด๋ฏธ์ง๋ฅผ ์์ฑํ๊ธฐ ์ํ
|
92 |
+
ํ๋กฌํํธ๋ฅผ ์์ฑํด์ฃผ์ธ์. ํ๋กฌํํธ๋ ์์ด๋ก ์์ฑํ๊ณ , ๊ตฌ์ฒด์ ์ธ ์๊ฐ์ ์์๋ฅผ ๋ด์์ผ ํฉ๋๋ค.
|
93 |
+
ํ๋กฌํํธ๋ง ๋ฐํํ์ธ์(๋ค๋ฅธ ์ค๋ช
์์ด).
|
94 |
+
|
95 |
+
์์ ํ์:
|
96 |
+
"A professional photo of [subject], [specific details], [atmosphere], [lighting], [perspective], high quality, detailed"
|
97 |
+
"""
|
98 |
+
|
99 |
+
try:
|
100 |
+
response = client.messages.create(
|
101 |
+
model="claude-3-7-sonnet-20250219",
|
102 |
+
max_tokens=150,
|
103 |
+
system=image_prompt_system,
|
104 |
+
messages=[{"role": "user", "content": blog_content}]
|
105 |
+
)
|
106 |
+
|
107 |
+
# ์๋ต์์ ํ๋กฌํํธ ์ถ์ถ
|
108 |
+
image_prompt = response.content[0].text.strip()
|
109 |
+
logging.info(f"์์ฑ๋ ์ด๋ฏธ์ง ํ๋กฌํํธ: {image_prompt}")
|
110 |
+
return image_prompt
|
111 |
+
except Exception as e:
|
112 |
+
logging.error(f"์ด๋ฏธ์ง ํ๋กฌ๏ฟฝ๏ฟฝํธ ์์ฑ ์ค๋ฅ: {e}")
|
113 |
+
return f"A professional photo related to {blog_topic}, detailed, high quality"
|
114 |
+
|
115 |
+
# ๋งํฌ๋ค์ด์ HTML๋ก ๋ณํํ๋ ํจ์
|
116 |
+
def convert_md_to_html(md_text, title="Ginigen Blog"):
|
117 |
+
html_content = markdown.markdown(md_text)
|
118 |
+
html_doc = f"""
|
119 |
+
<!DOCTYPE html>
|
120 |
+
<html>
|
121 |
+
<head>
|
122 |
+
<title>{title}</title>
|
123 |
+
<meta charset="utf-8">
|
124 |
+
<style>
|
125 |
+
body {{ font-family: Arial, sans-serif; line-height: 1.6; max-width: 800px; margin: 0 auto; padding: 20px; }}
|
126 |
+
h1 {{ color: #2c3e50; font-size: 2.5em; margin-bottom: 20px; }}
|
127 |
+
h2 {{ color: #3498db; margin-top: 25px; font-size: 1.8em; }}
|
128 |
+
h3 {{ color: #2980b9; font-size: 1.5em; }}
|
129 |
+
p {{ margin-bottom: 15px; font-size: 1.1em; }}
|
130 |
+
blockquote {{ background: #f9f9f9; border-left: 10px solid #ccc; margin: 1.5em 10px; padding: 1em 10px; }}
|
131 |
+
ul, ol {{ margin-bottom: 15px; }}
|
132 |
+
li {{ margin-bottom: 5px; }}
|
133 |
+
hr {{ border: 0; height: 1px; background: #ddd; margin: 20px 0; }}
|
134 |
+
img {{ max-width: 100%; height: auto; display: block; margin: 20px auto; }}
|
135 |
+
</style>
|
136 |
+
</head>
|
137 |
+
<body>
|
138 |
+
{html_content}
|
139 |
+
</body>
|
140 |
+
</html>
|
141 |
+
"""
|
142 |
+
return html_doc
|
143 |
+
|
144 |
+
# ๋งํฌ๋ค์ด์ PDF๋ก ๋ณํํ๋ ํจ์
|
145 |
+
def convert_md_to_pdf(md_text, title="Ginigen Blog"):
|
146 |
+
html_content = convert_md_to_html(md_text, title)
|
147 |
+
|
148 |
+
# ์์ ํ์ผ ์์ฑ
|
149 |
+
with tempfile.NamedTemporaryFile(suffix='.pdf', delete=False) as tmp:
|
150 |
+
tmp_path = tmp.name
|
151 |
+
|
152 |
+
# HTML์ PDF๋ก ๋ณํ
|
153 |
+
HTML(string=html_content).write_pdf(tmp_path)
|
154 |
+
|
155 |
+
# ์์ฑ๋ PDF ์ฝ๊ธฐ
|
156 |
+
with open(tmp_path, 'rb') as f:
|
157 |
+
pdf_data = f.read()
|
158 |
+
|
159 |
+
# ์์ ํ์ผ ์ญ์
|
160 |
+
os.unlink(tmp_path)
|
161 |
+
|
162 |
+
return pdf_data
|
163 |
+
|
164 |
+
# ๋ค์ด๋ก๋ ๋ฒํผ ์์ฑ ํฌํผ ํจ์
|
165 |
+
def create_download_link(bin_data, download_filename, link_text):
|
166 |
+
b64 = base64.b64encode(bin_data).decode()
|
167 |
+
href = f'<a href="data:application/octet-stream;base64,{b64}" download="{download_filename}">{link_text}</a>'
|
168 |
+
return href
|
169 |
+
|
170 |
+
def chatbot_interface():
|
171 |
+
st.title("Ginigen Blog")
|
172 |
+
|
173 |
+
# ๋ชจ๋ธ ๊ณ ์ ์ค์
|
174 |
+
if "ai_model" not in st.session_state:
|
175 |
+
st.session_state["ai_model"] = "claude-3-7-sonnet-20250219"
|
176 |
+
|
177 |
+
# ์ธ์
์ํ ์ด๊ธฐํ
|
178 |
+
if "messages" not in st.session_state:
|
179 |
+
st.session_state.messages = []
|
180 |
+
|
181 |
+
# ์๋ ์ ์ฅ ๊ธฐ๋ฅ
|
182 |
+
if "auto_save" not in st.session_state:
|
183 |
+
st.session_state.auto_save = True
|
184 |
+
|
185 |
+
# ์ด๋ฏธ์ง ์์ฑ ํ ๊ธ
|
186 |
+
if "generate_image" not in st.session_state:
|
187 |
+
st.session_state.generate_image = False
|
188 |
+
|
189 |
+
# ์ด๋ฏธ์ง API ์ํ
|
190 |
+
if "image_api_status" not in st.session_state:
|
191 |
+
st.session_state.image_api_status = test_image_api_connection()
|
192 |
+
|
193 |
+
# ๋ํ ๊ธฐ๋ก ๊ด๋ฆฌ (์ฌ์ด๋๋ฐ)
|
194 |
+
st.sidebar.title("๋ํ ๊ธฐ๋ก ๊ด๋ฆฌ")
|
195 |
+
|
196 |
+
# ์๋ ์ ์ฅ ํ ๊ธ
|
197 |
+
st.session_state.auto_save = st.sidebar.toggle("์๋ ์ ์ฅ", value=st.session_state.auto_save)
|
198 |
+
|
199 |
+
# ์ด๋ฏธ์ง ์์ฑ ํ ๊ธ
|
200 |
+
st.session_state.generate_image = st.sidebar.toggle("๋ธ๋ก๊ทธ ๊ธ ์์ฑ ํ ์ด๋ฏธ์ง ์๋ ์์ฑ", value=st.session_state.generate_image)
|
201 |
+
|
202 |
+
# ์ด๋ฏธ์ง API ์ํ ํ์
|
203 |
+
st.sidebar.text(st.session_state.image_api_status)
|
204 |
+
|
205 |
+
# ์ด๋ฏธ์ง ์์ฑ ์ค์ (ํ ๊ธ์ด ์ผ์ ธ ์์ ๋๋ง ํ์)
|
206 |
+
if st.session_state.generate_image:
|
207 |
+
st.sidebar.subheader("์ด๋ฏธ์ง ์์ฑ ์ค์ ")
|
208 |
+
width = st.sidebar.slider("๋๋น", 256, 1024, 768, 64)
|
209 |
+
height = st.sidebar.slider("๋์ด", 256, 1024, 768, 64)
|
210 |
+
guidance = st.sidebar.slider("๊ฐ์ด๋์ค ์ค์ผ์ผ", 1.0, 20.0, 3.5, 0.1)
|
211 |
+
inference_steps = st.sidebar.slider("์ธํผ๋ฐ์ค ์คํ
", 1, 50, 30, 1)
|
212 |
+
seed = st.sidebar.number_input("์๋", value=3, min_value=0, step=1)
|
213 |
+
else:
|
214 |
+
# ๊ธฐ๋ณธ๊ฐ ์ค์
|
215 |
+
width, height, guidance, inference_steps, seed = 768, 768, 3.5, 30, 3
|
216 |
+
|
217 |
+
# ๋ธ๋ก๊ทธ ๋ด์ฉ ๋ค์ด๋ก๋ ์น์
|
218 |
+
st.sidebar.title("๋ธ๋ก๊ทธ ๋ค์ด๋ก๋")
|
219 |
+
|
220 |
+
# ์ต์ ๋ธ๋ก๊ทธ ๋ด์ฉ ๊ฐ์ ธ์ค๊ธฐ
|
221 |
+
latest_blog = None
|
222 |
+
latest_blog_title = "๋ธ๋ก๊ทธ ๊ธ"
|
223 |
+
|
224 |
+
if len(st.session_state.messages) > 0:
|
225 |
+
# ๊ฐ์ฅ ์ต๊ทผ assistant ๋ฉ์์ง ์ฐพ๊ธฐ
|
226 |
+
for msg in reversed(st.session_state.messages):
|
227 |
+
if msg["role"] == "assistant" and msg["content"].strip():
|
228 |
+
latest_blog = msg["content"]
|
229 |
+
|
230 |
+
# ํ์ดํ ์ถ์ถ ์๋ (์ฒซ ๋ฒ์งธ ์ ๋ชฉ ํ๊ทธ ์ฌ์ฉ)
|
231 |
+
import re
|
232 |
+
title_match = re.search(r'# (.*?)(\n|$)', latest_blog)
|
233 |
+
if title_match:
|
234 |
+
latest_blog_title = title_match.group(1).strip()
|
235 |
+
# ์ฌ์ฉ์ ์
๋ ฅ์ ํ์ดํ๋ก ์ฌ์ฉ
|
236 |
+
elif len(st.session_state.messages) >= 2:
|
237 |
+
for i in range(len(st.session_state.messages)-1, -1, -1):
|
238 |
+
if st.session_state.messages[i]["role"] == "user":
|
239 |
+
latest_blog_title = st.session_state.messages[i]["content"][:30].strip()
|
240 |
+
if len(st.session_state.messages[i]["content"]) > 30:
|
241 |
+
latest_blog_title += "..."
|
242 |
+
break
|
243 |
+
break
|
244 |
+
|
245 |
+
# ๋ค์ด๋ก๋ ๋ฒํผ ๊ทธ๋ฃน
|
246 |
+
if latest_blog:
|
247 |
+
st.sidebar.subheader("์ต๊ทผ ๋ธ๋ก๊ทธ ๋ค์ด๋ก๋")
|
248 |
+
|
249 |
+
col1, col2, col3 = st.sidebar.columns(3)
|
250 |
+
|
251 |
+
# ๋งํฌ๋ค์ด์ผ๋ก ๋ค์ด๋ก๋
|
252 |
+
with col1:
|
253 |
+
st.download_button(
|
254 |
+
label="๋งํฌ๋ค์ด",
|
255 |
+
data=latest_blog,
|
256 |
+
file_name=f"{latest_blog_title}.md",
|
257 |
+
mime="text/markdown"
|
258 |
+
)
|
259 |
+
|
260 |
+
# HTML๋ก ๋ค์ด๋ก๋
|
261 |
+
with col2:
|
262 |
+
html_content = convert_md_to_html(latest_blog, latest_blog_title)
|
263 |
+
st.download_button(
|
264 |
+
label="HTML",
|
265 |
+
data=html_content,
|
266 |
+
file_name=f"{latest_blog_title}.html",
|
267 |
+
mime="text/html"
|
268 |
+
)
|
269 |
+
|
270 |
+
# PDF๋ก ๋ค์ด๋ก๋
|
271 |
+
with col3:
|
272 |
+
try:
|
273 |
+
pdf_data = convert_md_to_pdf(latest_blog, latest_blog_title)
|
274 |
+
st.download_button(
|
275 |
+
label="PDF",
|
276 |
+
data=pdf_data,
|
277 |
+
file_name=f"{latest_blog_title}.pdf",
|
278 |
+
mime="application/pdf"
|
279 |
+
)
|
280 |
+
except Exception as e:
|
281 |
+
st.error(f"PDF ์์ฑ ์ค๋ฅ: {e}")
|
282 |
+
logging.error(f"PDF ์์ฑ ์ค๋ฅ: {e}")
|
283 |
+
|
284 |
+
# ๋ํ ๊ธฐ๋ก ๋ถ๋ฌ์ค๊ธฐ
|
285 |
+
uploaded_file = st.sidebar.file_uploader("๋ํ ๊ธฐ๋ก ๋ถ๋ฌ์ค๊ธฐ", type=['json'])
|
286 |
+
if uploaded_file is not None:
|
287 |
+
try:
|
288 |
+
content = uploaded_file.getvalue().decode()
|
289 |
+
if content.strip():
|
290 |
+
st.session_state.messages = json.loads(content)
|
291 |
+
st.sidebar.success("๋ํ ๊ธฐ๋ก์ ์ฑ๊ณต์ ์ผ๋ก ๋ถ๋ฌ์์ต๋๋ค!")
|
292 |
+
else:
|
293 |
+
st.sidebar.warning("์
๋ก๋๋ ํ์ผ์ด ๋น์ด ์์ต๋๋ค.")
|
294 |
+
except json.JSONDecodeError:
|
295 |
+
st.sidebar.error("์ฌ๋ฐ๋ฅธ JSON ํ์์ ํ์ผ์ด ์๋๋๋ค.")
|
296 |
+
except Exception as e:
|
297 |
+
st.sidebar.error(f"ํ์ผ ์ฒ๋ฆฌ ์ค ์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค: {str(e)}")
|
298 |
+
|
299 |
+
# ๋ํ ๊ธฐ๋ก ์ด๊ธฐํ ๋ฒํผ
|
300 |
+
if st.sidebar.button("๋ํ ๊ธฐ๋ก ์ด๊ธฐํ"):
|
301 |
+
st.session_state.messages = []
|
302 |
+
st.sidebar.success("๋ํ ๊ธฐ๋ก์ด ์ด๊ธฐํ๋์์ต๋๋ค.")
|
303 |
+
|
304 |
+
# ๋ฉ์์ง ํ์
|
305 |
+
for message in st.session_state.messages:
|
306 |
+
with st.chat_message(message["role"]):
|
307 |
+
st.markdown(message["content"])
|
308 |
+
# ์ด๋ฏธ์ง๊ฐ ์๋ ๊ฒฝ์ฐ ํ์
|
309 |
+
if "image" in message:
|
310 |
+
st.image(message["image"], caption=message.get("image_caption", "์์ฑ๋ ์ด๋ฏธ์ง"))
|
311 |
+
|
312 |
+
# ์ฌ์ฉ์ ์
๋ ฅ
|
313 |
+
if prompt := st.chat_input("๋ฌด์์ ๋์๋๋ฆด๊น์?"):
|
314 |
+
st.session_state.messages.append({"role": "user", "content": prompt})
|
315 |
+
with st.chat_message("user"):
|
316 |
+
st.markdown(prompt)
|
317 |
+
|
318 |
+
# AI ์๋ต ์์ฑ
|
319 |
+
with st.chat_message("assistant"):
|
320 |
+
message_placeholder = st.empty()
|
321 |
+
full_response = ""
|
322 |
+
|
323 |
+
# API ํธ์ถ
|
324 |
+
with client.messages.stream(
|
325 |
+
max_tokens=MAX_TOKENS,
|
326 |
+
system=get_system_prompt(),
|
327 |
+
messages=[{"role": m["role"], "content": m["content"]} for m in st.session_state.messages],
|
328 |
+
model=st.session_state["ai_model"]
|
329 |
+
) as stream:
|
330 |
+
for text in stream.text_stream:
|
331 |
+
full_response += str(text) if text is not None else ""
|
332 |
+
message_placeholder.markdown(full_response + "โ")
|
333 |
+
|
334 |
+
message_placeholder.markdown(full_response)
|
335 |
+
|
336 |
+
# ์ด๋ฏธ์ง ์์ฑ ์ต์
์ด ์ผ์ ธ ์๋ ๊ฒฝ์ฐ
|
337 |
+
if st.session_state.generate_image:
|
338 |
+
with st.spinner("๋ธ๋ก๊ทธ์ ๋ง๋ ์ด๋ฏธ์ง ์์ฑ ์ค..."):
|
339 |
+
# ์ด๋ฏธ์ง ํ๋กฌํํธ ์์ฑ
|
340 |
+
image_prompt = extract_image_prompt(full_response, prompt)
|
341 |
+
|
342 |
+
# ์ด๋ฏธ์ง ์์ฑ
|
343 |
+
image, image_caption = generate_image(
|
344 |
+
image_prompt,
|
345 |
+
width=width,
|
346 |
+
height=height,
|
347 |
+
guidance=guidance,
|
348 |
+
inference_steps=inference_steps,
|
349 |
+
seed=seed
|
350 |
+
)
|
351 |
+
|
352 |
+
if image:
|
353 |
+
st.image(image, caption=image_caption)
|
354 |
+
# ์ด๋ฏธ์ง ์ ๋ณด๋ฅผ ์๋ต์ ํฌํจ
|
355 |
+
st.session_state.messages.append({
|
356 |
+
"role": "assistant",
|
357 |
+
"content": full_response,
|
358 |
+
"image": image,
|
359 |
+
"image_caption": image_caption
|
360 |
+
})
|
361 |
+
else:
|
362 |
+
st.error(f"์ด๋ฏธ์ง ์์ฑ ์คํจ: {image_caption}")
|
363 |
+
st.session_state.messages.append({
|
364 |
+
"role": "assistant",
|
365 |
+
"content": full_response
|
366 |
+
})
|
367 |
+
else:
|
368 |
+
# ์ด๋ฏธ์ง ์์ฑ ์์ด ์๋ต๋ง ์ ์ฅ
|
369 |
+
st.session_state.messages.append({
|
370 |
+
"role": "assistant",
|
371 |
+
"content": full_response
|
372 |
+
})
|
373 |
+
|
374 |
+
# ๋ธ๋ก๊ทธ ๋ค์ด๋ก๋ ๋ฒํผ ํ์ (์๋ต ๋ฐ๋ก ์๋์)
|
375 |
+
st.subheader("์ด ๋ธ๋ก๊ทธ ๋ค์ด๋ก๋:")
|
376 |
+
col1, col2, col3 = st.columns(3)
|
377 |
+
|
378 |
+
with col1:
|
379 |
+
st.download_button(
|
380 |
+
label="๋งํฌ๋ค์ด์ผ๋ก ์ ์ฅ",
|
381 |
+
data=full_response,
|
382 |
+
file_name=f"{prompt[:30]}.md",
|
383 |
+
mime="text/markdown"
|
384 |
+
)
|
385 |
+
|
386 |
+
with col2:
|
387 |
+
html_content = convert_md_to_html(full_response, prompt[:30])
|
388 |
+
st.download_button(
|
389 |
+
label="HTML๋ก ์ ์ฅ",
|
390 |
+
data=html_content,
|
391 |
+
file_name=f"{prompt[:30]}.html",
|
392 |
+
mime="text/html"
|
393 |
+
)
|
394 |
+
|
395 |
+
with col3:
|
396 |
+
try:
|
397 |
+
pdf_data = convert_md_to_pdf(full_response, prompt[:30])
|
398 |
+
st.download_button(
|
399 |
+
label="PDF๋ก ์ ์ฅ",
|
400 |
+
data=pdf_data,
|
401 |
+
file_name=f"{prompt[:30]}.pdf",
|
402 |
+
mime="application/pdf"
|
403 |
+
)
|
404 |
+
except Exception as e:
|
405 |
+
st.error(f"PDF ์์ฑ ์ค๋ฅ: {e}")
|
406 |
+
logging.error(f"PDF ์์ฑ ์ค๋ฅ: {e}")
|
407 |
+
|
408 |
+
# ์๋ ์ ์ฅ ๊ธฐ๋ฅ
|
409 |
+
if st.session_state.auto_save:
|
410 |
+
try:
|
411 |
+
# ์ด๋ฏธ์ง ์ ๋ณด๋ ์ ์ฅํ์ง ์์ (JSON์๋ ๋ฐ์ด๋๋ฆฌ ๋ฐ์ดํฐ๋ฅผ ์ง์ ์ ์ฅํ ์ ์์)
|
412 |
+
save_messages = []
|
413 |
+
for msg in st.session_state.messages:
|
414 |
+
save_msg = {"role": msg["role"], "content": msg["content"]}
|
415 |
+
save_messages.append(save_msg)
|
416 |
+
|
417 |
+
with open('chat_history_auto_save.json', 'w', encoding='utf-8') as f:
|
418 |
+
json.dump(save_messages, f, ensure_ascii=False, indent=4)
|
419 |
+
except Exception as e:
|
420 |
+
st.sidebar.error(f"์๋ ์ ์ฅ ์ค ์ค๋ฅ ๋ฐ์: {str(e)}")
|
421 |
+
|
422 |
+
# ๋ํ ๊ธฐ๋ก ๋ค์ด๋ก๋
|
423 |
+
if st.sidebar.button("๋ํ ๊ธฐ๋ก ๋ค์ด๋ก๋"):
|
424 |
+
# ์ด๋ฏธ์ง ์ ๋ณด๋ ์ ์ฅํ์ง ์์
|
425 |
+
save_messages = []
|
426 |
+
for msg in st.session_state.messages:
|
427 |
+
save_msg = {"role": msg["role"], "content": msg["content"]}
|
428 |
+
save_messages.append(save_msg)
|
429 |
+
|
430 |
+
json_history = json.dumps(save_messages, indent=4, ensure_ascii=False)
|
431 |
+
st.sidebar.download_button(
|
432 |
+
label="๋ํ ๊ธฐ๋ก ์ ์ฅํ๊ธฐ",
|
433 |
+
data=json_history,
|
434 |
+
file_name="chat_history.json",
|
435 |
+
mime="application/json"
|
436 |
+
)
|
437 |
+
|
438 |
+
def main():
|
439 |
+
chatbot_interface()
|
440 |
+
|
441 |
+
if __name__ == "__main__":
|
442 |
+
# requirements.txt ํ์ผ ์์ฑ
|
443 |
+
with open("requirements.txt", "w") as f:
|
444 |
+
f.write("streamlit>=1.31.0\n")
|
445 |
+
f.write("anthropic>=0.18.1\n")
|
446 |
+
f.write("gradio-client>=1.8.0\n")
|
447 |
+
f.write("requests>=2.32.3\n")
|
448 |
+
f.write("markdown>=3.5.1\n")
|
449 |
+
f.write("weasyprint>=60.2\n")
|
450 |
+
f.write("pillow>=10.1.0\n")
|
451 |
+
|
452 |
+
main()
|