File size: 4,924 Bytes
87791c3 233a83b a9bd4ef ab73867 a9bd4ef 6981f1b 7de0cb4 87791c3 a9bd4ef 233a83b a9bd4ef 233a83b a9bd4ef 233a83b a9bd4ef 233a83b a9bd4ef 233a83b a9bd4ef 233a83b 2e230c5 233a83b a9bd4ef 233a83b a9bd4ef 233a83b a9bd4ef 233a83b a9bd4ef 233a83b a9bd4ef 233a83b a9bd4ef 233a83b a9bd4ef 233a83b a9bd4ef 233a83b a9bd4ef 233a83b a9bd4ef 233a83b a9bd4ef 233a83b a9bd4ef 87791c3 233a83b a9bd4ef 233a83b | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 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 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 | import gradio as gr
import requests
import json
import os # os λΌμ΄λΈλ¬λ¦¬ μΆκ°
# νκ²½ λ³μμμ API ν€λ₯Ό κ°μ Έμ΄
API_KEY = os.getenv("UPSTAGE_API_KEY")
API_URL = "https://api.upstage.ai/v1/"
# API ν€κ° μλ κ²½μ° μλ¬ λ°μ
if API_KEY is None:
raise ValueError("νκ²½ λ³μ 'UPSTAGE_API_KEY'κ° μ€μ λμ§ μμμ΅λλ€. API ν€λ₯Ό μ€μ ν΄μ£ΌμΈμ.")
def chat_with_solar(message, history):
"""Solar Pro2μ λννλ ν¨μ"""
messages = [
{"role": "system", "content": "λΉμ μ λμμ΄ λλ AI μ΄μμ€ν΄νΈμ
λλ€. νκ΅μ΄λ‘ μΉκ·Όνκ³ μ ννκ² λ΅λ³ν΄μ£ΌμΈμ."}
]
for human_msg, ai_msg in history:
messages.append({"role": "user", "content": human_msg})
messages.append({"role": "assistant", "content": ai_msg})
messages.append({"role": "user", "content": message})
data = {
"model": "solar-pro-2",
"messages": messages,
"max_tokens": 1000,
"temperature": 0.7
}
headers = {
"Authorization": f"Bearer {API_KEY}",
"Content-Type": "application/json"
}
try:
response = requests.post(API_URL, headers=headers, json=data)
response.raise_for_status()
result = response.json()
ai_response = result['choices'][0]['message']['content']
return ai_response
except requests.exceptions.RequestException as e:
return f"API μμ² μ€ μ€λ₯κ° λ°μνμ΅λλ€: {str(e)}"
except (KeyError, IndexError) as e:
return f"μλ΅ νμ± μ€ μ€λ₯κ° λ°μνμ΅λλ€: {str(e)}"
except Exception as e:
return f"μμμΉ λͺ»ν μ€λ₯κ° λ°μνμ΅λλ€: {str(e)}"
def create_chatbot():
"""μ±λ΄ μΈν°νμ΄μ€ μμ±"""
with gr.Blocks(
title="Solar Pro2 μ±λ΄",
theme=gr.themes.Soft(),
css="""
.container { max-width: 900px; margin: auto; }
.header { text-align: center; padding: 20px; background: linear-gradient(90deg, #667eea 0%, #764ba2 100%); color: white; border-radius: 10px; margin-bottom: 20px; }
"""
) as demo:
gr.HTML("""
<div class="header">
<h1>π Solar Pro2 μ±λ΄</h1>
<p>Upstageμ Solar Pro2 λͺ¨λΈκ³Ό λνν΄λ³΄μΈμ!</p>
</div>
""")
chatbot = gr.Chatbot(
height=500,
bubble_full_width=False,
avatar_images=("π€", "π€"),
show_copy_button=True
)
with gr.Row():
msg = gr.Textbox(
placeholder="λ©μμ§λ₯Ό μ
λ ₯νμΈμ...",
container=False,
scale=4,
show_label=False
)
submit_btn = gr.Button("μ μ‘", scale=1, variant="primary")
clear_btn = gr.Button("λν μ΄κΈ°ν", scale=1)
gr.Examples(
examples=[
"μλ
νμΈμ! μκΈ°μκ°λ₯Ό ν΄μ£ΌμΈμ.",
"νμ΄μ¬μΌλ‘ κ°λ¨ν κ³μ°κΈ°λ₯Ό λ§λλ λ°©λ²μ μλ €μ£ΌμΈμ.",
"μ€λ λ μ¨κ° μ’λ€μ. μ΄λ€ νλμ μΆμ²νμλμ?",
"AIμ λν΄ κ°λ¨ν μ€λͺ
ν΄μ£ΌμΈμ.",
"λ§μλ νκ΅ μμ λ μνΌλ₯Ό νλ μΆμ²ν΄μ£ΌμΈμ."
],
inputs=msg,
label="μμ μ§λ¬Έλ€"
)
with gr.Accordion("βΉοΈ μ¬μ© μ 보", open=False):
gr.Markdown("""
### Solar Pro2 μ±λ΄μ λν΄
- **λͺ¨λΈ**: Upstage Solar Pro2
- **κΈ°λ₯**: νκ΅μ΄ λν, μ§λ¬Έ λ΅λ³, μ½λ μμ± λ±
- **νΉμ§**: λμ μ±λ₯μ νκ΅μ΄ μΈμ΄λͺ¨λΈ
### μ¬μ© λ°©λ²
1. μμ ν
μ€νΈ λ°μ€μ μ§λ¬Έμ΄λ λνλ₯Ό μ
λ ₯νμΈμ.
2. 'μ μ‘' λ²νΌμ ν΄λ¦νκ±°λ Enterλ₯Ό λλ₯΄μΈμ.
3. AIμ μλ΅μ νμΈνκ³ κ³μ λννμΈμ.
### μ£Όμμ¬ν
- `UPSTAGE_API_KEY` νκ²½ λ³μκ° μ¬λ°λ₯΄κ² μ€μ λμ΄ μλμ§ νμΈν΄μ£ΌμΈμ.
- μΈν°λ· μ°κ²°μ΄ νμν©λλ€.
""")
def respond(message, chat_history):
if not message or not message.strip():
return "", chat_history
bot_message = chat_with_solar(message, chat_history)
chat_history.append((message, bot_message))
return "", chat_history
# μ΄λ²€νΈ νΈλ€λ¬ μ°κ²° (μ€λ³΅ μ κ±°)
msg.submit(respond, [msg, chatbot], [msg, chatbot])
submit_btn.click(respond, [msg, chatbot], [msg, chatbot])
clear_btn.click(lambda: (None, []), outputs=[msg, chatbot])
return demo
if __name__ == "__main__":
demo = create_chatbot()
demo.launch(
share=True,
server_name="0.0.0.0",
server_port=7860,
show_error=True,
debug=True
) |