Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -8,7 +8,8 @@ import time
|
|
| 8 |
|
| 9 |
openai.api_key = os.getenv('OPENAI_KEY') # Initializing secrets
|
| 10 |
g = Github(os.getenv('GITHUB_KEY'))
|
| 11 |
-
|
|
|
|
| 12 |
|
| 13 |
with gr.Blocks(theme=gr.themes.Monochrome(),css="footer{display:none !important}") as chatblock: # Creating UI using gradio Blocks. This line sets the block as a variable called "chatblock", the theme as monochrome, and removes the footer of the page.
|
| 14 |
user_id = gr.TextArea(visible=False) # Displaying the user id, it is made not-visible because the id hasnt been created yet and we dont want the user seeing it anyway.
|
|
@@ -18,15 +19,15 @@ with gr.Blocks(theme=gr.themes.Monochrome(),css="footer{display:none !important}
|
|
| 18 |
global message_history # Made global variables so we can use them in the "predict_prompt" function.
|
| 19 |
global instructions
|
| 20 |
global filename
|
| 21 |
-
global user_dictionary
|
| 22 |
|
| 23 |
new_id = str(uuid.uuid4()) # Id created by uuid.
|
| 24 |
message_history = [] # Empty list for message history.
|
| 25 |
-
user_dictionary.update({new_id:message_history})
|
| 26 |
-
instructions = '
|
| 27 |
user_dictionary[new_id].append({"role": "developer", "content": instructions}) # This line adds the instructions to the message history (as a developer) so it will always inhibit the instructions no matter what the user says.
|
| 28 |
|
| 29 |
-
# Creating a new file with the
|
| 30 |
repo = g.get_user().get_repo("CATDATA")
|
| 31 |
filename = str(new_id) + '.txt'
|
| 32 |
repo.create_file(filename, "Saving" + filename, "")
|
|
@@ -57,7 +58,7 @@ with gr.Blocks(theme=gr.themes.Monochrome(),css="footer{display:none !important}
|
|
| 57 |
repo = g.get_user().get_repo("CATDATA")
|
| 58 |
commit_message = "Updating file content"
|
| 59 |
|
| 60 |
-
# This chunk of code basically loops through the message history so we can
|
| 61 |
content = ''
|
| 62 |
for i in range(1,len(user_dictionary[user_id]), 1):
|
| 63 |
if (i % 2 == 1) and (user_dictionary[user_id][i]["role"] == "user"):
|
|
@@ -66,14 +67,16 @@ with gr.Blocks(theme=gr.themes.Monochrome(),css="footer{display:none !important}
|
|
| 66 |
content += ('Bot: ' + user_dictionary[user_id][i]["content"] + '\n')
|
| 67 |
else:
|
| 68 |
content += ''
|
| 69 |
-
|
|
|
|
|
|
|
| 70 |
print(content)
|
| 71 |
|
| 72 |
#updating the repository with the entire message history
|
| 73 |
file = repo.get_contents(filename)
|
| 74 |
repo.update_file(file.path, commit_message, content, file.sha)
|
| 75 |
|
| 76 |
-
return response # Returning the response
|
| 77 |
|
| 78 |
with gr.Row(): # Creating the Row for the chatbot conversation using gradio.
|
| 79 |
|
|
|
|
| 8 |
|
| 9 |
openai.api_key = os.getenv('OPENAI_KEY') # Initializing secrets
|
| 10 |
g = Github(os.getenv('GITHUB_KEY'))
|
| 11 |
+
|
| 12 |
+
user_dictionary = {} #A dictionary that we can save mutible server side information. This will be formatted {new_id:message_history}
|
| 13 |
|
| 14 |
with gr.Blocks(theme=gr.themes.Monochrome(),css="footer{display:none !important}") as chatblock: # Creating UI using gradio Blocks. This line sets the block as a variable called "chatblock", the theme as monochrome, and removes the footer of the page.
|
| 15 |
user_id = gr.TextArea(visible=False) # Displaying the user id, it is made not-visible because the id hasnt been created yet and we dont want the user seeing it anyway.
|
|
|
|
| 19 |
global message_history # Made global variables so we can use them in the "predict_prompt" function.
|
| 20 |
global instructions
|
| 21 |
global filename
|
| 22 |
+
global user_dictionary # Pretty sure setting this as global is the only way to get it in the scope here but I dont know.
|
| 23 |
|
| 24 |
new_id = str(uuid.uuid4()) # Id created by uuid.
|
| 25 |
message_history = [] # Empty list for message history.
|
| 26 |
+
user_dictionary.update({new_id:message_history}) # Add the new id and empty history into the dictionary.
|
| 27 |
+
instructions = 'First Rule: Act like a human. Second Rule: Choose to be accommodative or unaccommodative' # Change the message under the apostrophes to change the instructions you want the bot to inhibit.
|
| 28 |
user_dictionary[new_id].append({"role": "developer", "content": instructions}) # This line adds the instructions to the message history (as a developer) so it will always inhibit the instructions no matter what the user says.
|
| 29 |
|
| 30 |
+
# Creating a new file with the new id to save the user responses and the chat bot responses to the repository
|
| 31 |
repo = g.get_user().get_repo("CATDATA")
|
| 32 |
filename = str(new_id) + '.txt'
|
| 33 |
repo.create_file(filename, "Saving" + filename, "")
|
|
|
|
| 58 |
repo = g.get_user().get_repo("CATDATA")
|
| 59 |
commit_message = "Updating file content"
|
| 60 |
|
| 61 |
+
# This chunk of code basically loops through the message history so we can update the save file with all the correct history in one piece of content.
|
| 62 |
content = ''
|
| 63 |
for i in range(1,len(user_dictionary[user_id]), 1):
|
| 64 |
if (i % 2 == 1) and (user_dictionary[user_id][i]["role"] == "user"):
|
|
|
|
| 67 |
content += ('Bot: ' + user_dictionary[user_id][i]["content"] + '\n')
|
| 68 |
else:
|
| 69 |
content += ''
|
| 70 |
+
|
| 71 |
+
# Print statements for the Code Logs
|
| 72 |
+
print("Session: " + user_id)
|
| 73 |
print(content)
|
| 74 |
|
| 75 |
#updating the repository with the entire message history
|
| 76 |
file = repo.get_contents(filename)
|
| 77 |
repo.update_file(file.path, commit_message, content, file.sha)
|
| 78 |
|
| 79 |
+
return response # Returning the chat bot response
|
| 80 |
|
| 81 |
with gr.Row(): # Creating the Row for the chatbot conversation using gradio.
|
| 82 |
|