Spaces:
Build error
Build error
| import streamlit as st | |
| import pandas as pd | |
| import json | |
| from io import StringIO | |
| from datetime import datetime | |
| import requests | |
| import sseclient | |
| # νμ΄μ§ μ€μ | |
| st.set_page_config(page_title="Solar ν둬ννΈ λΉκ΅κΈ°", page_icon="π") | |
| st.title("π μμ€ν ν둬ννΈ λΉκ΅ μ±λ΄") | |
| # --- μ λ ₯ UI --- | |
| api_key = st.text_input("π Upstage API Keyλ₯Ό μ λ ₯νμΈμ ('up_'λ‘ μμ)", type="password") | |
| user_input = st.text_input("π¬ μ¬μ©μ λ©μμ§λ₯Ό μ λ ₯νμΈμ", "") | |
| # --- μμ€ν ν둬ννΈ μλ΄ λ° μ λ ₯ --- | |
| st.markdown("### π§ 컀μ€ν μμ€ν ν둬ννΈ") | |
| st.markdown(""" | |
| **μμ€ν ν둬ννΈλ 무μμΈκ°μ?** | |
| μμ€ν ν둬ννΈλ AIμκ² λνλ₯Ό μμνκΈ° μ μ μ£Όμ΄μ§λ νΉλ³ν μ§μλ¬Έμ λλ€. | |
| AIκ° μ΄λ€ μν μ΄λ λ§ν¬, μ±κ²©μΌλ‘ λνμ μν΄μΌ νλμ§λ₯Ό μλ €μ£Όλ μν μ ν©λλ€. | |
| μλ₯Ό λ€μ΄, AIμκ² μμ λ°λ₯Έ λΉμμ²λΌ νλνκ² νκ±°λ, μ°½μμ μΈ μκ°, μ격ν λ¬Έλ² κ²μ¬μμ²λΌ νλνκ² λ§λ€ μ μμ΅λλ€. | |
| κ°μ μ§λ¬Έμ΄λΌλ μμ€ν ν둬ννΈμ λ°λΌ μμ ν λ€λ₯Έ μλ΅μ΄ λμ¬ μ μμ΅λλ€. | |
| """) | |
| # κΈ°λ³Έ μμ€ν ν둬ννΈμ μ¬μ©μ 컀μ€ν ν둬ννΈ μ λ ₯ | |
| custom_prompt = st.text_area("βοΈ μλμ μνλ μμ€ν ν둬ννΈλ₯Ό μ λ ₯νμΈμ:", "You are a helpful assistant.", height=100) | |
| default_prompt = "You are a helpful assistant." | |
| # --- μΈμ μν μ΄κΈ°ν --- | |
| if "default_messages" not in st.session_state: | |
| st.session_state.default_messages = [{"role": "system", "content": default_prompt}] | |
| if "custom_messages" not in st.session_state or st.session_state.custom_prompt != custom_prompt: | |
| st.session_state.custom_messages = [{"role": "system", "content": custom_prompt}] | |
| st.session_state.custom_prompt = custom_prompt | |
| # --- Solar Pro API νΈμΆ ν¨μ --- | |
| def solar_pro_chat(messages, api_key): | |
| """ | |
| Solar Pro APIμ λ©μμ§λ₯Ό 보λ΄κ³ μ€νΈλ¦¬λ° ννλ‘ μλ΅μ λ°μμ€λ ν¨μμ λλ€. | |
| Parameters: | |
| messages (list): μμ€ν /μ¬μ©μ λ©μμ§ λͺ©λ‘ | |
| api_key (str): Upstage API ν€ | |
| Returns: | |
| generator: AIμ μλ΅μ μ€νΈλ¦¬λ° ννλ‘ ν μ€μ© λ°ν | |
| """ | |
| url = "https://api.upstage.ai/v1/chat/completions" | |
| headers = { | |
| "Authorization": f"Bearer {api_key}", | |
| "Accept": "text/event-stream", | |
| "Content-Type": "application/json" | |
| } | |
| payload = { | |
| "model": "solar-pro", | |
| "messages": messages, | |
| "stream": True | |
| } | |
| try: | |
| response = requests.post(url, headers=headers, json=payload, stream=True) | |
| response.raise_for_status() | |
| client = sseclient.SSEClient(response) | |
| for event in client.events(): | |
| if event.data == "[DONE]": | |
| break | |
| try: | |
| content = json.loads(event.data)["choices"][0]["delta"].get("content", "") | |
| yield content | |
| except Exception as e: | |
| st.error("β οΈ μλ΅ μ²λ¦¬ μ€ μ€λ₯κ° λ°μνμ΅λλ€.") | |
| print(f"[SSE νμ± μ€λ₯] {e}") | |
| continue | |
| except requests.exceptions.RequestException as e: | |
| st.error("β API νΈμΆ μ€ν¨: API ν€λ λ€νΈμν¬ μνλ₯Ό νμΈν΄μ£ΌμΈμ.") | |
| print(f"[API μμ² μ€λ₯] {e}") | |
| return | |
| # --- λΉκ΅ μ€ν --- | |
| if st.button("π μλ΅ λΉκ΅νκΈ°") and api_key and user_input: | |
| # μ¬μ©μ μ λ ₯μ λ©μμ§μ μΆκ° | |
| st.session_state.default_messages.append({"role": "user", "content": user_input}) | |
| st.session_state.custom_messages.append({"role": "user", "content": user_input}) | |
| # λ κ°μ 컬λΌμΌλ‘ κ²°κ³Ό λλκΈ° | |
| col1, col2 = st.columns(2) | |
| # κΈ°λ³Έ ν둬ννΈ μλ΅ | |
| with col1: | |
| st.subheader("πΉ κΈ°λ³Έ ν둬ννΈ") | |
| default_response = "" | |
| default_area = st.empty() | |
| with st.spinner("κΈ°λ³Έ μλ΅ μμ± μ€..."): | |
| for chunk in solar_pro_chat(st.session_state.default_messages, api_key): | |
| default_response += chunk | |
| default_area.markdown(f"**π€ λ΄:** {default_response}") | |
| st.session_state.default_messages.append({"role": "assistant", "content": default_response}) | |
| # 컀μ€ν ν둬ννΈ μλ΅ | |
| with col2: | |
| st.subheader("πΈ 컀μ€ν ν둬ννΈ") | |
| custom_response = "" | |
| custom_area = st.empty() | |
| with st.spinner("컀μ€ν μλ΅ μμ± μ€..."): | |
| for chunk in solar_pro_chat(st.session_state.custom_messages, api_key): | |
| custom_response += chunk | |
| custom_area.markdown(f"**π€ λ΄:** {custom_response}") | |
| st.session_state.custom_messages.append({"role": "assistant", "content": custom_response}) | |
| # --- μ±ν λ΄μ CSVλ‘ μ μ₯ --- | |
| def generate_csv(messages, prompt_label): | |
| """ | |
| μ£Όμ΄μ§ λ©μμ§λ₯Ό CSVλ‘ λ³ννλ ν¨μμ λλ€. | |
| Parameters: | |
| messages (list): μμ€ν /μ¬μ©μ/AI λ©μμ§ λ¦¬μ€νΈ | |
| prompt_label (str): μμ€ν ν둬ννΈ ν μ€νΈ | |
| Returns: | |
| str: CSV νμμ λ¬Έμμ΄ | |
| """ | |
| rows = [{"role": "system", "content": prompt_label}] | |
| for msg in messages: | |
| if msg["role"] != "system": | |
| rows.append(msg) | |
| df = pd.DataFrame(rows) | |
| output = StringIO() | |
| df.to_csv(output, index=False) | |
| return output.getvalue() | |
| # νμ¬ μκ°μΌλ‘ νμΌλͺ μμ± | |
| now = datetime.now().strftime("%Y-%m-%d_%H-%M-%S") | |
| # --- λ€μ΄λ‘λ UI --- | |
| st.markdown("### β¬οΈ μ±ν λ΄μ λ€μ΄λ‘λ") | |
| col1, col2 = st.columns(2) | |
| with col1: | |
| st.download_button( | |
| label="κΈ°λ³Έ μλ΅ λ€μ΄λ‘λ", | |
| data=generate_csv(st.session_state.default_messages, default_prompt), | |
| file_name=f"default_chat_{now}.csv", | |
| mime="text/csv", | |
| ) | |
| with col2: | |
| st.download_button( | |
| label="컀μ€ν μλ΅ λ€μ΄λ‘λ", | |
| data=generate_csv(st.session_state.custom_messages, custom_prompt), | |
| file_name=f"custom_chat_{now}.csv", | |
| mime="text/csv", | |
| ) | |