ginipick commited on
Commit
7046eff
ยท
verified ยท
1 Parent(s): eaf1509

Update app-BACKUP2.py

Browse files
Files changed (1) hide show
  1. 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()