Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -10,7 +10,8 @@ import fastapi
|
|
10 |
from typing import List, Dict
|
11 |
import httpx
|
12 |
import pandas as pd
|
13 |
-
|
|
|
14 |
UseMemory=True
|
15 |
|
16 |
HF_TOKEN=os.environ.get("HF_TOKEN")
|
@@ -36,12 +37,28 @@ def SaveResult(text, outputfileName):
|
|
36 |
def store_message(name: str, message: str, outputfileName: str):
|
37 |
basedir = os.path.dirname(__file__)
|
38 |
savePath = outputfileName
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
df = pd.read_csv(savePath)
|
46 |
df = df.sort_values(df.columns[0],ascending=False)
|
47 |
return df
|
@@ -69,7 +86,10 @@ description = """Chatbot With persistent memory dataset allowing multiagent syst
|
|
69 |
|
70 |
def get_base(filename):
|
71 |
basedir = os.path.dirname(__file__)
|
72 |
-
|
|
|
|
|
|
|
73 |
return loadPath
|
74 |
|
75 |
def chat(message, history):
|
@@ -91,7 +111,7 @@ def chat(message, history):
|
|
91 |
df=pd.DataFrame()
|
92 |
|
93 |
if UseMemory:
|
94 |
-
outputfileName = '
|
95 |
df = store_message(message, response, outputfileName) # Save to dataset
|
96 |
basedir = get_base(outputfileName)
|
97 |
|
@@ -99,11 +119,11 @@ def chat(message, history):
|
|
99 |
|
100 |
|
101 |
with gr.Blocks() as demo:
|
102 |
-
gr.Markdown("<h1><center>🍰Gradio chatbot backed by
|
103 |
|
104 |
with gr.Row():
|
105 |
t1 = gr.Textbox(lines=1, default="", label="Chat Text:")
|
106 |
-
b1 = gr.Button("
|
107 |
|
108 |
with gr.Row(): # inputs and buttons
|
109 |
s1 = gr.State([])
|
|
|
10 |
from typing import List, Dict
|
11 |
import httpx
|
12 |
import pandas as pd
|
13 |
+
|
14 |
+
|
15 |
UseMemory=True
|
16 |
|
17 |
HF_TOKEN=os.environ.get("HF_TOKEN")
|
|
|
37 |
def store_message(name: str, message: str, outputfileName: str):
|
38 |
basedir = os.path.dirname(__file__)
|
39 |
savePath = outputfileName
|
40 |
+
|
41 |
+
# if file doesnt exist, create it with labels
|
42 |
+
from os.path import exists
|
43 |
+
file_exists = exists(savePath)
|
44 |
+
|
45 |
+
if (file_exists==False):
|
46 |
+
with open(savePath, "w") as f: #write
|
47 |
+
f.write(str("time, message, text\n")) # one time only to get column headers for CSV file
|
48 |
+
if name and message:
|
49 |
+
writer = csv.DictWriter(f, fieldnames=["time", "message", "name"])
|
50 |
+
writer.writerow(
|
51 |
+
{"time": str(datetime.now()), "message": message.strip(), "name": name.strip() }
|
52 |
+
)
|
53 |
+
df = pd.read_csv(savePath)
|
54 |
+
df = df.sort_values(df.columns[0],ascending=False)
|
55 |
+
else:
|
56 |
+
if name and message:
|
57 |
+
with open(savePath, "a") as csvfile:
|
58 |
+
writer = csv.DictWriter(csvfile, fieldnames=[ "time", "message", "name", ])
|
59 |
+
writer.writerow(
|
60 |
+
{"time": str(datetime.now()), "message": message.strip(), "name": name.strip() }
|
61 |
+
)
|
62 |
df = pd.read_csv(savePath)
|
63 |
df = df.sort_values(df.columns[0],ascending=False)
|
64 |
return df
|
|
|
86 |
|
87 |
def get_base(filename):
|
88 |
basedir = os.path.dirname(__file__)
|
89 |
+
print(basedir)
|
90 |
+
#loadPath = basedir + "\\" + filename # works on windows
|
91 |
+
loadPath = basedir + filename
|
92 |
+
print(loadPath)
|
93 |
return loadPath
|
94 |
|
95 |
def chat(message, history):
|
|
|
111 |
df=pd.DataFrame()
|
112 |
|
113 |
if UseMemory:
|
114 |
+
outputfileName = 'ChatbotMemory01-24-2023-05-07-PM.csv'
|
115 |
df = store_message(message, response, outputfileName) # Save to dataset
|
116 |
basedir = get_base(outputfileName)
|
117 |
|
|
|
119 |
|
120 |
|
121 |
with gr.Blocks() as demo:
|
122 |
+
gr.Markdown("<h1><center>🍰Gradio chatbot backed by dataframe CSV memory🎨</center></h1>")
|
123 |
|
124 |
with gr.Row():
|
125 |
t1 = gr.Textbox(lines=1, default="", label="Chat Text:")
|
126 |
+
b1 = gr.Button("Respond and Retrieve Messages")
|
127 |
|
128 |
with gr.Row(): # inputs and buttons
|
129 |
s1 = gr.State([])
|