Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,294 +1,546 @@
|
|
| 1 |
# ββββββββββββββββββββββββββββββββ Imports ββββββββββββββββββββββββββββββββ
|
| 2 |
-
import os, json, re, logging, requests, markdown, time
|
| 3 |
from datetime import datetime
|
| 4 |
|
| 5 |
import streamlit as st
|
| 6 |
import anthropic
|
| 7 |
from gradio_client import Client
|
| 8 |
-
|
|
|
|
| 9 |
|
| 10 |
-
# ββββββββββββββββββββββββββββββββ
|
| 11 |
ANTHROPIC_KEY = os.getenv("API_KEY", "")
|
| 12 |
-
BRAVE_KEY = os.getenv("SERPHOUSE_API_KEY", "") #
|
| 13 |
BRAVE_ENDPOINT = "https://api.search.brave.com/res/v1/web/search"
|
| 14 |
IMAGE_API_URL = "http://211.233.58.201:7896"
|
| 15 |
MAX_TOKENS = 7_999
|
| 16 |
|
| 17 |
-
#
|
| 18 |
BLOG_TEMPLATES = {
|
| 19 |
-
"
|
| 20 |
-
"
|
| 21 |
-
"
|
| 22 |
-
"
|
| 23 |
-
"
|
|
|
|
| 24 |
}
|
| 25 |
|
| 26 |
BLOG_TONES = {
|
| 27 |
-
"professional": "
|
| 28 |
-
"casual": "
|
| 29 |
-
"humorous": "
|
| 30 |
-
"storytelling": "
|
| 31 |
}
|
| 32 |
|
| 33 |
-
#
|
| 34 |
EXAMPLE_TOPICS = {
|
| 35 |
-
"example1": "
|
| 36 |
-
"example2": "2025
|
| 37 |
-
"example3": "
|
| 38 |
}
|
| 39 |
|
| 40 |
-
# ββββββββββββββββββββββββββββββββ
|
| 41 |
logging.basicConfig(level=logging.INFO,
|
| 42 |
format="%(asctime)s - %(levelname)s - %(message)s")
|
| 43 |
|
| 44 |
-
# ββββββββββββββββββββββββββββββββ Anthropic Client
|
| 45 |
-
|
|
|
|
|
|
|
| 46 |
|
| 47 |
-
# ββββββββββββββββββββββββββββββββ
|
| 48 |
-
|
| 49 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
base_prompt = """
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
1.
|
| 55 |
-
1.
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
2.
|
| 60 |
-
|
| 61 |
-
3.
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
"""
|
| 85 |
|
| 86 |
-
#
|
| 87 |
template_guides = {
|
| 88 |
"tutorial": """
|
| 89 |
-
|
| 90 |
-
-
|
| 91 |
-
-
|
| 92 |
-
-
|
| 93 |
-
-
|
| 94 |
-
-
|
| 95 |
-
-
|
| 96 |
-
-
|
| 97 |
""",
|
| 98 |
-
|
| 99 |
"review": """
|
| 100 |
-
|
| 101 |
-
-
|
| 102 |
-
-
|
| 103 |
-
-
|
| 104 |
-
-
|
| 105 |
-
-
|
| 106 |
-
-
|
| 107 |
-
-
|
| 108 |
""",
|
| 109 |
-
|
| 110 |
"storytelling": """
|
| 111 |
-
|
| 112 |
-
-
|
| 113 |
-
-
|
| 114 |
-
-
|
| 115 |
-
-
|
| 116 |
-
-
|
| 117 |
-
-
|
| 118 |
-
-
|
| 119 |
""",
|
| 120 |
-
|
| 121 |
"seo_optimized": """
|
| 122 |
-
|
| 123 |
-
-
|
| 124 |
-
-
|
| 125 |
-
- 300-500
|
| 126 |
-
-
|
| 127 |
-
-
|
| 128 |
-
-
|
| 129 |
-
- 2000-3000
|
| 130 |
"""
|
| 131 |
}
|
| 132 |
-
|
| 133 |
-
#
|
| 134 |
tone_guides = {
|
| 135 |
-
"professional": "
|
| 136 |
-
"casual": "
|
| 137 |
-
"humorous": "
|
| 138 |
-
"storytelling": "
|
| 139 |
}
|
| 140 |
-
|
| 141 |
-
#
|
| 142 |
search_guide = """
|
| 143 |
-
|
| 144 |
-
-
|
| 145 |
-
-
|
| 146 |
-
-
|
| 147 |
-
-
|
| 148 |
-
-
|
| 149 |
-
-
|
| 150 |
"""
|
| 151 |
-
|
| 152 |
-
#
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 157 |
final_prompt += "\n" + template_guides[template]
|
| 158 |
-
|
| 159 |
-
#
|
| 160 |
if tone in tone_guides:
|
| 161 |
-
final_prompt += f"\n\
|
| 162 |
-
|
| 163 |
-
#
|
| 164 |
if include_search_results:
|
| 165 |
final_prompt += f"\n\n{search_guide}"
|
| 166 |
-
|
| 167 |
-
#
|
| 168 |
-
|
| 169 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 170 |
return final_prompt
|
| 171 |
|
| 172 |
-
# ββββββββββββββββββββββββββββββββ Brave Search API
|
| 173 |
-
|
|
|
|
| 174 |
"""
|
| 175 |
-
Brave Web Search API
|
| 176 |
-
|
| 177 |
"""
|
| 178 |
if not BRAVE_KEY:
|
| 179 |
-
raise RuntimeError("β οΈ
|
| 180 |
|
| 181 |
headers = {
|
| 182 |
"Accept": "application/json",
|
| 183 |
"Accept-Encoding": "gzip",
|
| 184 |
"X-Subscription-Token": BRAVE_KEY
|
| 185 |
}
|
| 186 |
-
params = {"q": query, "count": str(count)}
|
| 187 |
-
|
| 188 |
-
for attempt in range(3):
|
| 189 |
try:
|
| 190 |
r = requests.get(BRAVE_ENDPOINT, headers=headers, params=params, timeout=15)
|
| 191 |
r.raise_for_status()
|
| 192 |
data = r.json()
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
raw = data.get("web", {}).get("results") or data.get("results", [])
|
| 198 |
if not raw:
|
| 199 |
-
logging.warning(f"Brave
|
| 200 |
-
raise ValueError("
|
| 201 |
-
|
| 202 |
arts = []
|
| 203 |
-
for i, res in enumerate(raw[:count], 1):
|
| 204 |
-
url
|
| 205 |
-
host
|
| 206 |
arts.append({
|
| 207 |
"index": i,
|
| 208 |
-
"title": res.get("title", "
|
| 209 |
"link": url,
|
| 210 |
-
"snippet": res.get("description", res.get("text", "
|
| 211 |
"displayed_link": host
|
| 212 |
})
|
| 213 |
-
|
| 214 |
-
logging.info(f"Brave
|
| 215 |
return arts
|
| 216 |
-
|
| 217 |
except Exception as e:
|
| 218 |
-
logging.error(f"Brave
|
| 219 |
-
if attempt < 2:
|
| 220 |
time.sleep(2)
|
| 221 |
-
|
| 222 |
-
return []
|
| 223 |
|
| 224 |
def mock_results(query: str) -> str:
|
| 225 |
-
"""
|
| 226 |
ts = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
| 227 |
-
return (f"#
|
| 228 |
-
f"
|
| 229 |
-
f"
|
| 230 |
-
f"- {query}
|
| 231 |
-
f"-
|
| 232 |
-
f"-
|
| 233 |
-
f"-
|
| 234 |
-
f"
|
| 235 |
|
| 236 |
def do_web_search(query: str) -> str:
|
| 237 |
-
"""
|
| 238 |
try:
|
| 239 |
arts = brave_search(query, 20)
|
| 240 |
if not arts:
|
| 241 |
-
logging.warning("
|
| 242 |
return mock_results(query)
|
| 243 |
-
|
| 244 |
-
hdr = "#
|
| 245 |
body = "\n".join(
|
| 246 |
f"### Result {a['index']}: {a['title']}\n\n{a['snippet']}\n\n"
|
| 247 |
-
f"
|
| 248 |
for a in arts
|
| 249 |
)
|
| 250 |
return hdr + body
|
| 251 |
except Exception as e:
|
| 252 |
-
logging.error(f"
|
| 253 |
return mock_results(query)
|
| 254 |
|
| 255 |
-
# ββββββββββββββββββββββββββββββββ
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 256 |
def generate_image(prompt, w=768, h=768, g=3.5, steps=30, seed=3):
|
| 257 |
-
|
|
|
|
|
|
|
| 258 |
try:
|
| 259 |
res = Client(IMAGE_API_URL).predict(
|
| 260 |
prompt=prompt, width=w, height=h, guidance=g,
|
| 261 |
inference_steps=steps, seed=seed,
|
| 262 |
do_img2img=False, init_image=None,
|
| 263 |
image2image_strength=0.8, resize_img=True,
|
| 264 |
-
api_name="/generate_image"
|
|
|
|
| 265 |
return res[0], f"Seed: {res[1]}"
|
| 266 |
except Exception as e:
|
| 267 |
-
logging.error(e)
|
|
|
|
| 268 |
|
| 269 |
-
def extract_image_prompt(
|
| 270 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 271 |
try:
|
|
|
|
| 272 |
res = client.messages.create(
|
| 273 |
model="claude-3-7-sonnet-20250219",
|
| 274 |
-
max_tokens=80,
|
| 275 |
-
|
|
|
|
| 276 |
)
|
| 277 |
return res.content[0].text.strip()
|
| 278 |
except Exception:
|
|
|
|
| 279 |
return f"A professional photo related to {topic}, high quality"
|
| 280 |
|
| 281 |
def md_to_html(md: str, title="Ginigen Blog"):
|
|
|
|
| 282 |
return f"<!DOCTYPE html><html><head><title>{title}</title><meta charset='utf-8'></head><body>{markdown.markdown(md)}</body></html>"
|
| 283 |
|
| 284 |
def keywords(text: str, top=5):
|
| 285 |
-
|
|
|
|
|
|
|
| 286 |
|
| 287 |
# ββββββββββββββββββββββββββββββββ Streamlit UI ββββββββββββββββββββββββββββ
|
| 288 |
def ginigen_app():
|
| 289 |
-
st.title("
|
| 290 |
|
| 291 |
-
#
|
| 292 |
if "ai_model" not in st.session_state:
|
| 293 |
st.session_state.ai_model = "claude-3-7-sonnet-20250219"
|
| 294 |
if "messages" not in st.session_state:
|
|
@@ -297,143 +549,229 @@ def ginigen_app():
|
|
| 297 |
st.session_state.auto_save = True
|
| 298 |
if "generate_image" not in st.session_state:
|
| 299 |
st.session_state.generate_image = False
|
| 300 |
-
if "
|
| 301 |
-
st.session_state.
|
| 302 |
if "blog_template" not in st.session_state:
|
| 303 |
-
st.session_state.blog_template = "
|
| 304 |
if "blog_tone" not in st.session_state:
|
| 305 |
st.session_state.blog_tone = "professional"
|
| 306 |
if "word_count" not in st.session_state:
|
| 307 |
st.session_state.word_count = 1750
|
| 308 |
|
| 309 |
-
#
|
| 310 |
sb = st.sidebar
|
| 311 |
-
sb.title("
|
| 312 |
-
|
| 313 |
-
# λΈλ‘κ·Έ ν
νλ¦Ώ λ° μ€νμΌ μ ν
|
| 314 |
-
sb.subheader("λΈλ‘κ·Έ μ€νμΌ μ€μ ")
|
| 315 |
-
sb.selectbox("λΈλ‘κ·Έ ν
νλ¦Ώ", options=list(BLOG_TEMPLATES.keys()),
|
| 316 |
-
format_func=lambda x: BLOG_TEMPLATES[x],
|
| 317 |
-
key="blog_template")
|
| 318 |
|
| 319 |
-
sb.
|
| 320 |
-
|
| 321 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 322 |
|
| 323 |
-
sb.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 324 |
|
| 325 |
-
|
| 326 |
-
sb.subheader("μμ μ£Όμ ")
|
| 327 |
|
| 328 |
-
|
| 329 |
-
|
| 330 |
-
|
| 331 |
-
if
|
| 332 |
-
# μμ μ£Όμ λ₯Ό μ
λ ₯μΌλ‘ μ¦μ μ²λ¦¬ (rerun μμ΄)
|
| 333 |
process_example(EXAMPLE_TOPICS["example1"])
|
| 334 |
-
|
| 335 |
-
if col2.button("μ¬λ¦ μΆμ ", key="ex2"):
|
| 336 |
process_example(EXAMPLE_TOPICS["example2"])
|
| 337 |
-
|
| 338 |
-
if col3.button("ν¬μ κ°μ΄λ", key="ex3"):
|
| 339 |
process_example(EXAMPLE_TOPICS["example3"])
|
| 340 |
|
| 341 |
-
sb.subheader("
|
| 342 |
-
sb.toggle("
|
| 343 |
-
sb.toggle("
|
|
|
|
|
|
|
|
|
|
| 344 |
|
| 345 |
-
|
| 346 |
-
|
| 347 |
-
if search_enabled:
|
| 348 |
-
st.warning("β οΈ μΉ κ²μ κΈ°λ₯μ νμ¬ λΆμμ ν μ μμ΅λλ€. κ²μ κ²°κ³Όκ° μμΌλ©΄ κΈ°λ³Έ μ§μμΌλ‘ λ체λ©λλ€.")
|
| 349 |
|
| 350 |
-
#
|
| 351 |
latest_blog = next(
|
| 352 |
-
(m["content"] for m in reversed(st.session_state.messages)
|
| 353 |
-
if m["role"] == "assistant" and m["content"].strip()),
|
| 354 |
-
|
|
|
|
| 355 |
if latest_blog:
|
| 356 |
-
|
| 357 |
-
title =
|
| 358 |
-
sb.subheader("
|
| 359 |
-
|
| 360 |
-
|
| 361 |
file_name=f"{title}.md", mime="text/markdown")
|
| 362 |
-
|
| 363 |
file_name=f"{title}.html", mime="text/html")
|
| 364 |
|
| 365 |
-
#
|
| 366 |
-
up = sb.file_uploader("
|
| 367 |
if up:
|
| 368 |
try:
|
| 369 |
st.session_state.messages = json.load(up)
|
| 370 |
-
sb.success("
|
| 371 |
except Exception as e:
|
| 372 |
-
sb.error(f"
|
| 373 |
-
|
| 374 |
-
#
|
| 375 |
-
if sb.button("
|
| 376 |
-
sb.download_button(
|
| 377 |
-
|
| 378 |
-
|
| 379 |
-
|
|
|
|
|
|
|
| 380 |
|
| 381 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 382 |
for m in st.session_state.messages:
|
| 383 |
with st.chat_message(m["role"]):
|
| 384 |
st.markdown(m["content"])
|
| 385 |
if "image" in m:
|
| 386 |
st.image(m["image"], caption=m.get("image_caption", ""))
|
| 387 |
|
| 388 |
-
#
|
| 389 |
-
prompt = st.chat_input("
|
| 390 |
-
|
| 391 |
if prompt:
|
| 392 |
-
process_input(prompt)
|
| 393 |
-
|
| 394 |
|
| 395 |
def process_example(topic):
|
| 396 |
-
"""
|
| 397 |
-
process_input(topic)
|
| 398 |
-
|
| 399 |
-
|
| 400 |
-
def process_input(prompt):
|
| 401 |
-
"""μ¬μ©μ μ
λ ₯ μ²λ¦¬ ν¨μ (μΌλ° μ
λ ₯κ³Ό μμ μ
λ ₯ λͺ¨λ μ²λ¦¬)"""
|
| 402 |
-
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 403 |
-
with st.chat_message("user"): st.markdown(prompt)
|
| 404 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 405 |
with st.chat_message("assistant"):
|
| 406 |
-
placeholder = st.empty()
|
| 407 |
-
|
| 408 |
-
|
| 409 |
-
use_web_search = st.session_state.
|
|
|
|
| 410 |
|
| 411 |
try:
|
| 412 |
-
|
| 413 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 414 |
sys_prompt = get_system_prompt(
|
| 415 |
template=st.session_state.blog_template,
|
| 416 |
tone=st.session_state.blog_tone,
|
| 417 |
word_count=st.session_state.word_count,
|
| 418 |
-
include_search_results=use_web_search
|
|
|
|
| 419 |
)
|
| 420 |
|
| 421 |
-
|
| 422 |
-
if
|
| 423 |
-
|
| 424 |
-
|
| 425 |
-
|
| 426 |
-
|
| 427 |
-
|
| 428 |
-
|
| 429 |
|
| 430 |
-
#
|
| 431 |
-
if
|
| 432 |
-
|
| 433 |
-
|
| 434 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 435 |
with client.messages.stream(
|
| 436 |
-
model=st.session_state.ai_model,
|
|
|
|
| 437 |
system=sys_prompt,
|
| 438 |
messages=messages
|
| 439 |
) as stream:
|
|
@@ -441,70 +779,71 @@ def process_input(prompt):
|
|
| 441 |
answer += t or ""
|
| 442 |
placeholder.markdown(answer + "β")
|
| 443 |
placeholder.markdown(answer)
|
| 444 |
-
|
| 445 |
-
#
|
| 446 |
answer_entry_saved = False
|
| 447 |
if st.session_state.generate_image:
|
| 448 |
-
with st.spinner("
|
| 449 |
ip = extract_image_prompt(answer, prompt)
|
| 450 |
img, cap = generate_image(ip)
|
| 451 |
if img:
|
| 452 |
st.image(img, caption=cap)
|
| 453 |
-
st.session_state.messages.append(
|
| 454 |
-
|
| 455 |
-
|
|
|
|
|
|
|
|
|
|
| 456 |
answer_entry_saved = True
|
|
|
|
|
|
|
| 457 |
if not answer_entry_saved:
|
| 458 |
-
st.session_state.messages.append(
|
| 459 |
-
|
| 460 |
-
|
| 461 |
-
|
| 462 |
-
st.
|
| 463 |
-
|
| 464 |
-
|
| 465 |
-
|
| 466 |
-
|
| 467 |
-
|
| 468 |
-
|
| 469 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 470 |
if st.session_state.auto_save and st.session_state.messages:
|
| 471 |
try:
|
| 472 |
fn = f"chat_history_auto_{datetime.now():%Y%m%d_%H%M%S}.json"
|
| 473 |
with open(fn, "w", encoding="utf-8") as fp:
|
| 474 |
-
json.dump(st.session_state.messages, fp,
|
| 475 |
-
ensure_ascii=False, indent=2)
|
| 476 |
except Exception as e:
|
| 477 |
-
logging.error(f"
|
| 478 |
-
|
| 479 |
except anthropic.BadRequestError as e:
|
| 480 |
error_message = str(e)
|
| 481 |
if "credit balance is too low" in error_message:
|
| 482 |
-
placeholder.error("β οΈ API
|
| 483 |
-
|
| 484 |
else:
|
| 485 |
-
placeholder.error(f"API
|
| 486 |
-
|
| 487 |
-
|
| 488 |
-
st.session_state.messages.append({"role": "assistant", "content": answer})
|
| 489 |
|
| 490 |
except Exception as e:
|
| 491 |
error_message = str(e)
|
| 492 |
-
placeholder.error(f"
|
| 493 |
-
|
| 494 |
-
st.session_state.messages.append({"role": "assistant", "content":
|
| 495 |
|
| 496 |
-
# ββββββββββββββββββββββββββββββββ main
|
| 497 |
-
def main():
|
|
|
|
| 498 |
|
| 499 |
if __name__ == "__main__":
|
| 500 |
-
|
| 501 |
-
with open("requirements.txt", "w") as f:
|
| 502 |
-
f.write("\n".join([
|
| 503 |
-
"streamlit>=1.31.0",
|
| 504 |
-
"anthropic>=0.18.1",
|
| 505 |
-
"gradio-client>=1.8.0",
|
| 506 |
-
"requests>=2.32.3",
|
| 507 |
-
"markdown>=3.5.1",
|
| 508 |
-
"pillow>=10.1.0"
|
| 509 |
-
]))
|
| 510 |
-
main()
|
|
|
|
| 1 |
# ββββββββββββββββββββββββββββββββ Imports ββββββββββββββββββββββββββββββββ
|
| 2 |
+
import os, json, re, logging, requests, markdown, time, io
|
| 3 |
from datetime import datetime
|
| 4 |
|
| 5 |
import streamlit as st
|
| 6 |
import anthropic
|
| 7 |
from gradio_client import Client
|
| 8 |
+
import pandas as pd
|
| 9 |
+
import PyPDF2 # For handling PDF files
|
| 10 |
|
| 11 |
+
# ββββββββββββββββββββββββββββββββ Environment Variables / Constants βββββββββββββββββββββββββ
|
| 12 |
ANTHROPIC_KEY = os.getenv("API_KEY", "")
|
| 13 |
+
BRAVE_KEY = os.getenv("SERPHOUSE_API_KEY", "") # Keep this name
|
| 14 |
BRAVE_ENDPOINT = "https://api.search.brave.com/res/v1/web/search"
|
| 15 |
IMAGE_API_URL = "http://211.233.58.201:7896"
|
| 16 |
MAX_TOKENS = 7_999
|
| 17 |
|
| 18 |
+
# Blog template and style definitions (in English)
|
| 19 |
BLOG_TEMPLATES = {
|
| 20 |
+
"ginigen": "Recommended style by Ginigen",
|
| 21 |
+
"standard": "Standard 8-step framework blog",
|
| 22 |
+
"tutorial": "Step-by-step tutorial format",
|
| 23 |
+
"review": "Product/service review format",
|
| 24 |
+
"storytelling": "Storytelling format",
|
| 25 |
+
"seo_optimized": "SEO-optimized blog"
|
| 26 |
}
|
| 27 |
|
| 28 |
BLOG_TONES = {
|
| 29 |
+
"professional": "Professional and formal tone",
|
| 30 |
+
"casual": "Friendly and conversational tone",
|
| 31 |
+
"humorous": "Humorous approach",
|
| 32 |
+
"storytelling": "Story-driven approach"
|
| 33 |
}
|
| 34 |
|
| 35 |
+
# Example blog topics
|
| 36 |
EXAMPLE_TOPICS = {
|
| 37 |
+
"example1": "Changes to the real estate tax system in 2025: Impact on average households and tax-saving strategies",
|
| 38 |
+
"example2": "Summer festivals in 2025: A comprehensive guide to major regional events and hidden attractions",
|
| 39 |
+
"example3": "Emerging industries to watch in 2025: An investment guide focused on AI opportunities"
|
| 40 |
}
|
| 41 |
|
| 42 |
+
# ββββββββββββββββββββββββββββββββ Logging ββββββββββββββββββββββββββββββββ
|
| 43 |
logging.basicConfig(level=logging.INFO,
|
| 44 |
format="%(asctime)s - %(levelname)s - %(message)s")
|
| 45 |
|
| 46 |
+
# ββββββββββββββββββββββββββββββββ Anthropic Client ββββββββββββββββββββββββ
|
| 47 |
+
@st.cache_resource
|
| 48 |
+
def get_anthropic_client():
|
| 49 |
+
return anthropic.Anthropic(api_key=ANTHROPIC_KEY)
|
| 50 |
|
| 51 |
+
# ββββββββββββββββββββββββββββββββ Blog Creation System Prompt βββββββββββββ
|
| 52 |
+
def get_system_prompt(template="ginigen", tone="professional", word_count=1750, include_search_results=False, include_uploaded_files=False) -> str:
|
| 53 |
+
"""
|
| 54 |
+
Generate a system prompt that includes:
|
| 55 |
+
- The 8-step blog writing framework
|
| 56 |
+
- The selected template and tone
|
| 57 |
+
- Guidelines for using web search results and uploaded files
|
| 58 |
+
"""
|
| 59 |
+
|
| 60 |
+
# Ginigen recommended style prompt (English version)
|
| 61 |
+
ginigen_prompt = """
|
| 62 |
+
## π Professional Blogger System Prompt
|
| 63 |
+
|
| 64 |
+
### β
Official 8-step Prompt
|
| 65 |
+
|
| 66 |
+
Follow these 8 steps exactly in order to write the blog post:
|
| 67 |
+
|
| 68 |
+
### 1. Start with a greeting and empathy
|
| 69 |
+
- Open with a friendly tone that draws the reader in
|
| 70 |
+
- Ask questions or present scenarios that resonate with the readerβs real-life concerns
|
| 71 |
+
|
| 72 |
+
### 2. Clearly present the problem
|
| 73 |
+
- Pinpoint the exact and realistic problem the reader is facing
|
| 74 |
+
- Emphasize the seriousness or urgency of this problem to maintain interest
|
| 75 |
+
|
| 76 |
+
### 3. Analyze the cause of the problem to build credibility
|
| 77 |
+
- Explain the causes of the problem logically
|
| 78 |
+
- Clearly and specifically present your analysis so the reader can understand it easily
|
| 79 |
+
- Include data, examples, or references if necessary
|
| 80 |
+
|
| 81 |
+
### 4. Offer a concrete solution
|
| 82 |
+
- Provide specific, actionable steps to solve the problem
|
| 83 |
+
- Give tips, strategies, and guidelines so the reader can implement them right away
|
| 84 |
+
|
| 85 |
+
### 5. Provide social proof
|
| 86 |
+
- Include real success stories, reviews, user experiences, or data
|
| 87 |
+
- Keep details factual and believable so the reader can trust the content
|
| 88 |
+
|
| 89 |
+
### 6. Call to action (CTA)
|
| 90 |
+
- Encourage the reader to take specific actions immediately
|
| 91 |
+
- Use urgent language such as βright now,β βfrom today,β or βimmediatelyβ to drive action
|
| 92 |
+
|
| 93 |
+
### 7. Add constraints or warnings to increase authenticity
|
| 94 |
+
- Acknowledge that the solution might not work for everyone
|
| 95 |
+
- Show sincerity and scarcity, which boosts trust
|
| 96 |
+
|
| 97 |
+
### 8. Express gratitude and guide them to further connection
|
| 98 |
+
- Thank the reader for their time
|
| 99 |
+
- Provide a natural lead-in to the next post, or ask for comments/subscriptions
|
| 100 |
+
|
| 101 |
+
---
|
| 102 |
+
|
| 103 |
+
### π© Writing Style Tips
|
| 104 |
+
- Maintain a friendly and human-like tone
|
| 105 |
+
- Frequently use questions and a conversational style to engage the reader
|
| 106 |
+
- Use clear headings, horizontal lines, bullet points, or numbered lists for readability
|
| 107 |
+
- Include real-life examples and specific data where possible
|
| 108 |
+
"""
|
| 109 |
+
|
| 110 |
+
# Standard 8-step framework (English version)
|
| 111 |
base_prompt = """
|
| 112 |
+
You are an expert in writing professional blog posts. For every blog writing request, strictly follow this 8-step framework to produce a coherent, engaging post:
|
| 113 |
+
|
| 114 |
+
Reader Connection Phase
|
| 115 |
+
1.1. Friendly greeting to build rapport
|
| 116 |
+
1.2. Reflect actual reader concerns through introductory questions
|
| 117 |
+
1.3. Stimulate immediate interest in the topic
|
| 118 |
+
|
| 119 |
+
Problem Definition Phase
|
| 120 |
+
2.1. Define the readerβs pain points in detail
|
| 121 |
+
2.2. Analyze the urgency and impact of the problem
|
| 122 |
+
2.3. Build a consensus on why it needs to be solved
|
| 123 |
+
|
| 124 |
+
Establish Expertise Phase
|
| 125 |
+
3.1. Analyze based on objective data
|
| 126 |
+
3.2. Cite expert views and research findings
|
| 127 |
+
3.3. Use real-life examples to further clarify the issue
|
| 128 |
+
|
| 129 |
+
Solution Phase
|
| 130 |
+
4.1. Provide step-by-step guidance
|
| 131 |
+
4.2. Suggest practical tips that can be applied immediately
|
| 132 |
+
4.3. Mention potential obstacles and how to overcome them
|
| 133 |
+
|
| 134 |
+
Build Trust Phase
|
| 135 |
+
5.1. Present actual success stories
|
| 136 |
+
5.2. Quote real user feedback
|
| 137 |
+
5.3. Use objective data to prove effectiveness
|
| 138 |
+
|
| 139 |
+
Action Phase
|
| 140 |
+
6.1. Suggest the first clear step the reader can take
|
| 141 |
+
6.2. Urge timely action by emphasizing urgency
|
| 142 |
+
6.3. Motivate by highlighting incentives or benefits
|
| 143 |
+
|
| 144 |
+
Authenticity Phase
|
| 145 |
+
7.1. Transparently disclose any limits of the solution
|
| 146 |
+
7.2. Admit that individual experiences may vary
|
| 147 |
+
7.3. Mention prerequisites or cautionary points
|
| 148 |
+
|
| 149 |
+
Relationship Continuation Phase
|
| 150 |
+
8.1. Conclude with sincere gratitude
|
| 151 |
+
8.2. Preview upcoming content to build anticipation
|
| 152 |
+
8.3. Provide channels for further communication
|
| 153 |
"""
|
| 154 |
|
| 155 |
+
# Additional guidelines for each template
|
| 156 |
template_guides = {
|
| 157 |
"tutorial": """
|
| 158 |
+
This blog should be in a tutorial style:
|
| 159 |
+
- Clearly state the goal and the final outcome first
|
| 160 |
+
- Provide step-by-step explanations with clear separations
|
| 161 |
+
- Indicate where images could be inserted for each step
|
| 162 |
+
- Mention approximate time requirements and difficulty level
|
| 163 |
+
- List necessary tools or prerequisite knowledge
|
| 164 |
+
- Give troubleshooting tips and common mistakes to avoid
|
| 165 |
+
- Conclude with suggestions for next steps or advanced applications
|
| 166 |
""",
|
|
|
|
| 167 |
"review": """
|
| 168 |
+
This blog should be in a review style:
|
| 169 |
+
- Separate objective facts from subjective opinions
|
| 170 |
+
- Clearly list your evaluation criteria
|
| 171 |
+
- Discuss both pros and cons in a balanced way
|
| 172 |
+
- Compare with similar products/services
|
| 173 |
+
- Specify the target audience for whom it is suitable
|
| 174 |
+
- Provide concrete use cases and outcomes
|
| 175 |
+
- Conclude with a final recommendation or alternatives
|
| 176 |
""",
|
|
|
|
| 177 |
"storytelling": """
|
| 178 |
+
This blog should be in a storytelling style:
|
| 179 |
+
- Start with a real or hypothetical person or case
|
| 180 |
+
- Emphasize emotional connection with the problem scenario
|
| 181 |
+
- Follow a narrative structure centered on conflict and resolution
|
| 182 |
+
- Include meaningful insights or lessons learned
|
| 183 |
+
- Maintain an emotional thread the reader can relate to
|
| 184 |
+
- Balance storytelling with useful information
|
| 185 |
+
- Encourage the reader to reflect on their own story
|
| 186 |
""",
|
|
|
|
| 187 |
"seo_optimized": """
|
| 188 |
+
This blog should be SEO-optimized:
|
| 189 |
+
- Include the main keyword in the title, headings, and first paragraph
|
| 190 |
+
- Spread related keywords naturally throughout the text
|
| 191 |
+
- Keep paragraphs around 300-500 characters
|
| 192 |
+
- Use question-based subheadings
|
| 193 |
+
- Make use of lists, tables, and bold text to diversify formatting
|
| 194 |
+
- Indicate where internal links could be inserted
|
| 195 |
+
- Provide sufficient content of at least 2000-3000 characters
|
| 196 |
"""
|
| 197 |
}
|
| 198 |
+
|
| 199 |
+
# Additional guidelines for each tone
|
| 200 |
tone_guides = {
|
| 201 |
+
"professional": "Use a professional, authoritative voice. Clearly explain any technical terms and present data or research to maintain a logical flow.",
|
| 202 |
+
"casual": "Use a relaxed, conversational style. Employ personal experiences, relatable examples, and a friendly voice (e.g., 'Itβs super useful!').",
|
| 203 |
+
"humorous": "Use humor and witty expressions. Add funny analogies or jokes while preserving accuracy and usefulness.",
|
| 204 |
+
"storytelling": "Write as if telling a story, with emotional depth and narrative flow. Incorporate characters, settings, conflicts, and resolutions."
|
| 205 |
}
|
| 206 |
+
|
| 207 |
+
# Guidelines for using search results
|
| 208 |
search_guide = """
|
| 209 |
+
Guidelines for Using Search Results:
|
| 210 |
+
- Accurately incorporate key information from the search results into the blog
|
| 211 |
+
- Include recent data, statistics, and case studies from the search results
|
| 212 |
+
- When quoting, specify the source within the text (e.g., βAccording to XYZ website...β)
|
| 213 |
+
- At the end of the blog, add a "References" section and list major sources with links
|
| 214 |
+
- If there are conflicting pieces of information, present multiple perspectives
|
| 215 |
+
- Make sure to reflect the latest trends and data from the search results
|
| 216 |
"""
|
| 217 |
+
|
| 218 |
+
# Guidelines for using uploaded files
|
| 219 |
+
upload_guide = """
|
| 220 |
+
Guidelines for Using Uploaded Files (Highest Priority):
|
| 221 |
+
- The uploaded files must be a main source of information for the blog
|
| 222 |
+
- Carefully examine the data, statistics, or examples in the file and integrate them
|
| 223 |
+
- Directly quote and thoroughly explain any key figures or claims from the file
|
| 224 |
+
- Highlight the file content as a crucial aspect of the blog
|
| 225 |
+
- Mention the source clearly, e.g., βAccording to the uploaded data...β
|
| 226 |
+
- For CSV files, detail important stats or numerical data in the blog
|
| 227 |
+
- For PDF files, quote crucial segments or statements
|
| 228 |
+
- For text files, integrate relevant content effectively
|
| 229 |
+
- Even if the file content seems tangential, do your best to connect it to the blog topic
|
| 230 |
+
- Keep consistency throughout and ensure the fileβs data is appropriately reflected
|
| 231 |
+
"""
|
| 232 |
+
|
| 233 |
+
# Choose base prompt
|
| 234 |
+
if template == "ginigen":
|
| 235 |
+
final_prompt = ginigen_prompt
|
| 236 |
+
else:
|
| 237 |
+
final_prompt = base_prompt
|
| 238 |
+
|
| 239 |
+
# If the user chose a specific template (and not ginigen), append the relevant guidelines
|
| 240 |
+
if template != "ginigen" and template in template_guides:
|
| 241 |
final_prompt += "\n" + template_guides[template]
|
| 242 |
+
|
| 243 |
+
# If a specific tone is selected, append that guideline
|
| 244 |
if tone in tone_guides:
|
| 245 |
+
final_prompt += f"\n\nTone and Manner: {tone_guides[tone]}"
|
| 246 |
+
|
| 247 |
+
# If web search results should be included
|
| 248 |
if include_search_results:
|
| 249 |
final_prompt += f"\n\n{search_guide}"
|
| 250 |
+
|
| 251 |
+
# If uploaded files should be included
|
| 252 |
+
if include_uploaded_files:
|
| 253 |
+
final_prompt += f"\n\n{upload_guide}"
|
| 254 |
+
|
| 255 |
+
# Word count guidelines
|
| 256 |
+
final_prompt += (
|
| 257 |
+
f"\n\nWriting Requirements:\n"
|
| 258 |
+
f"9.1. Word Count: around {word_count-250}-{word_count+250} characters\n"
|
| 259 |
+
f"9.2. Paragraph Length: 3-4 sentences each\n"
|
| 260 |
+
f"9.3. Visual Cues: Use subheadings, separators, and bullet/numbered lists\n"
|
| 261 |
+
f"9.4. Data: Cite all sources\n"
|
| 262 |
+
f"9.5. Readability: Use clear paragraph breaks and highlights where necessary"
|
| 263 |
+
)
|
| 264 |
+
|
| 265 |
return final_prompt
|
| 266 |
|
| 267 |
+
# ββββββββββββββββββββββββββββββββ Brave Search API ββββββββββββββββββββββββ
|
| 268 |
+
@st.cache_data(ttl=3600)
|
| 269 |
+
def brave_search(query: str, count: int = 20):
|
| 270 |
"""
|
| 271 |
+
Call the Brave Web Search API β list[dict]
|
| 272 |
+
Returns fields: index, title, link, snippet, displayed_link
|
| 273 |
"""
|
| 274 |
if not BRAVE_KEY:
|
| 275 |
+
raise RuntimeError("β οΈ SERPHOUSE_API_KEY (Brave API Key) environment variable is empty.")
|
| 276 |
|
| 277 |
headers = {
|
| 278 |
"Accept": "application/json",
|
| 279 |
"Accept-Encoding": "gzip",
|
| 280 |
"X-Subscription-Token": BRAVE_KEY
|
| 281 |
}
|
| 282 |
+
params = {"q": query, "count": str(count)}
|
| 283 |
+
|
| 284 |
+
for attempt in range(3):
|
| 285 |
try:
|
| 286 |
r = requests.get(BRAVE_ENDPOINT, headers=headers, params=params, timeout=15)
|
| 287 |
r.raise_for_status()
|
| 288 |
data = r.json()
|
| 289 |
+
|
| 290 |
+
logging.info(f"Brave search result data structure: {list(data.keys())}")
|
| 291 |
+
|
|
|
|
| 292 |
raw = data.get("web", {}).get("results") or data.get("results", [])
|
| 293 |
if not raw:
|
| 294 |
+
logging.warning(f"No Brave search results found. Response: {data}")
|
| 295 |
+
raise ValueError("No search results found.")
|
| 296 |
+
|
| 297 |
arts = []
|
| 298 |
+
for i, res in enumerate(raw[:count], 1):
|
| 299 |
+
url = res.get("url", res.get("link", ""))
|
| 300 |
+
host = re.sub(r"https?://(www\.)?", "", url).split("/")[0]
|
| 301 |
arts.append({
|
| 302 |
"index": i,
|
| 303 |
+
"title": res.get("title", "No title"),
|
| 304 |
"link": url,
|
| 305 |
+
"snippet": res.get("description", res.get("text", "No snippet")),
|
| 306 |
"displayed_link": host
|
| 307 |
})
|
| 308 |
+
|
| 309 |
+
logging.info(f"Brave search success: {len(arts)} results")
|
| 310 |
return arts
|
| 311 |
+
|
| 312 |
except Exception as e:
|
| 313 |
+
logging.error(f"Brave search failure (attempt {attempt+1}/3): {e}")
|
| 314 |
+
if attempt < 2:
|
| 315 |
time.sleep(2)
|
| 316 |
+
|
| 317 |
+
return []
|
| 318 |
|
| 319 |
def mock_results(query: str) -> str:
|
| 320 |
+
"""Fallback search results if API fails"""
|
| 321 |
ts = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
| 322 |
+
return (f"# Fallback Search Content (Generated: {ts})\n\n"
|
| 323 |
+
f"The search API request failed. Please generate the blog based on any pre-existing knowledge about '{query}'.\n\n"
|
| 324 |
+
f"You may consider the following points:\n\n"
|
| 325 |
+
f"- Basic concepts and importance of {query}\n"
|
| 326 |
+
f"- Commonly known related statistics or trends\n"
|
| 327 |
+
f"- Typical expert opinions on this subject\n"
|
| 328 |
+
f"- Questions that readers might have\n\n"
|
| 329 |
+
f"Note: This is fallback guidance, not real-time data.\n\n")
|
| 330 |
|
| 331 |
def do_web_search(query: str) -> str:
|
| 332 |
+
"""Perform web search and format the results."""
|
| 333 |
try:
|
| 334 |
arts = brave_search(query, 20)
|
| 335 |
if not arts:
|
| 336 |
+
logging.warning("No search results, using fallback content")
|
| 337 |
return mock_results(query)
|
| 338 |
+
|
| 339 |
+
hdr = "# Web Search Results\nUse the information below to enhance the reliability of your blog. When you quote, please cite the source, and add a References section at the end of the blog.\n\n"
|
| 340 |
body = "\n".join(
|
| 341 |
f"### Result {a['index']}: {a['title']}\n\n{a['snippet']}\n\n"
|
| 342 |
+
f"**Source**: [{a['displayed_link']}]({a['link']})\n\n---\n"
|
| 343 |
for a in arts
|
| 344 |
)
|
| 345 |
return hdr + body
|
| 346 |
except Exception as e:
|
| 347 |
+
logging.error(f"Web search process failed: {str(e)}")
|
| 348 |
return mock_results(query)
|
| 349 |
|
| 350 |
+
# ββββββββββββββββββββββββββββββββ File Upload Handling βββββββββββββββββββββ
|
| 351 |
+
def process_text_file(file):
|
| 352 |
+
"""Handle text file"""
|
| 353 |
+
try:
|
| 354 |
+
content = file.read()
|
| 355 |
+
file.seek(0)
|
| 356 |
+
|
| 357 |
+
text = content.decode('utf-8', errors='ignore')
|
| 358 |
+
if len(text) > 10000:
|
| 359 |
+
text = text[:9700] + "...(truncated)..."
|
| 360 |
+
|
| 361 |
+
result = f"## Text File: {file.name}\n\n"
|
| 362 |
+
result += text
|
| 363 |
+
return result
|
| 364 |
+
except Exception as e:
|
| 365 |
+
logging.error(f"Error processing text file: {str(e)}")
|
| 366 |
+
return f"Error processing text file: {str(e)}"
|
| 367 |
+
|
| 368 |
+
def process_csv_file(file):
|
| 369 |
+
"""Handle CSV file"""
|
| 370 |
+
try:
|
| 371 |
+
content = file.read()
|
| 372 |
+
file.seek(0)
|
| 373 |
+
|
| 374 |
+
df = pd.read_csv(io.BytesIO(content))
|
| 375 |
+
result = f"## CSV File: {file.name}\n\n"
|
| 376 |
+
result += f"- Rows: {len(df)}\n"
|
| 377 |
+
result += f"- Columns: {len(df.columns)}\n"
|
| 378 |
+
result += f"- Column Names: {', '.join(df.columns.tolist())}\n\n"
|
| 379 |
+
|
| 380 |
+
result += "### Data Preview\n\n"
|
| 381 |
+
preview_df = df.head(10)
|
| 382 |
+
try:
|
| 383 |
+
markdown_table = preview_df.to_markdown(index=False)
|
| 384 |
+
if markdown_table:
|
| 385 |
+
result += markdown_table + "\n\n"
|
| 386 |
+
else:
|
| 387 |
+
result += "Unable to display CSV data.\n\n"
|
| 388 |
+
except Exception as e:
|
| 389 |
+
logging.error(f"Markdown table conversion error: {e}")
|
| 390 |
+
result += "Displaying data as text:\n\n"
|
| 391 |
+
result += str(preview_df) + "\n\n"
|
| 392 |
+
|
| 393 |
+
num_cols = df.select_dtypes(include=['number']).columns
|
| 394 |
+
if len(num_cols) > 0:
|
| 395 |
+
result += "### Basic Statistical Information\n\n"
|
| 396 |
+
try:
|
| 397 |
+
stats_df = df[num_cols].describe().round(2)
|
| 398 |
+
stats_markdown = stats_df.to_markdown()
|
| 399 |
+
if stats_markdown:
|
| 400 |
+
result += stats_markdown + "\n\n"
|
| 401 |
+
else:
|
| 402 |
+
result += "Unable to display statistical information.\n\n"
|
| 403 |
+
except Exception as e:
|
| 404 |
+
logging.error(f"Statistical info conversion error: {e}")
|
| 405 |
+
result += "Unable to generate statistical information.\n\n"
|
| 406 |
+
|
| 407 |
+
return result
|
| 408 |
+
except Exception as e:
|
| 409 |
+
logging.error(f"CSV file processing error: {str(e)}")
|
| 410 |
+
return f"Error processing CSV file: {str(e)}"
|
| 411 |
+
|
| 412 |
+
def process_pdf_file(file):
|
| 413 |
+
"""Handle PDF file"""
|
| 414 |
+
try:
|
| 415 |
+
# Read file in bytes
|
| 416 |
+
file_bytes = file.read()
|
| 417 |
+
file.seek(0)
|
| 418 |
+
|
| 419 |
+
# Use PyPDF2
|
| 420 |
+
pdf_file = io.BytesIO(file_bytes)
|
| 421 |
+
reader = PyPDF2.PdfReader(pdf_file, strict=False)
|
| 422 |
+
|
| 423 |
+
# Basic info
|
| 424 |
+
result = f"## PDF File: {file.name}\n\n"
|
| 425 |
+
result += f"- Total pages: {len(reader.pages)}\n\n"
|
| 426 |
+
|
| 427 |
+
# Extract text by page (limit to first 5 pages)
|
| 428 |
+
max_pages = min(5, len(reader.pages))
|
| 429 |
+
all_text = ""
|
| 430 |
+
|
| 431 |
+
for i in range(max_pages):
|
| 432 |
+
try:
|
| 433 |
+
page = reader.pages[i]
|
| 434 |
+
page_text = page.extract_text()
|
| 435 |
+
|
| 436 |
+
current_page_text = f"### Page {i+1}\n\n"
|
| 437 |
+
if page_text and len(page_text.strip()) > 0:
|
| 438 |
+
# Limit to 1500 characters per page
|
| 439 |
+
if len(page_text) > 1500:
|
| 440 |
+
current_page_text += page_text[:1500] + "...(truncated)...\n\n"
|
| 441 |
+
else:
|
| 442 |
+
current_page_text += page_text + "\n\n"
|
| 443 |
+
else:
|
| 444 |
+
current_page_text += "(No text could be extracted from this page)\n\n"
|
| 445 |
+
|
| 446 |
+
all_text += current_page_text
|
| 447 |
+
|
| 448 |
+
# If total text is too long, break
|
| 449 |
+
if len(all_text) > 8000:
|
| 450 |
+
all_text += "...(truncating remaining pages; PDF is too large)...\n\n"
|
| 451 |
+
break
|
| 452 |
+
|
| 453 |
+
except Exception as page_err:
|
| 454 |
+
logging.error(f"Error processing PDF page {i+1}: {str(page_err)}")
|
| 455 |
+
all_text += f"### Page {i+1}\n\n(Error extracting content: {str(page_err)})\n\n"
|
| 456 |
+
|
| 457 |
+
if len(reader.pages) > max_pages:
|
| 458 |
+
all_text += f"\nNote: Only the first {max_pages} pages are shown out of {len(reader.pages)} total.\n\n"
|
| 459 |
+
|
| 460 |
+
result += "### PDF Content\n\n" + all_text
|
| 461 |
+
return result
|
| 462 |
+
|
| 463 |
+
except Exception as e:
|
| 464 |
+
logging.error(f"PDF file processing error: {str(e)}")
|
| 465 |
+
return f"## PDF File: {file.name}\n\nError occurred: {str(e)}\n\nThis PDF file cannot be processed."
|
| 466 |
+
|
| 467 |
+
def process_uploaded_files(files):
|
| 468 |
+
"""Combine the contents of all uploaded files into one string."""
|
| 469 |
+
if not files:
|
| 470 |
+
return None
|
| 471 |
+
|
| 472 |
+
result = "# Uploaded File Contents\n\n"
|
| 473 |
+
result += "Below is the content from the files provided by the user. Integrate this data as a main source of information for the blog.\n\n"
|
| 474 |
+
|
| 475 |
+
for file in files:
|
| 476 |
+
try:
|
| 477 |
+
ext = file.name.split('.')[-1].lower()
|
| 478 |
+
if ext == 'txt':
|
| 479 |
+
result += process_text_file(file) + "\n\n---\n\n"
|
| 480 |
+
elif ext == 'csv':
|
| 481 |
+
result += process_csv_file(file) + "\n\n---\n\n"
|
| 482 |
+
elif ext == 'pdf':
|
| 483 |
+
result += process_pdf_file(file) + "\n\n---\n\n"
|
| 484 |
+
else:
|
| 485 |
+
result += f"### Unsupported File: {file.name}\n\n---\n\n"
|
| 486 |
+
except Exception as e:
|
| 487 |
+
logging.error(f"File processing error {file.name}: {e}")
|
| 488 |
+
result += f"### File processing error: {file.name}\n\nError: {e}\n\n---\n\n"
|
| 489 |
+
|
| 490 |
+
return result
|
| 491 |
+
|
| 492 |
+
# ββββββββββββββββββββββββββββββββ Image & Utility βββββββββββββββββββββββββ
|
| 493 |
def generate_image(prompt, w=768, h=768, g=3.5, steps=30, seed=3):
|
| 494 |
+
"""Image generation function."""
|
| 495 |
+
if not prompt:
|
| 496 |
+
return None, "Insufficient prompt"
|
| 497 |
try:
|
| 498 |
res = Client(IMAGE_API_URL).predict(
|
| 499 |
prompt=prompt, width=w, height=h, guidance=g,
|
| 500 |
inference_steps=steps, seed=seed,
|
| 501 |
do_img2img=False, init_image=None,
|
| 502 |
image2image_strength=0.8, resize_img=True,
|
| 503 |
+
api_name="/generate_image"
|
| 504 |
+
)
|
| 505 |
return res[0], f"Seed: {res[1]}"
|
| 506 |
except Exception as e:
|
| 507 |
+
logging.error(e)
|
| 508 |
+
return None, str(e)
|
| 509 |
|
| 510 |
+
def extract_image_prompt(blog_text: str, topic: str):
|
| 511 |
+
"""
|
| 512 |
+
Analyze the blog content (blog_text) to generate a one-line English image prompt
|
| 513 |
+
related to the topic.
|
| 514 |
+
"""
|
| 515 |
+
client = get_anthropic_client()
|
| 516 |
+
sys = f"Generate a single-line English image prompt from the following text:\nTopic: {topic}"
|
| 517 |
try:
|
| 518 |
+
# Simple one-time call
|
| 519 |
res = client.messages.create(
|
| 520 |
model="claude-3-7-sonnet-20250219",
|
| 521 |
+
max_tokens=80,
|
| 522 |
+
system=sys,
|
| 523 |
+
messages=[{"role": "user", "content": blog_text}]
|
| 524 |
)
|
| 525 |
return res.content[0].text.strip()
|
| 526 |
except Exception:
|
| 527 |
+
# Fallback prompt
|
| 528 |
return f"A professional photo related to {topic}, high quality"
|
| 529 |
|
| 530 |
def md_to_html(md: str, title="Ginigen Blog"):
|
| 531 |
+
"""Convert Markdown to HTML."""
|
| 532 |
return f"<!DOCTYPE html><html><head><title>{title}</title><meta charset='utf-8'></head><body>{markdown.markdown(md)}</body></html>"
|
| 533 |
|
| 534 |
def keywords(text: str, top=5):
|
| 535 |
+
"""Simple keyword extraction."""
|
| 536 |
+
cleaned = re.sub(r"[^κ°-ν£a-zA-Z0-9\s]", "", text)
|
| 537 |
+
return " ".join(cleaned.split()[:top])
|
| 538 |
|
| 539 |
# ββββββββββββββββββββββββββββββββ Streamlit UI ββββββββββββββββββββββββββββ
|
| 540 |
def ginigen_app():
|
| 541 |
+
st.title("Ginigen Blog")
|
| 542 |
|
| 543 |
+
# Set default session state
|
| 544 |
if "ai_model" not in st.session_state:
|
| 545 |
st.session_state.ai_model = "claude-3-7-sonnet-20250219"
|
| 546 |
if "messages" not in st.session_state:
|
|
|
|
| 549 |
st.session_state.auto_save = True
|
| 550 |
if "generate_image" not in st.session_state:
|
| 551 |
st.session_state.generate_image = False
|
| 552 |
+
if "web_search_enabled" not in st.session_state:
|
| 553 |
+
st.session_state.web_search_enabled = True
|
| 554 |
if "blog_template" not in st.session_state:
|
| 555 |
+
st.session_state.blog_template = "ginigen" # Ginigen recommended style by default
|
| 556 |
if "blog_tone" not in st.session_state:
|
| 557 |
st.session_state.blog_tone = "professional"
|
| 558 |
if "word_count" not in st.session_state:
|
| 559 |
st.session_state.word_count = 1750
|
| 560 |
|
| 561 |
+
# Sidebar UI
|
| 562 |
sb = st.sidebar
|
| 563 |
+
sb.title("Blog Settings")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 564 |
|
| 565 |
+
sb.subheader("Blog Style Settings")
|
| 566 |
+
sb.selectbox(
|
| 567 |
+
"Blog Template",
|
| 568 |
+
options=list(BLOG_TEMPLATES.keys()),
|
| 569 |
+
format_func=lambda x: BLOG_TEMPLATES[x],
|
| 570 |
+
key="blog_template"
|
| 571 |
+
)
|
| 572 |
|
| 573 |
+
sb.selectbox(
|
| 574 |
+
"Blog Tone",
|
| 575 |
+
options=list(BLOG_TONES.keys()),
|
| 576 |
+
format_func=lambda x: BLOG_TONES[x],
|
| 577 |
+
key="blog_tone"
|
| 578 |
+
)
|
| 579 |
|
| 580 |
+
sb.slider("Blog Length (word count)", 800, 3000, key="word_count")
|
|
|
|
| 581 |
|
| 582 |
+
# Example topics
|
| 583 |
+
sb.subheader("Example Topics")
|
| 584 |
+
c1, c2, c3 = sb.columns(3)
|
| 585 |
+
if c1.button("Real Estate Tax", key="ex1"):
|
|
|
|
| 586 |
process_example(EXAMPLE_TOPICS["example1"])
|
| 587 |
+
if c2.button("Summer Festivals", key="ex2"):
|
|
|
|
| 588 |
process_example(EXAMPLE_TOPICS["example2"])
|
| 589 |
+
if c3.button("Investment Guide", key="ex3"):
|
|
|
|
| 590 |
process_example(EXAMPLE_TOPICS["example3"])
|
| 591 |
|
| 592 |
+
sb.subheader("Other Settings")
|
| 593 |
+
sb.toggle("Auto Save", key="auto_save")
|
| 594 |
+
sb.toggle("Auto Image Generation", key="generate_image")
|
| 595 |
+
|
| 596 |
+
web_search_enabled = sb.toggle("Use Web Search", value=st.session_state.web_search_enabled)
|
| 597 |
+
st.session_state.web_search_enabled = web_search_enabled
|
| 598 |
|
| 599 |
+
if web_search_enabled:
|
| 600 |
+
st.sidebar.info("β
Web search results will be integrated into the blog.")
|
|
|
|
|
|
|
| 601 |
|
| 602 |
+
# Download the latest blog (markdown/HTML)
|
| 603 |
latest_blog = next(
|
| 604 |
+
(m["content"] for m in reversed(st.session_state.messages)
|
| 605 |
+
if m["role"] == "assistant" and m["content"].strip()),
|
| 606 |
+
None
|
| 607 |
+
)
|
| 608 |
if latest_blog:
|
| 609 |
+
title_match = re.search(r"# (.*?)(\n|$)", latest_blog)
|
| 610 |
+
title = title_match.group(1).strip() if title_match else "blog"
|
| 611 |
+
sb.subheader("Download Latest Blog")
|
| 612 |
+
d1, d2 = sb.columns(2)
|
| 613 |
+
d1.download_button("Download as Markdown", latest_blog,
|
| 614 |
file_name=f"{title}.md", mime="text/markdown")
|
| 615 |
+
d2.download_button("Download as HTML", md_to_html(latest_blog, title),
|
| 616 |
file_name=f"{title}.html", mime="text/html")
|
| 617 |
|
| 618 |
+
# JSON conversation record upload
|
| 619 |
+
up = sb.file_uploader("Load Conversation History (.json)", type=["json"], key="json_uploader")
|
| 620 |
if up:
|
| 621 |
try:
|
| 622 |
st.session_state.messages = json.load(up)
|
| 623 |
+
sb.success("Conversation history loaded successfully")
|
| 624 |
except Exception as e:
|
| 625 |
+
sb.error(f"Failed to load: {e}")
|
| 626 |
+
|
| 627 |
+
# JSON conversation record download
|
| 628 |
+
if sb.button("Download Conversation as JSON"):
|
| 629 |
+
sb.download_button(
|
| 630 |
+
"Save",
|
| 631 |
+
data=json.dumps(st.session_state.messages, ensure_ascii=False, indent=2),
|
| 632 |
+
file_name="chat_history.json",
|
| 633 |
+
mime="application/json"
|
| 634 |
+
)
|
| 635 |
|
| 636 |
+
# File Upload
|
| 637 |
+
st.subheader("File Upload")
|
| 638 |
+
uploaded_files = st.file_uploader(
|
| 639 |
+
"Upload files to be referenced in your blog (txt, csv, pdf)",
|
| 640 |
+
type=["txt", "csv", "pdf"],
|
| 641 |
+
accept_multiple_files=True,
|
| 642 |
+
key="file_uploader"
|
| 643 |
+
)
|
| 644 |
+
|
| 645 |
+
if uploaded_files:
|
| 646 |
+
file_count = len(uploaded_files)
|
| 647 |
+
st.success(f"{file_count} files uploaded. They will be referenced in the blog.")
|
| 648 |
+
|
| 649 |
+
with st.expander("Preview Uploaded Files", expanded=False):
|
| 650 |
+
for idx, file in enumerate(uploaded_files):
|
| 651 |
+
st.write(f"**File Name:** {file.name}")
|
| 652 |
+
ext = file.name.split('.')[-1].lower()
|
| 653 |
+
|
| 654 |
+
if ext == 'txt':
|
| 655 |
+
preview = file.read(1000).decode('utf-8', errors='ignore')
|
| 656 |
+
file.seek(0)
|
| 657 |
+
st.text_area(
|
| 658 |
+
f"Preview of {file.name}",
|
| 659 |
+
preview + ("..." if len(preview) >= 1000 else ""),
|
| 660 |
+
height=150
|
| 661 |
+
)
|
| 662 |
+
elif ext == 'csv':
|
| 663 |
+
try:
|
| 664 |
+
df = pd.read_csv(file)
|
| 665 |
+
file.seek(0)
|
| 666 |
+
st.write("CSV Preview (up to 5 rows)")
|
| 667 |
+
st.dataframe(df.head(5))
|
| 668 |
+
except Exception as e:
|
| 669 |
+
st.error(f"CSV preview failed: {e}")
|
| 670 |
+
elif ext == 'pdf':
|
| 671 |
+
try:
|
| 672 |
+
file_bytes = file.read()
|
| 673 |
+
file.seek(0)
|
| 674 |
+
|
| 675 |
+
pdf_file = io.BytesIO(file_bytes)
|
| 676 |
+
reader = PyPDF2.PdfReader(pdf_file, strict=False)
|
| 677 |
+
|
| 678 |
+
pc = len(reader.pages)
|
| 679 |
+
st.write(f"PDF File: {pc} pages")
|
| 680 |
+
|
| 681 |
+
if pc > 0:
|
| 682 |
+
try:
|
| 683 |
+
page_text = reader.pages[0].extract_text()
|
| 684 |
+
preview = page_text[:500] if page_text else "(No text extracted)"
|
| 685 |
+
st.text_area("Preview of the first page", preview + "...", height=150)
|
| 686 |
+
except:
|
| 687 |
+
st.warning("Failed to extract text from the first page")
|
| 688 |
+
except Exception as e:
|
| 689 |
+
st.error(f"PDF preview failed: {e}")
|
| 690 |
+
|
| 691 |
+
if idx < file_count - 1:
|
| 692 |
+
st.divider()
|
| 693 |
+
|
| 694 |
+
# Display existing messages
|
| 695 |
for m in st.session_state.messages:
|
| 696 |
with st.chat_message(m["role"]):
|
| 697 |
st.markdown(m["content"])
|
| 698 |
if "image" in m:
|
| 699 |
st.image(m["image"], caption=m.get("image_caption", ""))
|
| 700 |
|
| 701 |
+
# User input
|
| 702 |
+
prompt = st.chat_input("Enter a blog topic or keywords.")
|
|
|
|
| 703 |
if prompt:
|
| 704 |
+
process_input(prompt, uploaded_files)
|
|
|
|
| 705 |
|
| 706 |
def process_example(topic):
|
| 707 |
+
"""Process the selected example topic."""
|
| 708 |
+
process_input(topic, [])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 709 |
|
| 710 |
+
def process_input(prompt: str, uploaded_files):
|
| 711 |
+
# Add user's message if it doesn't already exist
|
| 712 |
+
if not any(m["role"] == "user" and m["content"] == prompt for m in st.session_state.messages):
|
| 713 |
+
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 714 |
+
|
| 715 |
+
with st.chat_message("user"):
|
| 716 |
+
st.markdown(prompt)
|
| 717 |
+
|
| 718 |
with st.chat_message("assistant"):
|
| 719 |
+
placeholder = st.empty()
|
| 720 |
+
answer = ""
|
| 721 |
+
|
| 722 |
+
use_web_search = st.session_state.web_search_enabled
|
| 723 |
+
has_uploaded_files = bool(uploaded_files) and len(uploaded_files) > 0
|
| 724 |
|
| 725 |
try:
|
| 726 |
+
client = get_anthropic_client()
|
| 727 |
+
|
| 728 |
+
# Prepare conversation messages
|
| 729 |
+
messages = [{"role": m["role"], "content": m["content"]} for m in st.session_state.messages]
|
| 730 |
+
|
| 731 |
+
# Web search
|
| 732 |
+
if use_web_search:
|
| 733 |
+
with st.spinner("Performing web search..."):
|
| 734 |
+
sr = do_web_search(keywords(prompt, top=5))
|
| 735 |
+
if sr:
|
| 736 |
+
messages.append({"role": "user", "content": sr})
|
| 737 |
+
|
| 738 |
+
# Process uploaded files β content
|
| 739 |
+
file_content = None
|
| 740 |
+
if has_uploaded_files:
|
| 741 |
+
with st.spinner("Analyzing uploaded files..."):
|
| 742 |
+
file_content = process_uploaded_files(uploaded_files)
|
| 743 |
+
|
| 744 |
+
# Build system prompt
|
| 745 |
sys_prompt = get_system_prompt(
|
| 746 |
template=st.session_state.blog_template,
|
| 747 |
tone=st.session_state.blog_tone,
|
| 748 |
word_count=st.session_state.word_count,
|
| 749 |
+
include_search_results=use_web_search,
|
| 750 |
+
include_uploaded_files=has_uploaded_files
|
| 751 |
)
|
| 752 |
|
| 753 |
+
# If we have file content, append it to the system prompt
|
| 754 |
+
if file_content:
|
| 755 |
+
sys_prompt += (
|
| 756 |
+
"\n\n"
|
| 757 |
+
"Below is the content of the uploaded file(s). Please make sure to integrate it thoroughly in the blog:\n\n"
|
| 758 |
+
f"{file_content}\n\n"
|
| 759 |
+
"Ensure the file content is accurately reflected in the blog.\n"
|
| 760 |
+
)
|
| 761 |
|
| 762 |
+
# Append additional user message about file usage
|
| 763 |
+
if has_uploaded_files:
|
| 764 |
+
extra_user_msg = (
|
| 765 |
+
f"{prompt}\n\n"
|
| 766 |
+
"Additional note: Please make sure to reference the uploaded file content in the blog. "
|
| 767 |
+
"Use and analyze any data, statistics, or text included in the file(s)."
|
| 768 |
+
)
|
| 769 |
+
messages.append({"role": "user", "content": extra_user_msg})
|
| 770 |
+
|
| 771 |
+
# Claude streaming
|
| 772 |
with client.messages.stream(
|
| 773 |
+
model=st.session_state.ai_model,
|
| 774 |
+
max_tokens=MAX_TOKENS,
|
| 775 |
system=sys_prompt,
|
| 776 |
messages=messages
|
| 777 |
) as stream:
|
|
|
|
| 779 |
answer += t or ""
|
| 780 |
placeholder.markdown(answer + "β")
|
| 781 |
placeholder.markdown(answer)
|
| 782 |
+
|
| 783 |
+
# Image generation option
|
| 784 |
answer_entry_saved = False
|
| 785 |
if st.session_state.generate_image:
|
| 786 |
+
with st.spinner("Generating image..."):
|
| 787 |
ip = extract_image_prompt(answer, prompt)
|
| 788 |
img, cap = generate_image(ip)
|
| 789 |
if img:
|
| 790 |
st.image(img, caption=cap)
|
| 791 |
+
st.session_state.messages.append({
|
| 792 |
+
"role": "assistant",
|
| 793 |
+
"content": answer,
|
| 794 |
+
"image": img,
|
| 795 |
+
"image_caption": cap
|
| 796 |
+
})
|
| 797 |
answer_entry_saved = True
|
| 798 |
+
|
| 799 |
+
# Save the answer
|
| 800 |
if not answer_entry_saved:
|
| 801 |
+
st.session_state.messages.append({"role": "assistant", "content": answer})
|
| 802 |
+
|
| 803 |
+
# Download buttons
|
| 804 |
+
st.subheader("Download This Blog")
|
| 805 |
+
c1, c2 = st.columns(2)
|
| 806 |
+
c1.download_button(
|
| 807 |
+
"Markdown",
|
| 808 |
+
data=answer,
|
| 809 |
+
file_name=f"{prompt[:30]}.md",
|
| 810 |
+
mime="text/markdown"
|
| 811 |
+
)
|
| 812 |
+
c2.download_button(
|
| 813 |
+
"HTML",
|
| 814 |
+
data=md_to_html(answer, prompt[:30]),
|
| 815 |
+
file_name=f"{prompt[:30]}.html",
|
| 816 |
+
mime="text/html"
|
| 817 |
+
)
|
| 818 |
+
|
| 819 |
+
# Auto save
|
| 820 |
if st.session_state.auto_save and st.session_state.messages:
|
| 821 |
try:
|
| 822 |
fn = f"chat_history_auto_{datetime.now():%Y%m%d_%H%M%S}.json"
|
| 823 |
with open(fn, "w", encoding="utf-8") as fp:
|
| 824 |
+
json.dump(st.session_state.messages, fp, ensure_ascii=False, indent=2)
|
|
|
|
| 825 |
except Exception as e:
|
| 826 |
+
logging.error(f"Auto-save failed: {e}")
|
| 827 |
+
|
| 828 |
except anthropic.BadRequestError as e:
|
| 829 |
error_message = str(e)
|
| 830 |
if "credit balance is too low" in error_message:
|
| 831 |
+
placeholder.error("β οΈ Insufficient API credits: Please top up your Anthropic API account.")
|
| 832 |
+
ans = "Unable to generate blog due to low API credits. Please recharge and try again."
|
| 833 |
else:
|
| 834 |
+
placeholder.error(f"API request error: {error_message}")
|
| 835 |
+
ans = f"An error occurred while calling the API: {error_message}"
|
| 836 |
+
st.session_state.messages.append({"role": "assistant", "content": ans})
|
|
|
|
| 837 |
|
| 838 |
except Exception as e:
|
| 839 |
error_message = str(e)
|
| 840 |
+
placeholder.error(f"An error occurred: {error_message}")
|
| 841 |
+
ans = f"An error occurred while processing your request: {error_message}"
|
| 842 |
+
st.session_state.messages.append({"role": "assistant", "content": ans})
|
| 843 |
|
| 844 |
+
# ββββββββββββββββββββββββββββββββ main ββββββββββββββββββββββββββββββββββββ
|
| 845 |
+
def main():
|
| 846 |
+
ginigen_app()
|
| 847 |
|
| 848 |
if __name__ == "__main__":
|
| 849 |
+
main()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|