Spaces:
Runtime error
Runtime error
changed to openai
Browse files
app.py
CHANGED
|
@@ -1,53 +1,47 @@
|
|
| 1 |
import gradio as gr
|
| 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 |
-
threading.Thread(target=load_model_bg).start()
|
| 40 |
-
|
| 41 |
-
# Gradio ์ฑ
|
| 42 |
with gr.Blocks() as demo:
|
| 43 |
chatbot = gr.Chatbot()
|
| 44 |
msg = gr.Textbox(label="๋ฌธ์ฅ์ ์
๋ ฅํ์ธ์", placeholder="์: ๋ ์ ๋ง ์ ๊ทธ๋ ๊ฒ ๋งํด?")
|
| 45 |
-
status = gr.Markdown(get_status)
|
| 46 |
|
| 47 |
def respond_and_clear(user_input, history):
|
| 48 |
-
updated_history =
|
| 49 |
-
return "", updated_history
|
| 50 |
|
| 51 |
-
msg.submit(respond_and_clear, [msg, chatbot], [msg, chatbot
|
| 52 |
|
| 53 |
-
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import openai
|
| 3 |
+
|
| 4 |
+
# โ
OpenAI API ํค ์
๋ ฅ (์์ ํ ์ ์ฅ ๋ฐฉ์ ๊ถ์ฅ)
|
| 5 |
+
openai.api_key = "YOUR_OPENAI_API_KEY"
|
| 6 |
+
|
| 7 |
+
# GPT์๊ฒ ๋ณด๋ผ ํ๋กฌํํธ๋ฅผ ๊ตฌ์ฑํ๊ณ ์๋ต ๋ฐ๊ธฐ
|
| 8 |
+
def chat_with_gpt(message, history):
|
| 9 |
+
# ์์คํ
ํ๋กฌํํธ: ์ญํ ์ค์
|
| 10 |
+
system_prompt = "๋๋ ๋ฌธ์ฅ์ ๊ณต์ํ๊ณ ์์ ์๊ฒ ๋ฐ๊ฟ์ฃผ๋ ํ๊ตญ์ด ์ ๋ฌธ๊ฐ์ผ."
|
| 11 |
+
|
| 12 |
+
# ์ฌ์ฉ์ ํ๋กฌํํธ ๊ตฌ์ฑ
|
| 13 |
+
user_prompt = f"""์๋ ๋ฌธ์ฅ์ ๋ฌด๋กํ๊ฑฐ๋ ๊ณต๊ฒฉ์ ์ธ ํํ์ ์ฐพ์๋ด๊ณ , ๋ ์์ ์๋ ํํ์ผ๋ก ๋ฐ๊ฟ์ค. ๊ฐ๋จํ ์ด์ ๋ ํจ๊ป ์๋ ค์ค.
|
| 14 |
+
|
| 15 |
+
๋ฌธ์ฅ: "{message}"
|
| 16 |
+
|
| 17 |
+
์๋ต ํ์:
|
| 18 |
+
1. ์ง์ ์ฌํญ: (๋ฌด๋กํ ํํ์ด ์๋ค๋ฉด ์ด๋ค ๋ถ๋ถ์ธ์ง ์ค๋ช
)
|
| 19 |
+
2. ์ ์ ๋ฌธ์ฅ: (๋ ์์ ์๊ฒ ๋ฐ๊พผ ๋ฌธ์ฅ ์ ์)
|
| 20 |
+
"""
|
| 21 |
+
|
| 22 |
+
try:
|
| 23 |
+
response = openai.ChatCompletion.create(
|
| 24 |
+
model="gpt-4", # ๋๋ "gpt-3.5-turbo"
|
| 25 |
+
messages=[
|
| 26 |
+
{"role": "system", "content": system_prompt},
|
| 27 |
+
{"role": "user", "content": user_prompt}
|
| 28 |
+
],
|
| 29 |
+
temperature=0.7,
|
| 30 |
+
)
|
| 31 |
+
reply = response["choices"][0]["message"]["content"].strip()
|
| 32 |
+
return history + [[message, reply]]
|
| 33 |
+
except Exception as e:
|
| 34 |
+
return history + [[message, f"โ ๏ธ ์ค๋ฅ ๋ฐ์: {str(e)}"]]
|
| 35 |
+
|
| 36 |
+
# Gradio ์ฑ ๊ตฌ์ฑ
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
with gr.Blocks() as demo:
|
| 38 |
chatbot = gr.Chatbot()
|
| 39 |
msg = gr.Textbox(label="๋ฌธ์ฅ์ ์
๋ ฅํ์ธ์", placeholder="์: ๋ ์ ๋ง ์ ๊ทธ๋ ๊ฒ ๋งํด?")
|
|
|
|
| 40 |
|
| 41 |
def respond_and_clear(user_input, history):
|
| 42 |
+
updated_history = chat_with_gpt(user_input, history)
|
| 43 |
+
return "", updated_history
|
| 44 |
|
| 45 |
+
msg.submit(respond_and_clear, [msg, chatbot], [msg, chatbot])
|
| 46 |
|
| 47 |
+
demo.launch()
|