Spaces:
Build error
Build error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import os
|
| 3 |
+
import openai
|
| 4 |
+
|
| 5 |
+
load_dotenv()
|
| 6 |
+
os.environ["OPENAI_API_KEY"] = os.environ['my_secret']
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
def predict(input, history):
|
| 11 |
+
history.append({"role": "user", "content": input})
|
| 12 |
+
|
| 13 |
+
gpt_response = openai.ChatCompletion.create(
|
| 14 |
+
model=model_id,
|
| 15 |
+
messages=history
|
| 16 |
+
)
|
| 17 |
+
|
| 18 |
+
response = gpt_response["choices"][0]["message"]["content"]
|
| 19 |
+
|
| 20 |
+
history.append({"role": "assistant", "content": response})
|
| 21 |
+
|
| 22 |
+
messages = [(history[i]["content"], history[i+1]["content"]) for i in range(1, len(history), 2)]
|
| 23 |
+
|
| 24 |
+
return messages, history
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
with gr.Blocks() as demo:
|
| 28 |
+
chatbot = gr.Chatbot(label="ChatBot")
|
| 29 |
+
|
| 30 |
+
state = gr.State([{
|
| 31 |
+
"role": "system",
|
| 32 |
+
"content": "You are a chatbot for psychological counseling."
|
| 33 |
+
}])
|
| 34 |
+
|
| 35 |
+
with gr.Row():
|
| 36 |
+
txt = gr.Textbox(show_label=False, placeholder="상담을 요청해보세요").style(container=False)
|
| 37 |
+
|
| 38 |
+
txt.submit(predict, [txt, state], [chatbot, state])
|
| 39 |
+
|
| 40 |
+
demo.launch(debug=True, share=True)
|