Spaces:
Running
Running
Update app-BACKUP2.py
Browse files- app-BACKUP2.py +163 -0
app-BACKUP2.py
CHANGED
|
@@ -5,6 +5,10 @@ import anthropic
|
|
| 5 |
import requests
|
| 6 |
import logging
|
| 7 |
from gradio_client import Client
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
# ๋ก๊น
์ค์
|
| 10 |
logging.basicConfig(
|
|
@@ -108,6 +112,61 @@ def extract_image_prompt(blog_content, blog_topic):
|
|
| 108 |
logging.error(f"์ด๋ฏธ์ง ํ๋กฌํํธ ์์ฑ ์ค๋ฅ: {e}")
|
| 109 |
return f"A professional photo related to {blog_topic}, detailed, high quality"
|
| 110 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 111 |
def chatbot_interface():
|
| 112 |
st.title("Ginigen Blog")
|
| 113 |
|
|
@@ -155,6 +214,73 @@ def chatbot_interface():
|
|
| 155 |
# ๊ธฐ๋ณธ๊ฐ ์ค์
|
| 156 |
width, height, guidance, inference_steps, seed = 768, 768, 3.5, 30, 3
|
| 157 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 158 |
# ๋ํ ๊ธฐ๋ก ๋ถ๋ฌ์ค๊ธฐ
|
| 159 |
uploaded_file = st.sidebar.file_uploader("๋ํ ๊ธฐ๋ก ๋ถ๋ฌ์ค๊ธฐ", type=['json'])
|
| 160 |
if uploaded_file is not None:
|
|
@@ -244,6 +370,40 @@ def chatbot_interface():
|
|
| 244 |
"role": "assistant",
|
| 245 |
"content": full_response
|
| 246 |
})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 247 |
|
| 248 |
# ์๋ ์ ์ฅ ๊ธฐ๋ฅ
|
| 249 |
if st.session_state.auto_save:
|
|
@@ -285,5 +445,8 @@ if __name__ == "__main__":
|
|
| 285 |
f.write("anthropic>=0.18.1\n")
|
| 286 |
f.write("gradio-client>=1.8.0\n")
|
| 287 |
f.write("requests>=2.32.3\n")
|
|
|
|
|
|
|
|
|
|
| 288 |
|
| 289 |
main()
|
|
|
|
| 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(
|
|
|
|
| 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 |
|
|
|
|
| 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:
|
|
|
|
| 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:
|
|
|
|
| 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()
|