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