Spaces:
Build error
Build error
rkoushikroy2
commited on
Commit
β’
f6d82ae
1
Parent(s):
6a5ecdf
Upload 2 files
Browse files- app.py +10 -31
- helper_functions.py +52 -4
app.py
CHANGED
@@ -4,40 +4,11 @@ from helper_functions import *
|
|
4 |
|
5 |
with gr.Blocks() as app:
|
6 |
gr.Markdown('# FundedNext Customer Service Chatbot')
|
|
|
7 |
session_data = gr.State([
|
8 |
-
[{"role": "system", "content": pre_text}],[]
|
9 |
])
|
10 |
-
def user(user_message, history):
|
11 |
-
return "", history + [[user_message, None]]
|
12 |
|
13 |
-
def bot(history, session_data_fn):
|
14 |
-
messages_archived = session_data_fn[0]
|
15 |
-
messages_current = session_data_fn[1]
|
16 |
-
bot_message, messages_archived, messages_current = get_reply(history[-1][0], messages_archived, messages_current)
|
17 |
-
history[-1][1] = bot_message
|
18 |
-
session_data_fn[0] = messages_archived
|
19 |
-
session_data_fn[1] = messages_current
|
20 |
-
return history, session_data_fn
|
21 |
-
|
22 |
-
def reset_memory(session_data_fn):
|
23 |
-
messages_archived = session_data_fn[0]
|
24 |
-
if(len(messages_archived)>=21):
|
25 |
-
messages_archived = messages_archived[0:1] + messages_archived[3:]
|
26 |
-
session_data_fn[0] = messages_archived
|
27 |
-
return session_data_fn
|
28 |
-
|
29 |
-
def clear_data(session_data_fn):
|
30 |
-
messages_archived = [
|
31 |
-
{"role": "system", "content": pre_text}
|
32 |
-
]
|
33 |
-
messages_current = []
|
34 |
-
session_data_fn[0] = messages_archived
|
35 |
-
session_data_fn[1] = messages_current
|
36 |
-
return None, session_data_fn
|
37 |
-
|
38 |
-
def get_context_gr(session_data_fn):
|
39 |
-
messages_current = session_data_fn[1]
|
40 |
-
return str(messages_current)
|
41 |
|
42 |
with gr.Tab("Chat"):
|
43 |
with gr.Row():
|
@@ -52,6 +23,11 @@ with gr.Blocks() as app:
|
|
52 |
with gr.Tab("Prompt"):
|
53 |
context = gr.Textbox()
|
54 |
submit_p = gr.Button("Check Prompt")
|
|
|
|
|
|
|
|
|
|
|
55 |
# Tab Chat
|
56 |
msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
|
57 |
bot, [chatbot, session_data], [chatbot, session_data]
|
@@ -71,4 +47,7 @@ with gr.Blocks() as app:
|
|
71 |
)
|
72 |
# Tab Prompt
|
73 |
submit_p.click(get_context_gr, session_data, context, queue=False)
|
|
|
|
|
|
|
74 |
app.launch(auth=(os.getenv("id"), os.getenv("password")), show_api=False)
|
|
|
4 |
|
5 |
with gr.Blocks() as app:
|
6 |
gr.Markdown('# FundedNext Customer Service Chatbot')
|
7 |
+
# Message Archived, Message Current, User Message, Bot Message
|
8 |
session_data = gr.State([
|
9 |
+
[{"role": "system", "content": pre_text}],[],[],[]
|
10 |
])
|
|
|
|
|
11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
|
13 |
with gr.Tab("Chat"):
|
14 |
with gr.Row():
|
|
|
23 |
with gr.Tab("Prompt"):
|
24 |
context = gr.Textbox()
|
25 |
submit_p = gr.Button("Check Prompt")
|
26 |
+
|
27 |
+
with gr.Tab("Download"):
|
28 |
+
file = gr.File(interactive = False)
|
29 |
+
file_download_btn = gr.Button("Downlaod Chat History")
|
30 |
+
|
31 |
# Tab Chat
|
32 |
msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
|
33 |
bot, [chatbot, session_data], [chatbot, session_data]
|
|
|
47 |
)
|
48 |
# Tab Prompt
|
49 |
submit_p.click(get_context_gr, session_data, context, queue=False)
|
50 |
+
# Tab Download
|
51 |
+
file_download_btn.click(fn = download_file, inputs = session_data, outputs = file, queue = False)
|
52 |
+
# app.launch(debug=True)
|
53 |
app.launch(auth=(os.getenv("id"), os.getenv("password")), show_api=False)
|
helper_functions.py
CHANGED
@@ -3,6 +3,7 @@ import os
|
|
3 |
import openai
|
4 |
import pandas as pd
|
5 |
import numpy as np
|
|
|
6 |
|
7 |
# Set up OpenAI API key
|
8 |
openai.api_key = os.getenv("OPENAI_API_KEY")
|
@@ -37,11 +38,12 @@ def search(df, query, max_n, max_token):
|
|
37 |
|
38 |
def get_context(query):
|
39 |
results = search(df, query, max_n = 10, max_token = 500)
|
40 |
-
return f"""I will ask you
|
41 |
-
|
|
|
42 |
{results}
|
43 |
-
|
44 |
-
My question is:
|
45 |
"""
|
46 |
|
47 |
def get_reply(message, messages_archived, messages_current):
|
@@ -65,3 +67,49 @@ def get_reply(message, messages_archived, messages_current):
|
|
65 |
|
66 |
return reply, messages_archived, messages_current
|
67 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
import openai
|
4 |
import pandas as pd
|
5 |
import numpy as np
|
6 |
+
import time
|
7 |
|
8 |
# Set up OpenAI API key
|
9 |
openai.api_key = os.getenv("OPENAI_API_KEY")
|
|
|
38 |
|
39 |
def get_context(query):
|
40 |
results = search(df, query, max_n = 10, max_token = 500)
|
41 |
+
return f"""I will ask you a question. You will answer with the help of knowlwedge base.
|
42 |
+
Sometimes the knowledge base will be empty. It means that you will have to answer the question yourself.
|
43 |
+
--Start of Knowledge Base--
|
44 |
{results}
|
45 |
+
--End of Knowledge Base--
|
46 |
+
My question is: "{query}"
|
47 |
"""
|
48 |
|
49 |
def get_reply(message, messages_archived, messages_current):
|
|
|
67 |
|
68 |
return reply, messages_archived, messages_current
|
69 |
|
70 |
+
|
71 |
+
def user(user_message, history):
|
72 |
+
return "", history + [[user_message, None]]
|
73 |
+
|
74 |
+
def bot(history, session_data_fn):
|
75 |
+
messages_archived = session_data_fn[0]
|
76 |
+
messages_current = session_data_fn[1]
|
77 |
+
bot_message, messages_archived, messages_current = get_reply(history[-1][0], messages_archived, messages_current)
|
78 |
+
history[-1][1] = bot_message
|
79 |
+
session_data_fn[0] = messages_archived
|
80 |
+
session_data_fn[1] = messages_current
|
81 |
+
session_data_fn[2].append(history[-1][0])
|
82 |
+
session_data_fn[3].append(history[-1][1])
|
83 |
+
return history, session_data_fn
|
84 |
+
|
85 |
+
def reset_memory(session_data_fn):
|
86 |
+
messages_archived = session_data_fn[0]
|
87 |
+
# print("Message Archived Len=", len(messages_archived))
|
88 |
+
if(len(messages_archived)>=21):
|
89 |
+
messages_archived = messages_archived[0:1] + messages_archived[3:]
|
90 |
+
session_data_fn[0] = messages_archived
|
91 |
+
return session_data_fn
|
92 |
+
|
93 |
+
def clear_data(session_data_fn):
|
94 |
+
messages_archived = [
|
95 |
+
{"role": "system", "content": pre_text}
|
96 |
+
]
|
97 |
+
messages_current = []
|
98 |
+
session_data_fn[0] = messages_archived
|
99 |
+
session_data_fn[1] = messages_current
|
100 |
+
session_data_fn[2] = []
|
101 |
+
session_data_fn[3] = []
|
102 |
+
return None, session_data_fn
|
103 |
+
|
104 |
+
def get_context_gr(session_data_fn):
|
105 |
+
messages_current = session_data_fn[1]
|
106 |
+
return str(messages_current)
|
107 |
+
|
108 |
+
def download_file(session_data_fn):
|
109 |
+
df = pd.DataFrame(list(zip(session_data_fn[2], session_data_fn[3])),
|
110 |
+
columns =['user_message', 'bot_reply'])
|
111 |
+
current_date_time = time.strftime("Date %Y_%m_%d Time %H_%M_%S", time.localtime())
|
112 |
+
file_name = "files/Chat History " + current_date_time + ".csv"
|
113 |
+
df.to_csv(file_name, index=False)
|
114 |
+
return file_name
|
115 |
+
|