ginipick commited on
Commit
1df6810
ยท
verified ยท
1 Parent(s): 1033bb6

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +452 -0
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()