Spaces:
Runtime error
Runtime error
import gradio as gr | |
import openai | |
# β OpenAI API ν€ μ λ ₯ (μμ ν μ μ₯ λ°©μ κΆμ₯) | |
openai.api_key = "YOUR_OPENAI_API_KEY" | |
# GPTμκ² λ³΄λΌ ν둬ννΈλ₯Ό ꡬμ±νκ³ μλ΅ λ°κΈ° | |
def chat_with_gpt(message, history): | |
# μμ€ν ν둬ννΈ: μν μ€μ | |
system_prompt = "λλ λ¬Έμ₯μ 곡μνκ³ μμ μκ² λ°κΏμ£Όλ νκ΅μ΄ μ λ¬Έκ°μΌ." | |
# μ¬μ©μ ν둬ννΈ κ΅¬μ± | |
user_prompt = f"""μλ λ¬Έμ₯μ 무λ‘νκ±°λ 곡격μ μΈ ννμ μ°Ύμλ΄κ³ , λ μμ μλ ννμΌλ‘ λ°κΏμ€. κ°λ¨ν μ΄μ λ ν¨κ» μλ €μ€. | |
λ¬Έμ₯: "{message}" | |
μλ΅ νμ: | |
1. μ§μ μ¬ν: (무λ‘ν ννμ΄ μλ€λ©΄ μ΄λ€ λΆλΆμΈμ§ μ€λͺ ) | |
2. μ μ λ¬Έμ₯: (λ μμ μκ² λ°κΎΌ λ¬Έμ₯ μ μ) | |
""" | |
try: | |
response = openai.ChatCompletion.create( | |
model="gpt-4", # λλ "gpt-3.5-turbo" | |
messages=[ | |
{"role": "system", "content": system_prompt}, | |
{"role": "user", "content": user_prompt} | |
], | |
temperature=0.7, | |
) | |
reply = response["choices"][0]["message"]["content"].strip() | |
return history + [[message, reply]] | |
except Exception as e: | |
return history + [[message, f"β οΈ μ€λ₯ λ°μ: {str(e)}"]] | |
# Gradio μ± κ΅¬μ± | |
with gr.Blocks() as demo: | |
chatbot = gr.Chatbot() | |
msg = gr.Textbox(label="λ¬Έμ₯μ μ λ ₯νμΈμ", placeholder="μ: λ μ λ§ μ κ·Έλ κ² λ§ν΄?") | |
def respond_and_clear(user_input, history): | |
updated_history = chat_with_gpt(user_input, history) | |
return "", updated_history | |
msg.submit(respond_and_clear, [msg, chatbot], [msg, chatbot]) | |
demo.launch() |