Spaces:
Build error
Build error
Update app.py
Browse filestranslate it to Korean
app.py
CHANGED
|
@@ -6,36 +6,47 @@ from datetime import datetime
|
|
| 6 |
import requests
|
| 7 |
import sseclient
|
| 8 |
|
| 9 |
-
|
| 10 |
-
st.
|
|
|
|
| 11 |
|
| 12 |
-
# ---
|
| 13 |
-
api_key = st.text_input("π Upstage API Key (
|
| 14 |
-
user_input = st.text_input("π¬
|
| 15 |
|
| 16 |
-
|
|
|
|
| 17 |
st.markdown("""
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
""")
|
| 24 |
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
# --- Default prompt setting ---
|
| 28 |
default_prompt = "You are a helpful assistant."
|
| 29 |
|
| 30 |
-
# ---
|
| 31 |
if "default_messages" not in st.session_state:
|
| 32 |
st.session_state.default_messages = [{"role": "system", "content": default_prompt}]
|
| 33 |
if "custom_messages" not in st.session_state or st.session_state.custom_prompt != custom_prompt:
|
| 34 |
st.session_state.custom_messages = [{"role": "system", "content": custom_prompt}]
|
| 35 |
st.session_state.custom_prompt = custom_prompt
|
| 36 |
|
| 37 |
-
# --- Solar Pro API
|
| 38 |
def solar_pro_chat(messages, api_key):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
url = "https://api.upstage.ai/v1/chat/completions"
|
| 40 |
headers = {
|
| 41 |
"Authorization": f"Bearer {api_key}",
|
|
@@ -59,43 +70,57 @@ def solar_pro_chat(messages, api_key):
|
|
| 59 |
content = json.loads(event.data)["choices"][0]["delta"].get("content", "")
|
| 60 |
yield content
|
| 61 |
except Exception as e:
|
| 62 |
-
st.error("β οΈ
|
| 63 |
-
print(f"[SSE
|
| 64 |
continue
|
| 65 |
except requests.exceptions.RequestException as e:
|
| 66 |
-
st.error("β API
|
| 67 |
-
print(f"[API
|
| 68 |
return
|
| 69 |
|
| 70 |
-
# ---
|
| 71 |
-
if st.button("π
|
|
|
|
| 72 |
st.session_state.default_messages.append({"role": "user", "content": user_input})
|
| 73 |
st.session_state.custom_messages.append({"role": "user", "content": user_input})
|
| 74 |
|
|
|
|
| 75 |
col1, col2 = st.columns(2)
|
| 76 |
|
|
|
|
| 77 |
with col1:
|
| 78 |
-
st.subheader("πΉ
|
| 79 |
default_response = ""
|
| 80 |
default_area = st.empty()
|
| 81 |
-
with st.spinner("
|
| 82 |
for chunk in solar_pro_chat(st.session_state.default_messages, api_key):
|
| 83 |
default_response += chunk
|
| 84 |
-
default_area.markdown(f"**π€
|
| 85 |
st.session_state.default_messages.append({"role": "assistant", "content": default_response})
|
| 86 |
|
|
|
|
| 87 |
with col2:
|
| 88 |
-
st.subheader("πΈ
|
| 89 |
custom_response = ""
|
| 90 |
custom_area = st.empty()
|
| 91 |
-
with st.spinner("
|
| 92 |
for chunk in solar_pro_chat(st.session_state.custom_messages, api_key):
|
| 93 |
custom_response += chunk
|
| 94 |
-
custom_area.markdown(f"**π€
|
| 95 |
st.session_state.custom_messages.append({"role": "assistant", "content": custom_response})
|
| 96 |
|
| 97 |
-
# ---
|
| 98 |
def generate_csv(messages, prompt_label):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
rows = [{"role": "system", "content": prompt_label}]
|
| 100 |
for msg in messages:
|
| 101 |
if msg["role"] != "system":
|
|
@@ -105,21 +130,23 @@ def generate_csv(messages, prompt_label):
|
|
| 105 |
df.to_csv(output, index=False)
|
| 106 |
return output.getvalue()
|
| 107 |
|
|
|
|
| 108 |
now = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
|
| 109 |
-
|
|
|
|
|
|
|
| 110 |
col1, col2 = st.columns(2)
|
| 111 |
with col1:
|
| 112 |
st.download_button(
|
| 113 |
-
label="
|
| 114 |
data=generate_csv(st.session_state.default_messages, default_prompt),
|
| 115 |
file_name=f"default_chat_{now}.csv",
|
| 116 |
mime="text/csv",
|
| 117 |
)
|
| 118 |
with col2:
|
| 119 |
st.download_button(
|
| 120 |
-
label="
|
| 121 |
data=generate_csv(st.session_state.custom_messages, custom_prompt),
|
| 122 |
file_name=f"custom_chat_{now}.csv",
|
| 123 |
mime="text/csv",
|
| 124 |
)
|
| 125 |
-
|
|
|
|
| 6 |
import requests
|
| 7 |
import sseclient
|
| 8 |
|
| 9 |
+
# νμ΄μ§ μ€μ
|
| 10 |
+
st.set_page_config(page_title="Solar ν둬ννΈ λΉκ΅κΈ°", page_icon="π")
|
| 11 |
+
st.title("π μμ€ν
ν둬ννΈ λΉκ΅ μ±λ΄")
|
| 12 |
|
| 13 |
+
# --- μ
λ ₯ UI ---
|
| 14 |
+
api_key = st.text_input("π Upstage API Keyλ₯Ό μ
λ ₯νμΈμ ('up_'λ‘ μμ)", type="password")
|
| 15 |
+
user_input = st.text_input("π¬ μ¬μ©μ λ©μμ§λ₯Ό μ
λ ₯νμΈμ", "")
|
| 16 |
|
| 17 |
+
# --- μμ€ν
ν둬ννΈ μλ΄ λ° μ
λ ₯ ---
|
| 18 |
+
st.markdown("### π§ 컀μ€ν
μμ€ν
ν둬ννΈ")
|
| 19 |
st.markdown("""
|
| 20 |
+
**μμ€ν
ν둬ννΈλ 무μμΈκ°μ?**
|
| 21 |
+
μμ€ν
ν둬ννΈλ AIμκ² λνλ₯Ό μμνκΈ° μ μ μ£Όμ΄μ§λ νΉλ³ν μ§μλ¬Έμ
λλ€.
|
| 22 |
+
AIκ° μ΄λ€ μν μ΄λ λ§ν¬, μ±κ²©μΌλ‘ λνμ μν΄μΌ νλμ§λ₯Ό μλ €μ£Όλ μν μ ν©λλ€.
|
| 23 |
+
μλ₯Ό λ€μ΄, AIμκ² μμ λ°λ₯Έ λΉμμ²λΌ νλνκ² νκ±°λ, μ°½μμ μΈ μκ°, μ격ν λ¬Έλ² κ²μ¬μμ²λΌ νλνκ² λ§λ€ μ μμ΅λλ€.
|
| 24 |
+
κ°μ μ§λ¬Έμ΄λΌλ μμ€ν
ν둬ννΈμ λ°λΌ μμ ν λ€λ₯Έ μλ΅μ΄ λμ¬ μ μμ΅λλ€.
|
| 25 |
""")
|
| 26 |
|
| 27 |
+
# κΈ°λ³Έ μμ€ν
ν둬ννΈμ μ¬μ©μ 컀μ€ν
ν둬ννΈ μ
λ ₯
|
| 28 |
+
custom_prompt = st.text_area("βοΈ μλμ μνλ μμ€ν
ν둬ννΈλ₯Ό μ
λ ₯νμΈμ:", "You are a helpful assistant.", height=100)
|
|
|
|
| 29 |
default_prompt = "You are a helpful assistant."
|
| 30 |
|
| 31 |
+
# --- μΈμ
μν μ΄κΈ°ν ---
|
| 32 |
if "default_messages" not in st.session_state:
|
| 33 |
st.session_state.default_messages = [{"role": "system", "content": default_prompt}]
|
| 34 |
if "custom_messages" not in st.session_state or st.session_state.custom_prompt != custom_prompt:
|
| 35 |
st.session_state.custom_messages = [{"role": "system", "content": custom_prompt}]
|
| 36 |
st.session_state.custom_prompt = custom_prompt
|
| 37 |
|
| 38 |
+
# --- Solar Pro API νΈμΆ ν¨μ ---
|
| 39 |
def solar_pro_chat(messages, api_key):
|
| 40 |
+
"""
|
| 41 |
+
Solar Pro APIμ λ©μμ§λ₯Ό 보λ΄κ³ μ€νΈλ¦¬λ° ννλ‘ μλ΅μ λ°μμ€λ ν¨μμ
λλ€.
|
| 42 |
+
|
| 43 |
+
Parameters:
|
| 44 |
+
messages (list): μμ€ν
/μ¬μ©μ λ©μμ§ λͺ©λ‘
|
| 45 |
+
api_key (str): Upstage API ν€
|
| 46 |
+
|
| 47 |
+
Returns:
|
| 48 |
+
generator: AIμ μλ΅μ μ€νΈλ¦¬λ° ννλ‘ ν μ€μ© λ°ν
|
| 49 |
+
"""
|
| 50 |
url = "https://api.upstage.ai/v1/chat/completions"
|
| 51 |
headers = {
|
| 52 |
"Authorization": f"Bearer {api_key}",
|
|
|
|
| 70 |
content = json.loads(event.data)["choices"][0]["delta"].get("content", "")
|
| 71 |
yield content
|
| 72 |
except Exception as e:
|
| 73 |
+
st.error("β οΈ μλ΅ μ²λ¦¬ μ€ μ€λ₯κ° λ°μνμ΅λλ€.")
|
| 74 |
+
print(f"[SSE νμ± μ€λ₯] {e}")
|
| 75 |
continue
|
| 76 |
except requests.exceptions.RequestException as e:
|
| 77 |
+
st.error("β API νΈμΆ μ€ν¨: API ν€λ λ€νΈμν¬ μνλ₯Ό νμΈν΄μ£ΌμΈμ.")
|
| 78 |
+
print(f"[API μμ² μ€λ₯] {e}")
|
| 79 |
return
|
| 80 |
|
| 81 |
+
# --- λΉκ΅ μ€ν ---
|
| 82 |
+
if st.button("π μλ΅ λΉκ΅νκΈ°") and api_key and user_input:
|
| 83 |
+
# μ¬μ©μ μ
λ ₯μ λ©μμ§μ μΆκ°
|
| 84 |
st.session_state.default_messages.append({"role": "user", "content": user_input})
|
| 85 |
st.session_state.custom_messages.append({"role": "user", "content": user_input})
|
| 86 |
|
| 87 |
+
# λ κ°μ 컬λΌμΌλ‘ κ²°κ³Ό λλκΈ°
|
| 88 |
col1, col2 = st.columns(2)
|
| 89 |
|
| 90 |
+
# κΈ°λ³Έ ν둬ννΈ μλ΅
|
| 91 |
with col1:
|
| 92 |
+
st.subheader("πΉ κΈ°λ³Έ ν둬ννΈ")
|
| 93 |
default_response = ""
|
| 94 |
default_area = st.empty()
|
| 95 |
+
with st.spinner("κΈ°λ³Έ μλ΅ μμ± μ€..."):
|
| 96 |
for chunk in solar_pro_chat(st.session_state.default_messages, api_key):
|
| 97 |
default_response += chunk
|
| 98 |
+
default_area.markdown(f"**π€ λ΄:** {default_response}")
|
| 99 |
st.session_state.default_messages.append({"role": "assistant", "content": default_response})
|
| 100 |
|
| 101 |
+
# 컀μ€ν
ν둬ννΈ μλ΅
|
| 102 |
with col2:
|
| 103 |
+
st.subheader("πΈ 컀μ€ν
ν둬ννΈ")
|
| 104 |
custom_response = ""
|
| 105 |
custom_area = st.empty()
|
| 106 |
+
with st.spinner("컀μ€ν
μλ΅ μμ± μ€..."):
|
| 107 |
for chunk in solar_pro_chat(st.session_state.custom_messages, api_key):
|
| 108 |
custom_response += chunk
|
| 109 |
+
custom_area.markdown(f"**π€ λ΄:** {custom_response}")
|
| 110 |
st.session_state.custom_messages.append({"role": "assistant", "content": custom_response})
|
| 111 |
|
| 112 |
+
# --- μ±ν
λ΄μ CSVλ‘ μ μ₯ ---
|
| 113 |
def generate_csv(messages, prompt_label):
|
| 114 |
+
"""
|
| 115 |
+
μ£Όμ΄μ§ λ©μμ§λ₯Ό CSVλ‘ λ³ννλ ν¨μμ
λλ€.
|
| 116 |
+
|
| 117 |
+
Parameters:
|
| 118 |
+
messages (list): μμ€ν
/μ¬μ©μ/AI λ©μμ§ λ¦¬μ€νΈ
|
| 119 |
+
prompt_label (str): μμ€ν
ν둬ννΈ ν
μ€νΈ
|
| 120 |
+
|
| 121 |
+
Returns:
|
| 122 |
+
str: CSV νμμ λ¬Έμμ΄
|
| 123 |
+
"""
|
| 124 |
rows = [{"role": "system", "content": prompt_label}]
|
| 125 |
for msg in messages:
|
| 126 |
if msg["role"] != "system":
|
|
|
|
| 130 |
df.to_csv(output, index=False)
|
| 131 |
return output.getvalue()
|
| 132 |
|
| 133 |
+
# νμ¬ μκ°μΌλ‘ νμΌλͺ
μμ±
|
| 134 |
now = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
|
| 135 |
+
|
| 136 |
+
# --- λ€μ΄λ‘λ UI ---
|
| 137 |
+
st.markdown("### β¬οΈ μ±ν
λ΄μ λ€μ΄λ‘λ")
|
| 138 |
col1, col2 = st.columns(2)
|
| 139 |
with col1:
|
| 140 |
st.download_button(
|
| 141 |
+
label="κΈ°λ³Έ μλ΅ λ€μ΄λ‘λ",
|
| 142 |
data=generate_csv(st.session_state.default_messages, default_prompt),
|
| 143 |
file_name=f"default_chat_{now}.csv",
|
| 144 |
mime="text/csv",
|
| 145 |
)
|
| 146 |
with col2:
|
| 147 |
st.download_button(
|
| 148 |
+
label="컀μ€ν
μλ΅ λ€μ΄λ‘λ",
|
| 149 |
data=generate_csv(st.session_state.custom_messages, custom_prompt),
|
| 150 |
file_name=f"custom_chat_{now}.csv",
|
| 151 |
mime="text/csv",
|
| 152 |
)
|
|
|