Update app.py
Browse files
app.py
CHANGED
@@ -15,7 +15,7 @@ import string
|
|
15 |
import re
|
16 |
from llama_cpp import Llama
|
17 |
|
18 |
-
|
19 |
repo_name = 'TheBloke/OpenHermes-2.5-Mistral-7B-GGUF'
|
20 |
model_file = "openhermes-2.5-mistral-7b.Q4_K_M.gguf"
|
21 |
SCOPES = ['https://www.googleapis.com/auth/drive']
|
@@ -26,18 +26,20 @@ initContext = """<|im_start|>You are playing the role of an aggressive patient c
|
|
26 |
unique_id = ""
|
27 |
timestamp = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
|
28 |
|
29 |
-
|
30 |
def load_model():
|
31 |
llm = Llama(model_path=model_file, model_type="mistral",n_gpu_layers=-1,n_ctx = 2048)
|
32 |
return llm
|
33 |
|
|
|
34 |
def generate_unique_id():
|
35 |
# Generate a random sequence of 3 letters and 3 digits
|
36 |
letters = ''.join(random.choices(string.ascii_letters, k=3))
|
37 |
digits = ''.join(random.choices(string.digits, k=3))
|
38 |
unique_id = letters + digits
|
39 |
return unique_id
|
40 |
-
|
|
|
41 |
print('Fetching model:', repo_name, model_file)
|
42 |
snapshot_download(repo_id=repo_name, local_dir=".", allow_patterns=model_file)
|
43 |
print('Done fetching model:')
|
@@ -45,21 +47,22 @@ print('Done fetching model:')
|
|
45 |
|
46 |
class ChatbotAPP:
|
47 |
def __init__(self,model,service_account_file,scopes,folder_id,unique_id,initContext):
|
48 |
-
self.llm = model
|
49 |
-
self.service_account_file = service_account_file
|
50 |
-
self.scopes = scopes
|
51 |
-
self.folder_id = folder_id
|
52 |
-
self.unique_id = unique_id
|
53 |
-
self.chat_history = []
|
54 |
-
self.chat_log_history = []
|
55 |
-
self.isFirstRun = True
|
56 |
-
self.initContext = initContext
|
57 |
-
self.context = ""
|
58 |
-
self.agreed = False
|
59 |
-
self.service = self.get_drive_service()
|
60 |
-
self.app = self.create_app()
|
61 |
-
self.chat_log_name = ""
|
62 |
|
|
|
63 |
def get_drive_service(self):
|
64 |
credentials = service_account.Credentials.from_service_account_file(
|
65 |
self.service_account_file, scopes=self.scopes)
|
@@ -67,6 +70,14 @@ class ChatbotAPP:
|
|
67 |
print("Google Service Created")
|
68 |
return self.service
|
69 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
70 |
def search_file(self):
|
71 |
#Search for a file by name in the specified Google Drive folder.
|
72 |
query = f"name = '{self.chat_log_name}' and '{self.folder_id}' in parents and trashed = false"
|
@@ -78,7 +89,8 @@ class ChatbotAPP:
|
|
78 |
print(f"Chat log {self.chat_log_name} exist")
|
79 |
return files
|
80 |
|
81 |
-
def strip_text(self,text):
|
|
|
82 |
# Pattern to match text inside parentheses or angle brackets and any text following angle brackets
|
83 |
pattern = r"\(.*?\)|<.*?>.*"
|
84 |
|
@@ -87,7 +99,8 @@ class ChatbotAPP:
|
|
87 |
|
88 |
return cleaned_text
|
89 |
|
90 |
-
def upload_to_google_drive(self):
|
|
|
91 |
existing_files = self.search_file()
|
92 |
print(existing_files)
|
93 |
|
@@ -105,7 +118,8 @@ class ChatbotAPP:
|
|
105 |
with open(self.chat_log_name, "w") as log_file:
|
106 |
json.dump(data, log_file, indent=4)
|
107 |
|
108 |
-
if not existing_files:
|
|
|
109 |
# If the file does not exist, upload it
|
110 |
file_metadata = {
|
111 |
'name': self.chat_log_name,
|
@@ -122,7 +136,8 @@ class ChatbotAPP:
|
|
122 |
updated_file = self.service.files().update(fileId=file_id, media_body=media).execute()
|
123 |
print(f"Updated existing file with ID: {updated_file.get('id')}")
|
124 |
|
125 |
-
def generate(self,prompt, history):
|
|
|
126 |
|
127 |
#if not len(Name) == 0 and not len(Occupation) == 0 and not len(Ethnicity) == 0 and not len(Gender) == 0 and not len(Age) == 0 and not len(YearsOfExp):
|
128 |
if self.agreed:
|
@@ -177,7 +192,7 @@ class ChatbotAPP:
|
|
177 |
return self.chat_history
|
178 |
|
179 |
|
180 |
-
def start_chat_button_fn(self,agree_status):
|
181 |
|
182 |
if agree_status:
|
183 |
self.agreed = agree_status
|
@@ -186,13 +201,15 @@ class ChatbotAPP:
|
|
186 |
else:
|
187 |
return "You must agree to the terms and conditions to proceed"
|
188 |
|
189 |
-
def reset_chat_interface(self):
|
|
|
190 |
self.chat_history = []
|
191 |
self.chat_log_history = []
|
192 |
self.isFirstRun = True
|
193 |
return "Chat has been reset."
|
194 |
|
195 |
-
def reset_name_interface(self):
|
|
|
196 |
Name = ""
|
197 |
Occupation = ""
|
198 |
YearsOfExp = ""
|
@@ -204,14 +221,15 @@ class ChatbotAPP:
|
|
204 |
|
205 |
def reset_all(self):
|
206 |
|
207 |
-
message1 = reset_chat_interface()
|
208 |
#message2 = reset_name_interface()
|
209 |
-
message3 = load_model()
|
210 |
-
self.unique_id = generate_unique_id()
|
211 |
return f"All Chat components have been rest. Uniqe ID for this session is, {self.unique_id}. Please note this down.",self.unique_id
|
212 |
|
213 |
|
214 |
-
def create_app(self):
|
|
|
215 |
with gr.Blocks() as app:
|
216 |
gr.Markdown("# ECU-IVADE: Conversational AI Model for Aggressive Patient Behavior (Beta Testing)")
|
217 |
unique_id_display = gr.Textbox(value=self.unique_id, label="Session Unique ID", interactive=False,show_copy_button = True)
|
@@ -256,6 +274,7 @@ class ChatbotAPP:
|
|
256 |
reset_button.click(self.reset_all, inputs=[], outputs=[reset_output,unique_id_display])
|
257 |
return app
|
258 |
|
|
|
259 |
llm = load_model()
|
260 |
unique_id = generate_unique_id()
|
261 |
chatbot_app = ChatbotAPP(llm,SERVICE_ACCOUNT_FILE,SCOPES,folder_id,unique_id,initContext)
|
|
|
15 |
import re
|
16 |
from llama_cpp import Llama
|
17 |
|
18 |
+
# Variables for model, Google Drive, and initial context
|
19 |
repo_name = 'TheBloke/OpenHermes-2.5-Mistral-7B-GGUF'
|
20 |
model_file = "openhermes-2.5-mistral-7b.Q4_K_M.gguf"
|
21 |
SCOPES = ['https://www.googleapis.com/auth/drive']
|
|
|
26 |
unique_id = ""
|
27 |
timestamp = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
|
28 |
|
29 |
+
# Function to load the LLaMA model
|
30 |
def load_model():
|
31 |
llm = Llama(model_path=model_file, model_type="mistral",n_gpu_layers=-1,n_ctx = 2048)
|
32 |
return llm
|
33 |
|
34 |
+
# Function to generate a unique identifier for each chat session
|
35 |
def generate_unique_id():
|
36 |
# Generate a random sequence of 3 letters and 3 digits
|
37 |
letters = ''.join(random.choices(string.ascii_letters, k=3))
|
38 |
digits = ''.join(random.choices(string.digits, k=3))
|
39 |
unique_id = letters + digits
|
40 |
return unique_id
|
41 |
+
|
42 |
+
# Download the model from Hugging Face Hub
|
43 |
print('Fetching model:', repo_name, model_file)
|
44 |
snapshot_download(repo_id=repo_name, local_dir=".", allow_patterns=model_file)
|
45 |
print('Done fetching model:')
|
|
|
47 |
|
48 |
class ChatbotAPP:
|
49 |
def __init__(self,model,service_account_file,scopes,folder_id,unique_id,initContext):
|
50 |
+
self.llm = model # LLaMA model instance
|
51 |
+
self.service_account_file = service_account_file # Path to Google service account credentials
|
52 |
+
self.scopes = scopes # Google Drive API scopes
|
53 |
+
self.folder_id = folder_id # Google Drive folder ID to store chat logs
|
54 |
+
self.unique_id = unique_id # Unique identifier for the chat session
|
55 |
+
self.chat_history = [] # List to store chat history for the current session
|
56 |
+
self.chat_log_history = [] # List to store chat logs for uploading
|
57 |
+
self.isFirstRun = True # Flag to check if it's the first run of the chat session
|
58 |
+
self.initContext = initContext # Initial context for the chat session
|
59 |
+
self.context = "" # Current context for the chat session
|
60 |
+
self.agreed = False # Flag to check if the user agreed to terms and conditions
|
61 |
+
self.service = self.get_drive_service() # Google Drive service instance
|
62 |
+
self.app = self.create_app() # Gradio app instance
|
63 |
+
self.chat_log_name = "" # Filename for the chat log
|
64 |
|
65 |
+
# Method to create Google Drive service instance
|
66 |
def get_drive_service(self):
|
67 |
credentials = service_account.Credentials.from_service_account_file(
|
68 |
self.service_account_file, scopes=self.scopes)
|
|
|
70 |
print("Google Service Created")
|
71 |
return self.service
|
72 |
|
73 |
+
def generate_unique_id(): #in an instance the user resets using the reset button
|
74 |
+
# Generate a random sequence of 3 letters and 3 digits
|
75 |
+
letters = ''.join(random.choices(string.ascii_letters, k=3))
|
76 |
+
digits = ''.join(random.choices(string.digits, k=3))
|
77 |
+
unique_id = letters + digits
|
78 |
+
return unique_id
|
79 |
+
|
80 |
+
# Method to search for a chat log file in Google Drive
|
81 |
def search_file(self):
|
82 |
#Search for a file by name in the specified Google Drive folder.
|
83 |
query = f"name = '{self.chat_log_name}' and '{self.folder_id}' in parents and trashed = false"
|
|
|
89 |
print(f"Chat log {self.chat_log_name} exist")
|
90 |
return files
|
91 |
|
92 |
+
def strip_text(self,text): # Method to strip unwanted text from chat messages
|
93 |
+
|
94 |
# Pattern to match text inside parentheses or angle brackets and any text following angle brackets
|
95 |
pattern = r"\(.*?\)|<.*?>.*"
|
96 |
|
|
|
99 |
|
100 |
return cleaned_text
|
101 |
|
102 |
+
def upload_to_google_drive(self): # Method to upload the current chat log to Google Drive
|
103 |
+
|
104 |
existing_files = self.search_file()
|
105 |
print(existing_files)
|
106 |
|
|
|
118 |
with open(self.chat_log_name, "w") as log_file:
|
119 |
json.dump(data, log_file, indent=4)
|
120 |
|
121 |
+
if not existing_files: # Upload or update the chat log file on Google Drive
|
122 |
+
|
123 |
# If the file does not exist, upload it
|
124 |
file_metadata = {
|
125 |
'name': self.chat_log_name,
|
|
|
136 |
updated_file = self.service.files().update(fileId=file_id, media_body=media).execute()
|
137 |
print(f"Updated existing file with ID: {updated_file.get('id')}")
|
138 |
|
139 |
+
def generate(self,prompt, history): # Method to generate a response to the user's input
|
140 |
+
|
141 |
|
142 |
#if not len(Name) == 0 and not len(Occupation) == 0 and not len(Ethnicity) == 0 and not len(Gender) == 0 and not len(Age) == 0 and not len(YearsOfExp):
|
143 |
if self.agreed:
|
|
|
192 |
return self.chat_history
|
193 |
|
194 |
|
195 |
+
def start_chat_button_fn(self,agree_status): # Method to handle the start chat button action
|
196 |
|
197 |
if agree_status:
|
198 |
self.agreed = agree_status
|
|
|
201 |
else:
|
202 |
return "You must agree to the terms and conditions to proceed"
|
203 |
|
204 |
+
def reset_chat_interface(self): # Method to reset the chat interface
|
205 |
+
|
206 |
self.chat_history = []
|
207 |
self.chat_log_history = []
|
208 |
self.isFirstRun = True
|
209 |
return "Chat has been reset."
|
210 |
|
211 |
+
def reset_name_interface(self): # Method to create the Gradio app interface
|
212 |
+
|
213 |
Name = ""
|
214 |
Occupation = ""
|
215 |
YearsOfExp = ""
|
|
|
221 |
|
222 |
def reset_all(self):
|
223 |
|
224 |
+
message1 = self.reset_chat_interface()
|
225 |
#message2 = reset_name_interface()
|
226 |
+
#message3 = load_model()
|
227 |
+
self.unique_id = self.generate_unique_id()
|
228 |
return f"All Chat components have been rest. Uniqe ID for this session is, {self.unique_id}. Please note this down.",self.unique_id
|
229 |
|
230 |
|
231 |
+
def create_app(self): # Method to launch the Gradio app
|
232 |
+
|
233 |
with gr.Blocks() as app:
|
234 |
gr.Markdown("# ECU-IVADE: Conversational AI Model for Aggressive Patient Behavior (Beta Testing)")
|
235 |
unique_id_display = gr.Textbox(value=self.unique_id, label="Session Unique ID", interactive=False,show_copy_button = True)
|
|
|
274 |
reset_button.click(self.reset_all, inputs=[], outputs=[reset_output,unique_id_display])
|
275 |
return app
|
276 |
|
277 |
+
# Create an instance of the ChatbotAPP class and launch the app
|
278 |
llm = load_model()
|
279 |
unique_id = generate_unique_id()
|
280 |
chatbot_app = ChatbotAPP(llm,SERVICE_ACCOUNT_FILE,SCOPES,folder_id,unique_id,initContext)
|