Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,6 +1,6 @@
|
|
1 |
-
# 4/
|
2 |
|
3 |
-
# This version added saving chat history to a log file
|
4 |
# Updated the GPT model to gpt-4
|
5 |
# Add timestamp and ip address
|
6 |
|
@@ -26,8 +26,10 @@ from llama_index.llms.openai import OpenAI
|
|
26 |
from datetime import datetime;
|
27 |
import socket;
|
28 |
|
29 |
-
# access
|
30 |
from pathlib import Path
|
|
|
|
|
31 |
|
32 |
# deprecated
|
33 |
storage_context = StorageContext.from_defaults(persist_dir='./')
|
@@ -43,9 +45,19 @@ class Chatbot:
|
|
43 |
openai.api_key = api_key
|
44 |
self.chat_history = []
|
45 |
|
46 |
-
# access chat log file in
|
47 |
-
|
48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
49 |
def generate_response(self, user_input):
|
50 |
query_engine = index.as_query_engine(llm=llm_predictor)
|
51 |
response = query_engine.query(user_input)
|
@@ -65,10 +77,10 @@ class Chatbot:
|
|
65 |
def append_chat_history(self, user_input, output):
|
66 |
# append chat history
|
67 |
dt = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
68 |
-
print(dt)
|
69 |
hostname = socket.gethostname()
|
70 |
ip = socket.gethostbyname(hostname)
|
71 |
-
print(ip)
|
72 |
|
73 |
#self.chat_history.append({"role": "datetime", "content": dt})
|
74 |
#self.chat_history.append({"role": "IP", "content": ip})
|
@@ -85,8 +97,9 @@ class Chatbot:
|
|
85 |
self.chat_history.append(dictionary)
|
86 |
|
87 |
def save_chat_history(self):
|
88 |
-
with
|
89 |
-
|
|
|
90 |
|
91 |
def create_bot(user_input):
|
92 |
bot = Chatbot(os.getenv("OPENAI_API_KEY"), index=index)
|
@@ -107,7 +120,7 @@ def create_bot(user_input):
|
|
107 |
output = "Invalid request."
|
108 |
|
109 |
bot.append_chat_history(user_input, output)
|
110 |
-
|
111 |
|
112 |
return output
|
113 |
|
|
|
1 |
+
# 4/29/2024
|
2 |
|
3 |
+
# This version added saving chat history to a log file (need persist data from a space to a dataset)
|
4 |
# Updated the GPT model to gpt-4
|
5 |
# Add timestamp and ip address
|
6 |
|
|
|
26 |
from datetime import datetime;
|
27 |
import socket;
|
28 |
|
29 |
+
# access data folder of persistent storage
|
30 |
from pathlib import Path
|
31 |
+
from uuid import uuid4
|
32 |
+
from huggingface_hub import CommitScheduler
|
33 |
|
34 |
# deprecated
|
35 |
storage_context = StorageContext.from_defaults(persist_dir='./')
|
|
|
45 |
openai.api_key = api_key
|
46 |
self.chat_history = []
|
47 |
|
48 |
+
# access chat log file in data folder (persistent storage)
|
49 |
+
dataset_dir = Path("index")
|
50 |
+
dataset_dir.mkdir(parents=True, exist_ok=True)
|
51 |
+
self.history_file = dataset_dir / f"chat_log.json"
|
52 |
+
#self.history_file = f"zlmqi/index/chat_log.json"
|
53 |
+
|
54 |
+
scheduler = CommitScheduler(
|
55 |
+
repo_id="zlmqi",
|
56 |
+
repo_type="dataset",
|
57 |
+
folder_path=dataset_dir,
|
58 |
+
path_in_repo="data",
|
59 |
+
)
|
60 |
+
|
61 |
def generate_response(self, user_input):
|
62 |
query_engine = index.as_query_engine(llm=llm_predictor)
|
63 |
response = query_engine.query(user_input)
|
|
|
77 |
def append_chat_history(self, user_input, output):
|
78 |
# append chat history
|
79 |
dt = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
80 |
+
#print(dt)
|
81 |
hostname = socket.gethostname()
|
82 |
ip = socket.gethostbyname(hostname)
|
83 |
+
#print(ip)
|
84 |
|
85 |
#self.chat_history.append({"role": "datetime", "content": dt})
|
86 |
#self.chat_history.append({"role": "IP", "content": ip})
|
|
|
97 |
self.chat_history.append(dictionary)
|
98 |
|
99 |
def save_chat_history(self):
|
100 |
+
with scheduler.lock:
|
101 |
+
with open(self.history_file, 'w') as f:
|
102 |
+
json.dump(self.chat_history, f)
|
103 |
|
104 |
def create_bot(user_input):
|
105 |
bot = Chatbot(os.getenv("OPENAI_API_KEY"), index=index)
|
|
|
120 |
output = "Invalid request."
|
121 |
|
122 |
bot.append_chat_history(user_input, output)
|
123 |
+
bot.save_chat_history()
|
124 |
|
125 |
return output
|
126 |
|