add history download to json
Browse files
app.py
CHANGED
@@ -1,6 +1,7 @@
|
|
1 |
from openai import OpenAI
|
2 |
import gradio as gr
|
3 |
import os
|
|
|
4 |
|
5 |
api_key = os.getenv("TYPHOON_API_KEY") # Replace with your key
|
6 |
|
@@ -11,7 +12,7 @@ client = OpenAI(
|
|
11 |
|
12 |
def predict(message, history, system_prompt):
|
13 |
history_openai_format = [{"role": "system", "content": system_prompt}]
|
14 |
-
for human, assistant in history:
|
15 |
if isinstance(human, str) and human.strip():
|
16 |
history_openai_format.append({"role": "user", "content": human})
|
17 |
if isinstance(assistant, str) and assistant.strip():
|
@@ -40,10 +41,21 @@ def chat_bot(user_input, history, system_prompt):
|
|
40 |
history[-1] = (user_input, bot_response)
|
41 |
yield "", history
|
42 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
43 |
CSS ="""
|
44 |
.contain { display: flex; flex-direction: column; }
|
45 |
.gradio-container { height: 100vh !important; }
|
46 |
-
#component-0 { height:
|
47 |
#chatbot { flex-grow: 1; overflow: auto;}
|
48 |
"""
|
49 |
|
@@ -67,17 +79,22 @@ with gr.Blocks(css=CSS) as demo:
|
|
67 |
inputs=msg,
|
68 |
)
|
69 |
|
70 |
-
with gr.Tab("Setting"):
|
71 |
system_prompt = gr.Code(
|
72 |
value="You are HoraCare, an empathetic Thai girl assistant skilled in psychotherapy and Tarot reading. \nYou provide insights and support using Tarot cards, offering clarity and healing. You always answer in Thai.",
|
73 |
show_label=True,
|
74 |
label="System Prompt",
|
75 |
lines=2
|
76 |
)
|
|
|
|
|
|
|
|
|
77 |
|
78 |
clear.click(lambda: [], [], chatbot)
|
79 |
msg.submit(chat_bot, [msg, chatbot, system_prompt], [msg, chatbot])
|
80 |
send.click(chat_bot, [msg, chatbot, system_prompt], [msg, chatbot])
|
|
|
81 |
|
82 |
|
83 |
demo.launch()
|
|
|
1 |
from openai import OpenAI
|
2 |
import gradio as gr
|
3 |
import os
|
4 |
+
import json
|
5 |
|
6 |
api_key = os.getenv("TYPHOON_API_KEY") # Replace with your key
|
7 |
|
|
|
12 |
|
13 |
def predict(message, history, system_prompt):
|
14 |
history_openai_format = [{"role": "system", "content": system_prompt}]
|
15 |
+
for human, assistant in history[-3:]:
|
16 |
if isinstance(human, str) and human.strip():
|
17 |
history_openai_format.append({"role": "user", "content": human})
|
18 |
if isinstance(assistant, str) and assistant.strip():
|
|
|
41 |
history[-1] = (user_input, bot_response)
|
42 |
yield "", history
|
43 |
|
44 |
+
def get_log(history, system_prompt):
|
45 |
+
history_openai_format = [{"role": "system", "content": system_prompt}]
|
46 |
+
for human, assistant in history:
|
47 |
+
if isinstance(human, str) and human.strip():
|
48 |
+
history_openai_format.append({"role": "user", "content": human})
|
49 |
+
if isinstance(assistant, str) and assistant.strip():
|
50 |
+
history_openai_format.append({"role": "assistant", "content": assistant})
|
51 |
+
|
52 |
+
history_openai_format_json = '[\n' + ",\n".join([json.dumps(h, ensure_ascii=False) for h in history_openai_format]) + '\n]'
|
53 |
+
return history_openai_format_json
|
54 |
+
|
55 |
CSS ="""
|
56 |
.contain { display: flex; flex-direction: column; }
|
57 |
.gradio-container { height: 100vh !important; }
|
58 |
+
#component-0 { height: 80%; }
|
59 |
#chatbot { flex-grow: 1; overflow: auto;}
|
60 |
"""
|
61 |
|
|
|
79 |
inputs=msg,
|
80 |
)
|
81 |
|
82 |
+
with gr.Tab("Setting") as setting_tab:
|
83 |
system_prompt = gr.Code(
|
84 |
value="You are HoraCare, an empathetic Thai girl assistant skilled in psychotherapy and Tarot reading. \nYou provide insights and support using Tarot cards, offering clarity and healing. You always answer in Thai.",
|
85 |
show_label=True,
|
86 |
label="System Prompt",
|
87 |
lines=2
|
88 |
)
|
89 |
+
|
90 |
+
msg_log = gr.Code(language='json', label='msg_log')
|
91 |
+
|
92 |
+
# download_log = gr.Code()
|
93 |
|
94 |
clear.click(lambda: [], [], chatbot)
|
95 |
msg.submit(chat_bot, [msg, chatbot, system_prompt], [msg, chatbot])
|
96 |
send.click(chat_bot, [msg, chatbot, system_prompt], [msg, chatbot])
|
97 |
+
setting_tab.select(get_log, [chatbot, system_prompt], [msg_log])
|
98 |
|
99 |
|
100 |
demo.launch()
|