Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -809,8 +809,15 @@ def process_example(topic):
|
|
| 809 |
process_input(topic, [])
|
| 810 |
|
| 811 |
# ββ μμ λ process_input μ 체 ββββββββββββββββββββββββββββββββββββββββββββ
|
|
|
|
|
|
|
| 812 |
def process_input(prompt: str, uploaded_files):
|
| 813 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 814 |
if not any(m["role"] == "user" and m["content"] == prompt
|
| 815 |
for m in st.session_state.messages):
|
| 816 |
st.session_state.messages.append({"role": "user", "content": prompt})
|
|
@@ -818,27 +825,34 @@ def process_input(prompt: str, uploaded_files):
|
|
| 818 |
with st.chat_message("user"):
|
| 819 |
st.markdown(prompt)
|
| 820 |
|
|
|
|
| 821 |
with st.chat_message("assistant"):
|
| 822 |
-
placeholder
|
| 823 |
-
message_placeholder
|
| 824 |
-
full_response
|
| 825 |
|
| 826 |
use_web_search = st.session_state.web_search_enabled
|
| 827 |
has_uploaded = bool(uploaded_files)
|
| 828 |
|
| 829 |
try:
|
|
|
|
| 830 |
status = st.status("Preparing to generate ideasβ¦")
|
| 831 |
status.update(label="Initializing modelβ¦")
|
| 832 |
-
|
| 833 |
client = get_openai_client()
|
| 834 |
|
| 835 |
-
#
|
| 836 |
selected_cat = st.session_state.get("category_focus", "(None)")
|
| 837 |
if selected_cat == "(None)":
|
| 838 |
selected_cat = None
|
| 839 |
sys_prompt = get_idea_system_prompt(selected_category=selected_cat)
|
| 840 |
|
| 841 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 842 |
search_content = None
|
| 843 |
if use_web_search:
|
| 844 |
status.update(label="Searching the webβ¦")
|
|
@@ -851,13 +865,76 @@ def process_input(prompt: str, uploaded_files):
|
|
| 851 |
with st.spinner("Processing filesβ¦"):
|
| 852 |
file_content = process_uploaded_files(uploaded_files)
|
| 853 |
|
| 854 |
-
#
|
| 855 |
user_content = prompt
|
| 856 |
if search_content:
|
| 857 |
user_content += "\n\n" + search_content
|
| 858 |
if file_content:
|
| 859 |
user_content += "\n\n" + file_content
|
| 860 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 861 |
# ---- μΉ΄ν
κ³ λ¦¬/νμνλͺ© 컨ν
μ€νΈ μΆκ° ---------------------------
|
| 862 |
def category_context(sel):
|
| 863 |
if sel: # νΉμ μΉ΄ν
κ³ λ¦¬ μ ν μ
|
|
@@ -919,28 +996,26 @@ def process_input(prompt: str, uploaded_files):
|
|
| 919 |
"image_caption": f"μμ΄λμ΄ μκ°ν β {cap}"
|
| 920 |
})
|
| 921 |
|
| 922 |
-
# ββ
|
| 923 |
def write_output(md_text: str, prompt: str):
|
| 924 |
-
|
| 925 |
-
# β λν κΈ°λ‘μ λ΅λ³ μ μ₯
|
| 926 |
st.session_state.messages.append({"role": "assistant", "content": md_text})
|
| 927 |
|
| 928 |
# β‘ λ€μ΄λ‘λ λ²νΌ
|
| 929 |
st.subheader("Download This Output")
|
| 930 |
col_md, col_html = st.columns(2)
|
| 931 |
-
col_md.download_button(
|
| 932 |
-
|
| 933 |
-
|
| 934 |
-
|
| 935 |
-
|
| 936 |
-
file_name=f"{prompt[:30]}.html", mime="text/html")
|
| 937 |
-
|
| 938 |
# β’ JSON μλ μ μ₯
|
| 939 |
if st.session_state.auto_save:
|
| 940 |
fn = f"chat_history_auto_{datetime.now():%Y%m%d_%H%M%S}.json"
|
| 941 |
with open(fn, "w", encoding="utf-8") as fp:
|
| 942 |
-
json.dump(st.session_state.messages, fp,
|
| 943 |
-
|
|
|
|
| 944 |
|
| 945 |
|
| 946 |
except Exception as e:
|
|
|
|
| 809 |
process_input(topic, [])
|
| 810 |
|
| 811 |
# ββ μμ λ process_input μ 체 ββββββββββββββββββββββββββββββββββββββββββββ
|
| 812 |
+
|
| 813 |
+
# ββ μμ λ process_input (μ€λ³΅ μΆλ ₯ μ κ±° + μΉ΄ν
κ³ λ¦¬ DB μ λ¬ + μ΄λ―Έμ§ ν둬ννΈ ν΅ν©) ββ
|
| 814 |
def process_input(prompt: str, uploaded_files):
|
| 815 |
+
"""
|
| 816 |
+
1. μ¬μ©μ λ©μμ§ κΈ°λ‘ λ° UI μΆλ ₯
|
| 817 |
+
2. μΉκ²μΒ·νμΌλ΄μ©Β·μΉ΄ν
κ³ λ¦¬ DBλ₯Ό ν¬ν¨ν GPT νΈμΆ
|
| 818 |
+
3. μ΅μ’
μλ΅ μ€νΈλ¦¬λ°, μ΄λ―Έμ§ μμ±(μ ν), κ²°κ³Ό μ μ₯Β·λ€μ΄λ‘λ(1νλ§)
|
| 819 |
+
"""
|
| 820 |
+
# ββ μ¬μ©μ λ©μμ§ μ μ₯ βββββββββββββββββββββββββββββββββββββββββββββββ
|
| 821 |
if not any(m["role"] == "user" and m["content"] == prompt
|
| 822 |
for m in st.session_state.messages):
|
| 823 |
st.session_state.messages.append({"role": "user", "content": prompt})
|
|
|
|
| 825 |
with st.chat_message("user"):
|
| 826 |
st.markdown(prompt)
|
| 827 |
|
| 828 |
+
# ββ μ΄μμ€ν΄νΈ μλ΅ μμ ββββββββββββββββββββββββββββββββββββββββββββ
|
| 829 |
with st.chat_message("assistant"):
|
| 830 |
+
placeholder = st.empty()
|
| 831 |
+
message_placeholder = st.empty()
|
| 832 |
+
full_response = ""
|
| 833 |
|
| 834 |
use_web_search = st.session_state.web_search_enabled
|
| 835 |
has_uploaded = bool(uploaded_files)
|
| 836 |
|
| 837 |
try:
|
| 838 |
+
# β λͺ¨λΈ μ΄κΈ°ν
|
| 839 |
status = st.status("Preparing to generate ideasβ¦")
|
| 840 |
status.update(label="Initializing modelβ¦")
|
|
|
|
| 841 |
client = get_openai_client()
|
| 842 |
|
| 843 |
+
# β‘ μμ€ν
ν둬ννΈ + μΉ΄ν
κ³ λ¦¬ DB
|
| 844 |
selected_cat = st.session_state.get("category_focus", "(None)")
|
| 845 |
if selected_cat == "(None)":
|
| 846 |
selected_cat = None
|
| 847 |
sys_prompt = get_idea_system_prompt(selected_category=selected_cat)
|
| 848 |
|
| 849 |
+
def category_context(sel):
|
| 850 |
+
if sel:
|
| 851 |
+
return json.dumps({sel: physical_transformation_categories[sel]},
|
| 852 |
+
ensure_ascii=False)
|
| 853 |
+
return "ALL_CATEGORIES: " + ", ".join(physical_transformation_categories.keys())
|
| 854 |
+
|
| 855 |
+
# β’ (μ ν) μΉ κ²μ Β· νμΌ μ²λ¦¬
|
| 856 |
search_content = None
|
| 857 |
if use_web_search:
|
| 858 |
status.update(label="Searching the webβ¦")
|
|
|
|
| 865 |
with st.spinner("Processing filesβ¦"):
|
| 866 |
file_content = process_uploaded_files(uploaded_files)
|
| 867 |
|
| 868 |
+
# β£ μ¬μ©μ λ©μμ§ κ²°ν©
|
| 869 |
user_content = prompt
|
| 870 |
if search_content:
|
| 871 |
user_content += "\n\n" + search_content
|
| 872 |
if file_content:
|
| 873 |
user_content += "\n\n" + file_content
|
| 874 |
|
| 875 |
+
api_messages = [
|
| 876 |
+
{"role": "system", "content": sys_prompt},
|
| 877 |
+
{"role": "system", "name": "category_db",
|
| 878 |
+
"content": category_context(selected_cat)},
|
| 879 |
+
{"role": "user", "content": user_content},
|
| 880 |
+
]
|
| 881 |
+
|
| 882 |
+
# β€ GPT μ€νΈλ¦¬λ° νΈμΆ
|
| 883 |
+
status.update(label="Generating ideasβ¦")
|
| 884 |
+
stream = client.chat.completions.create(
|
| 885 |
+
model="gpt-4.1-mini",
|
| 886 |
+
messages=api_messages,
|
| 887 |
+
temperature=1,
|
| 888 |
+
max_tokens=MAX_TOKENS,
|
| 889 |
+
top_p=1,
|
| 890 |
+
stream=True
|
| 891 |
+
)
|
| 892 |
+
for chunk in stream:
|
| 893 |
+
if chunk.choices and chunk.choices[0].delta.content:
|
| 894 |
+
full_response += chunk.choices[0].delta.content
|
| 895 |
+
message_placeholder.markdown(full_response + "β")
|
| 896 |
+
message_placeholder.markdown(full_response)
|
| 897 |
+
status.update(label="Ideas created!", state="complete")
|
| 898 |
+
|
| 899 |
+
# β₯ μ΄λ―Έμ§ μμ± (CCM ν€λ© or λ κ±°μ ν¨ν΄ μλ νμ§)
|
| 900 |
+
if st.session_state.generate_image and full_response:
|
| 901 |
+
ccm_match = re.search(r"###\s*μ΄λ―Έμ§\s*ν둬ννΈ\s*\n+([^\n]+)",
|
| 902 |
+
full_response, flags=re.IGNORECASE)
|
| 903 |
+
legacy_match = None
|
| 904 |
+
if not ccm_match:
|
| 905 |
+
legacy_match = re.search(
|
| 906 |
+
r"\|\s*(?:\*\*)?Image\s+Prompt(?:\*\*)?\s*\|\s*([^|\n]+)",
|
| 907 |
+
full_response, flags=re.IGNORECASE) \
|
| 908 |
+
or re.search(r"(?i)Image\s+Prompt\s*[:\-]\s*([^\n]+)",
|
| 909 |
+
full_response)
|
| 910 |
+
match = ccm_match or legacy_match
|
| 911 |
+
if match:
|
| 912 |
+
raw_prompt = re.sub(r"[\r\n`\"'\\]", " ", match.group(1)).strip()
|
| 913 |
+
with st.spinner("μμ΄λμ΄ μ΄λ―Έμ§ μμ± μ€β¦"):
|
| 914 |
+
img, cap = generate_image(raw_prompt)
|
| 915 |
+
if img:
|
| 916 |
+
st.image(img, caption=f"μμ΄λμ΄ μκ°ν β {cap}")
|
| 917 |
+
st.session_state.messages.append({
|
| 918 |
+
"role": "assistant",
|
| 919 |
+
"content": "",
|
| 920 |
+
"image": img,
|
| 921 |
+
"image_caption": f"μμ΄λμ΄ μκ°ν β {cap}"
|
| 922 |
+
})
|
| 923 |
+
|
| 924 |
+
# β¦ κ²°κ³Όλ₯Ό ν λ²λ§ μ μ₯Β·λ€μ΄λ‘λΒ·μλλ°±μ
|
| 925 |
+
write_output(full_response, prompt)
|
| 926 |
+
|
| 927 |
+
# ββ μμΈ μ²λ¦¬ (μ€λ³΅ μΆλ ₯ λ°©μ§) ββββββββββββββββββββββββββββββββββ
|
| 928 |
+
except Exception as e:
|
| 929 |
+
err = str(e)
|
| 930 |
+
placeholder.error(f"β οΈ Error: {err}")
|
| 931 |
+
logging.error(err)
|
| 932 |
+
st.session_state.messages.append({
|
| 933 |
+
"role": "assistant",
|
| 934 |
+
"content": f"β οΈ μμ
μ€ μ€λ₯κ° λ°μνμ΅λλ€: {err}"
|
| 935 |
+
})
|
| 936 |
+
|
| 937 |
+
|
| 938 |
# ---- μΉ΄ν
κ³ λ¦¬/νμνλͺ© 컨ν
μ€νΈ μΆκ° ---------------------------
|
| 939 |
def category_context(sel):
|
| 940 |
if sel: # νΉμ μΉ΄ν
κ³ λ¦¬ μ ν μ
|
|
|
|
| 996 |
"image_caption": f"μμ΄λμ΄ μκ°ν β {cap}"
|
| 997 |
})
|
| 998 |
|
| 999 |
+
# ββ ν¬νΌ: κ²°κ³Όλ₯Ό ν λ²λ§ κΈ°λ‘Β·λ€μ΄λ‘λΒ·μ μ₯ βββββββββββββββββββββββββββββ
|
| 1000 |
def write_output(md_text: str, prompt: str):
|
| 1001 |
+
# β μ±ν
κΈ°λ‘ μ μ₯
|
|
|
|
| 1002 |
st.session_state.messages.append({"role": "assistant", "content": md_text})
|
| 1003 |
|
| 1004 |
# β‘ λ€μ΄λ‘λ λ²νΌ
|
| 1005 |
st.subheader("Download This Output")
|
| 1006 |
col_md, col_html = st.columns(2)
|
| 1007 |
+
col_md.download_button("Markdown", md_text,
|
| 1008 |
+
file_name=f"{prompt[:30]}.md", mime="text/markdown")
|
| 1009 |
+
col_html.download_button("HTML", md_to_html(md_text, prompt[:30]),
|
| 1010 |
+
file_name=f"{prompt[:30]}.html", mime="text/html")
|
| 1011 |
+
|
|
|
|
|
|
|
| 1012 |
# β’ JSON μλ μ μ₯
|
| 1013 |
if st.session_state.auto_save:
|
| 1014 |
fn = f"chat_history_auto_{datetime.now():%Y%m%d_%H%M%S}.json"
|
| 1015 |
with open(fn, "w", encoding="utf-8") as fp:
|
| 1016 |
+
json.dump(st.session_state.messages, fp, ensure_ascii=False, indent=2)
|
| 1017 |
+
|
| 1018 |
+
|
| 1019 |
|
| 1020 |
|
| 1021 |
except Exception as e:
|