projectBongBong / app.py
devvtaco's picture
changed to openai
1fcd7be verified
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()