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() |