Chu Thi Thanh
commited on
Commit
·
cea6169
1
Parent(s):
6fe72da
Upload files
Browse files- .gitignore +1 -0
- app.py +48 -0
- chat.py +15 -0
- requirements.txt +1 -0
.gitignore
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
__pycache__/
|
app.py
ADDED
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from chat import get_response
|
3 |
+
|
4 |
+
def clear_session():
|
5 |
+
return "", []
|
6 |
+
|
7 |
+
def add_query(chat_history, input):
|
8 |
+
if not input:
|
9 |
+
raise gr.Error("Please enter a question.")
|
10 |
+
chat_history.append((input, None))
|
11 |
+
return chat_history
|
12 |
+
|
13 |
+
def response(history, query, model):
|
14 |
+
chat_history = []
|
15 |
+
for i in history:
|
16 |
+
if i[0]:
|
17 |
+
chat_history.append(i[0])
|
18 |
+
if i[1]:
|
19 |
+
chat_history.append(i[1])
|
20 |
+
|
21 |
+
messages = [{"role": "user", "content": chat_history[0]}]
|
22 |
+
for i in range(1, len(chat_history), 2):
|
23 |
+
messages.append({"role": "assistant", "content": chat_history[i]})
|
24 |
+
messages.append({"role": "user", "content": chat_history[i + 1]})
|
25 |
+
|
26 |
+
res_msg = get_response(model, messages)
|
27 |
+
history[-1] = (query, res_msg)
|
28 |
+
return "", history
|
29 |
+
|
30 |
+
demo = gr.Blocks(title= "Chatbot", theme="Soft")
|
31 |
+
with demo:
|
32 |
+
with gr.Column("Chatbot - Family Relationships"):
|
33 |
+
model = gr.Radio(["gpt-3.5-turbo", "gpt-4-turbo", "ft:gpt-3.5-turbo-0125:personal::9hiINnam:ckpt-step-278"],
|
34 |
+
label="model",
|
35 |
+
info="Kindly choose a model before initiating the chat and clear the chat history before switching models. The last one is fine-tuned models.")
|
36 |
+
chatbot = gr.Chatbot(value=[], elem_id='chatbot')
|
37 |
+
|
38 |
+
text_input = gr.Textbox(
|
39 |
+
show_label=False,
|
40 |
+
placeholder="Ask me anything!",
|
41 |
+
container=False)
|
42 |
+
|
43 |
+
clear_btn = gr.Button("🧹 Clear")
|
44 |
+
text_input.submit(add_query, inputs=[chatbot, text_input], outputs=[chatbot], concurrency_limit=1).\
|
45 |
+
success(response, inputs=[chatbot, text_input, model], outputs=[text_input, chatbot])
|
46 |
+
clear_btn.click(clear_session, inputs=[], outputs=[text_input, chatbot])
|
47 |
+
|
48 |
+
demo.launch()
|
chat.py
ADDED
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from openai import OpenAI
|
2 |
+
|
3 |
+
client = OpenAI()
|
4 |
+
|
5 |
+
def get_response(model, messages):
|
6 |
+
pre_message = [
|
7 |
+
{'role': 'system', 'content': "You are therapy assistant specializing in family relationships"},
|
8 |
+
]
|
9 |
+
to_send = pre_message + messages
|
10 |
+
response = client.chat.completions.create(
|
11 |
+
model=model,
|
12 |
+
messages=to_send
|
13 |
+
)
|
14 |
+
|
15 |
+
return response.choices[0].message.content
|
requirements.txt
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
openai==1.25.0
|