Spaces:
Runtime error
Runtime error
File size: 1,697 Bytes
531d477 1fcd7be 171f69d fb5e935 171f69d fb5e935 1fcd7be 171f69d 1fcd7be 531d477 1fcd7be |
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 |
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() |