Update app.py
Browse files
app.py
CHANGED
|
@@ -1,102 +1,76 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import requests
|
| 3 |
import json
|
|
|
|
| 4 |
|
| 5 |
-
|
|
|
|
| 6 |
API_URL = "https://api.upstage.ai/v1/chat/completions"
|
| 7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
def chat_with_solar(message, history):
|
| 9 |
"""Solar Pro2μ λννλ ν¨μ"""
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
# μμ€ν
λ©μμ§ μΆκ°
|
| 15 |
-
messages.append({
|
| 16 |
-
"role": "system",
|
| 17 |
-
"content": "λΉμ μ λμμ΄ λλ AI μ΄μμ€ν΄νΈμ
λλ€. νκ΅μ΄λ‘ μΉκ·Όνκ³ μ ννκ² λ΅λ³ν΄μ£ΌμΈμ."
|
| 18 |
-
})
|
| 19 |
-
|
| 20 |
-
# μ΄μ λν νμ€ν 리 μΆκ°
|
| 21 |
for human_msg, ai_msg in history:
|
| 22 |
messages.append({"role": "user", "content": human_msg})
|
| 23 |
messages.append({"role": "assistant", "content": ai_msg})
|
| 24 |
-
|
| 25 |
-
# νμ¬ μ¬μ©μ λ©μμ§ μΆκ°
|
| 26 |
messages.append({"role": "user", "content": message})
|
| 27 |
-
|
| 28 |
-
# API μμ² λ°μ΄ν°
|
| 29 |
data = {
|
| 30 |
"model": "solar-pro-2",
|
| 31 |
"messages": messages,
|
| 32 |
"max_tokens": 1000,
|
| 33 |
"temperature": 0.7
|
| 34 |
}
|
| 35 |
-
|
| 36 |
-
# API ν€λ
|
| 37 |
headers = {
|
| 38 |
"Authorization": f"Bearer {API_KEY}",
|
| 39 |
"Content-Type": "application/json"
|
| 40 |
}
|
| 41 |
-
|
| 42 |
try:
|
| 43 |
-
# API νΈμΆ
|
| 44 |
response = requests.post(API_URL, headers=headers, json=data)
|
| 45 |
response.raise_for_status()
|
| 46 |
-
|
| 47 |
-
# μλ΅ νμ±
|
| 48 |
result = response.json()
|
| 49 |
ai_response = result['choices'][0]['message']['content']
|
| 50 |
-
|
| 51 |
return ai_response
|
| 52 |
-
|
| 53 |
except requests.exceptions.RequestException as e:
|
| 54 |
return f"API μμ² μ€ μ€λ₯κ° λ°μνμ΅λλ€: {str(e)}"
|
| 55 |
-
except KeyError as e:
|
| 56 |
return f"μλ΅ νμ± μ€ μ€λ₯κ° λ°μνμ΅λλ€: {str(e)}"
|
| 57 |
except Exception as e:
|
| 58 |
return f"μμμΉ λͺ»ν μ€λ₯κ° λ°μνμ΅λλ€: {str(e)}"
|
| 59 |
|
| 60 |
-
# Gradio μΈν°νμ΄μ€ μμ±
|
| 61 |
def create_chatbot():
|
| 62 |
"""μ±λ΄ μΈν°νμ΄μ€ μμ±"""
|
| 63 |
-
|
| 64 |
with gr.Blocks(
|
| 65 |
title="Solar Pro2 μ±λ΄",
|
| 66 |
theme=gr.themes.Soft(),
|
| 67 |
css="""
|
| 68 |
-
.container {
|
| 69 |
-
|
| 70 |
-
margin: auto;
|
| 71 |
-
}
|
| 72 |
-
.header {
|
| 73 |
-
text-align: center;
|
| 74 |
-
padding: 20px;
|
| 75 |
-
background: linear-gradient(90deg, #667eea 0%, #764ba2 100%);
|
| 76 |
-
color: white;
|
| 77 |
-
border-radius: 10px;
|
| 78 |
-
margin-bottom: 20px;
|
| 79 |
-
}
|
| 80 |
"""
|
| 81 |
) as demo:
|
| 82 |
-
|
| 83 |
-
# ν€λ
|
| 84 |
gr.HTML("""
|
| 85 |
<div class="header">
|
| 86 |
<h1>π Solar Pro2 μ±λ΄</h1>
|
| 87 |
<p>Upstageμ Solar Pro2 λͺ¨λΈκ³Ό λνν΄λ³΄μΈμ!</p>
|
| 88 |
</div>
|
| 89 |
""")
|
| 90 |
-
|
| 91 |
-
# μ±ν
μΈν°νμ΄μ€
|
| 92 |
chatbot = gr.Chatbot(
|
| 93 |
height=500,
|
| 94 |
bubble_full_width=False,
|
| 95 |
avatar_images=("π€", "π€"),
|
| 96 |
show_copy_button=True
|
| 97 |
)
|
| 98 |
-
|
| 99 |
-
# μ
λ ₯ μμ
|
| 100 |
with gr.Row():
|
| 101 |
msg = gr.Textbox(
|
| 102 |
placeholder="λ©μμ§λ₯Ό μ
λ ₯νμΈμ...",
|
|
@@ -106,8 +80,7 @@ def create_chatbot():
|
|
| 106 |
)
|
| 107 |
submit_btn = gr.Button("μ μ‘", scale=1, variant="primary")
|
| 108 |
clear_btn = gr.Button("λν μ΄κΈ°ν", scale=1)
|
| 109 |
-
|
| 110 |
-
# μμ μ§λ¬Έλ€
|
| 111 |
gr.Examples(
|
| 112 |
examples=[
|
| 113 |
"μλ
νμΈμ! μκΈ°μκ°λ₯Ό ν΄μ£ΌμΈμ.",
|
|
@@ -119,61 +92,43 @@ def create_chatbot():
|
|
| 119 |
inputs=msg,
|
| 120 |
label="μμ μ§λ¬Έλ€"
|
| 121 |
)
|
| 122 |
-
|
| 123 |
-
# μ 보 μΉμ
|
| 124 |
with gr.Accordion("βΉοΈ μ¬μ© μ 보", open=False):
|
| 125 |
gr.Markdown("""
|
| 126 |
### Solar Pro2 μ±λ΄μ λν΄
|
| 127 |
-
|
| 128 |
- **λͺ¨λΈ**: Upstage Solar Pro2
|
| 129 |
- **κΈ°λ₯**: νκ΅μ΄ λν, μ§λ¬Έ λ΅λ³, μ½λ μμ± λ±
|
| 130 |
- **νΉμ§**: λμ μ±λ₯μ νκ΅μ΄ μΈμ΄λͺ¨λΈ
|
| 131 |
-
|
| 132 |
### μ¬μ© λ°©λ²
|
| 133 |
-
1. μμ ν
μ€νΈ λ°μ€μ μ§λ¬Έμ΄λ λνλ₯Ό μ
λ ₯νμΈμ
|
| 134 |
-
2. 'μ μ‘' λ²νΌμ ν΄λ¦νκ±°λ Enter
|
| 135 |
-
3. AIμ μλ΅μ νμΈνκ³ κ³μ λννμΈμ
|
| 136 |
-
|
| 137 |
### μ£Όμμ¬ν
|
| 138 |
-
-
|
| 139 |
-
- μΈν°λ· μ°κ²°μ΄ νμν©λλ€
|
| 140 |
""")
|
| 141 |
-
|
| 142 |
-
# μ΄λ²€νΈ νΈλ€λ¬
|
| 143 |
def respond(message, chat_history):
|
| 144 |
-
if not message.strip():
|
| 145 |
return "", chat_history
|
| 146 |
-
|
| 147 |
-
# AI μλ΅ μμ±
|
| 148 |
bot_message = chat_with_solar(message, chat_history)
|
| 149 |
-
|
| 150 |
-
# μ±ν
νμ€ν 리μ μΆκ°
|
| 151 |
chat_history.append((message, bot_message))
|
| 152 |
-
|
| 153 |
return "", chat_history
|
| 154 |
-
|
| 155 |
-
#
|
| 156 |
msg.submit(respond, [msg, chatbot], [msg, chatbot])
|
| 157 |
submit_btn.click(respond, [msg, chatbot], [msg, chatbot])
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
# μν° ν€λ‘ μ μ‘ κ°λ₯νλλ‘ μ€μ
|
| 163 |
-
msg.submit(respond, [msg, chatbot], [msg, chatbot])
|
| 164 |
-
|
| 165 |
-
return demo
|
| 166 |
|
| 167 |
-
# λ©μΈ μ€ν
|
| 168 |
if __name__ == "__main__":
|
| 169 |
-
# μ±λ΄ μμ± λ° μ€ν
|
| 170 |
demo = create_chatbot()
|
| 171 |
-
|
| 172 |
-
# λ‘컬μμ μ€ν
|
| 173 |
demo.launch(
|
| 174 |
-
share=True,
|
| 175 |
-
server_name="0.0.0.0",
|
| 176 |
-
server_port=7860,
|
| 177 |
-
show_error=True,
|
| 178 |
-
debug=True
|
| 179 |
)
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import requests
|
| 3 |
import json
|
| 4 |
+
import os # os λΌμ΄λΈλ¬λ¦¬ μΆκ°
|
| 5 |
|
| 6 |
+
# νκ²½ λ³μμμ API ν€λ₯Ό κ°μ Έμ΄
|
| 7 |
+
API_KEY = os.getenv("UPSTAGE_API_KEY")
|
| 8 |
API_URL = "https://api.upstage.ai/v1/chat/completions"
|
| 9 |
|
| 10 |
+
# API ν€κ° μλ κ²½μ° μλ¬ λ°μ
|
| 11 |
+
if API_KEY is None:
|
| 12 |
+
raise ValueError("νκ²½ λ³μ 'UPSTAGE_API_KEY'κ° μ€μ λμ§ μμμ΅λλ€. API ν€λ₯Ό μ€μ ν΄μ£ΌμΈμ.")
|
| 13 |
+
|
| 14 |
def chat_with_solar(message, history):
|
| 15 |
"""Solar Pro2μ λννλ ν¨μ"""
|
| 16 |
+
messages = [
|
| 17 |
+
{"role": "system", "content": "λΉμ μ λμμ΄ λλ AI μ΄μμ€ν΄νΈμ
λλ€. νκ΅μ΄λ‘ μΉκ·Όνκ³ μ ννκ² λ΅λ³ν΄μ£ΌμΈμ."}
|
| 18 |
+
]
|
| 19 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
for human_msg, ai_msg in history:
|
| 21 |
messages.append({"role": "user", "content": human_msg})
|
| 22 |
messages.append({"role": "assistant", "content": ai_msg})
|
| 23 |
+
|
|
|
|
| 24 |
messages.append({"role": "user", "content": message})
|
| 25 |
+
|
|
|
|
| 26 |
data = {
|
| 27 |
"model": "solar-pro-2",
|
| 28 |
"messages": messages,
|
| 29 |
"max_tokens": 1000,
|
| 30 |
"temperature": 0.7
|
| 31 |
}
|
|
|
|
|
|
|
| 32 |
headers = {
|
| 33 |
"Authorization": f"Bearer {API_KEY}",
|
| 34 |
"Content-Type": "application/json"
|
| 35 |
}
|
| 36 |
+
|
| 37 |
try:
|
|
|
|
| 38 |
response = requests.post(API_URL, headers=headers, json=data)
|
| 39 |
response.raise_for_status()
|
|
|
|
|
|
|
| 40 |
result = response.json()
|
| 41 |
ai_response = result['choices'][0]['message']['content']
|
|
|
|
| 42 |
return ai_response
|
|
|
|
| 43 |
except requests.exceptions.RequestException as e:
|
| 44 |
return f"API μμ² μ€ μ€λ₯κ° λ°μνμ΅λλ€: {str(e)}"
|
| 45 |
+
except (KeyError, IndexError) as e:
|
| 46 |
return f"μλ΅ νμ± μ€ μ€λ₯κ° λ°μνμ΅λλ€: {str(e)}"
|
| 47 |
except Exception as e:
|
| 48 |
return f"μμμΉ λͺ»ν μ€λ₯κ° λ°μνμ΅λλ€: {str(e)}"
|
| 49 |
|
|
|
|
| 50 |
def create_chatbot():
|
| 51 |
"""μ±λ΄ μΈν°νμ΄μ€ μμ±"""
|
|
|
|
| 52 |
with gr.Blocks(
|
| 53 |
title="Solar Pro2 μ±λ΄",
|
| 54 |
theme=gr.themes.Soft(),
|
| 55 |
css="""
|
| 56 |
+
.container { max-width: 900px; margin: auto; }
|
| 57 |
+
.header { text-align: center; padding: 20px; background: linear-gradient(90deg, #667eea 0%, #764ba2 100%); color: white; border-radius: 10px; margin-bottom: 20px; }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
"""
|
| 59 |
) as demo:
|
|
|
|
|
|
|
| 60 |
gr.HTML("""
|
| 61 |
<div class="header">
|
| 62 |
<h1>π Solar Pro2 μ±λ΄</h1>
|
| 63 |
<p>Upstageμ Solar Pro2 λͺ¨λΈκ³Ό λνν΄λ³΄μΈμ!</p>
|
| 64 |
</div>
|
| 65 |
""")
|
| 66 |
+
|
|
|
|
| 67 |
chatbot = gr.Chatbot(
|
| 68 |
height=500,
|
| 69 |
bubble_full_width=False,
|
| 70 |
avatar_images=("π€", "π€"),
|
| 71 |
show_copy_button=True
|
| 72 |
)
|
| 73 |
+
|
|
|
|
| 74 |
with gr.Row():
|
| 75 |
msg = gr.Textbox(
|
| 76 |
placeholder="λ©μμ§λ₯Ό μ
λ ₯νμΈμ...",
|
|
|
|
| 80 |
)
|
| 81 |
submit_btn = gr.Button("μ μ‘", scale=1, variant="primary")
|
| 82 |
clear_btn = gr.Button("λν μ΄κΈ°ν", scale=1)
|
| 83 |
+
|
|
|
|
| 84 |
gr.Examples(
|
| 85 |
examples=[
|
| 86 |
"μλ
νμΈμ! μκΈ°μκ°λ₯Ό ν΄μ£ΌμΈμ.",
|
|
|
|
| 92 |
inputs=msg,
|
| 93 |
label="μμ μ§λ¬Έλ€"
|
| 94 |
)
|
| 95 |
+
|
|
|
|
| 96 |
with gr.Accordion("βΉοΈ μ¬μ© μ 보", open=False):
|
| 97 |
gr.Markdown("""
|
| 98 |
### Solar Pro2 μ±λ΄μ λν΄
|
|
|
|
| 99 |
- **λͺ¨λΈ**: Upstage Solar Pro2
|
| 100 |
- **κΈ°λ₯**: νκ΅μ΄ λν, μ§λ¬Έ λ΅λ³, μ½λ μμ± λ±
|
| 101 |
- **νΉμ§**: λμ μ±λ₯μ νκ΅μ΄ μΈμ΄λͺ¨λΈ
|
|
|
|
| 102 |
### μ¬μ© λ°©λ²
|
| 103 |
+
1. μμ ν
μ€νΈ λ°μ€μ μ§λ¬Έμ΄λ λνλ₯Ό μ
λ ₯νμΈμ.
|
| 104 |
+
2. 'μ μ‘' λ²νΌμ ν΄λ¦νκ±°λ EnterοΏ½οΏ½ λλ₯΄μΈμ.
|
| 105 |
+
3. AIμ μλ΅μ νμΈνκ³ κ³μ λννμΈμ.
|
|
|
|
| 106 |
### μ£Όμμ¬ν
|
| 107 |
+
- `UPSTAGE_API_KEY` νκ²½ λ³μκ° μ¬λ°λ₯΄κ² μ€μ λμ΄ μλμ§ νμΈν΄μ£ΌμΈμ.
|
| 108 |
+
- μΈν°λ· μ°κ²°μ΄ νμν©λλ€.
|
| 109 |
""")
|
| 110 |
+
|
|
|
|
| 111 |
def respond(message, chat_history):
|
| 112 |
+
if not message or not message.strip():
|
| 113 |
return "", chat_history
|
| 114 |
+
|
|
|
|
| 115 |
bot_message = chat_with_solar(message, chat_history)
|
|
|
|
|
|
|
| 116 |
chat_history.append((message, bot_message))
|
|
|
|
| 117 |
return "", chat_history
|
| 118 |
+
|
| 119 |
+
# μ΄λ²€νΈ νΈλ€λ¬ μ°κ²° (μ€λ³΅ μ κ±°)
|
| 120 |
msg.submit(respond, [msg, chatbot], [msg, chatbot])
|
| 121 |
submit_btn.click(respond, [msg, chatbot], [msg, chatbot])
|
| 122 |
+
clear_btn.click(lambda: (None, []), outputs=[msg, chatbot])
|
| 123 |
+
|
| 124 |
+
return demo
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 125 |
|
|
|
|
| 126 |
if __name__ == "__main__":
|
|
|
|
| 127 |
demo = create_chatbot()
|
|
|
|
|
|
|
| 128 |
demo.launch(
|
| 129 |
+
share=True,
|
| 130 |
+
server_name="0.0.0.0",
|
| 131 |
+
server_port=7860,
|
| 132 |
+
show_error=True,
|
| 133 |
+
debug=True
|
| 134 |
)
|