Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,57 +1,44 @@
|
|
| 1 |
-
#Importing gradio for User Interface, importing openai api for the chatbot, importing os for retrieving the secrets from the environment to save as variables, importing uuid for creating a user id.
|
| 2 |
import gradio as gr
|
| 3 |
import openai
|
| 4 |
import os
|
| 5 |
from github import Github
|
| 6 |
import uuid
|
| 7 |
|
| 8 |
-
#Initializing secrets
|
| 9 |
-
openai.api_key = os.getenv('OPENAI_KEY')
|
| 10 |
|
| 11 |
-
# 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.
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
#
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
#When the chatblock loads we send the information from the following function below called "load_user" to "user_id".
|
| 18 |
-
@chatblock.load(outputs=[user_id])
|
| 19 |
-
#When the chatblock loads, the message history is initialized, the instructions are initialized, and the users id is created, sent, and made visible to the ui variable above.
|
| 20 |
-
def load_user():
|
| 21 |
-
global message_history #Made global variables so we can use them in the "predict_prompt" function.
|
| 22 |
global instructions
|
| 23 |
|
| 24 |
-
message_history = [] #List for message history.
|
| 25 |
-
instructions = 'You only speak in the form of periods and spaces. For example: "... .. .. ..... . ..."' #Change the message under the apostrophes to change the instructions you want the bot to inhibit.
|
| 26 |
-
message_history.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.
|
| 27 |
|
| 28 |
new_id = str(uuid.uuid4()) #id created by uuid.
|
| 29 |
return gr.Markdown(f"<h1><center> Session Id: {new_id} </center></h1>",visible=True) #Returning the "Markdown" object with what we want.
|
| 30 |
|
| 31 |
-
#Initial message for the User Interface (basically the message for the typing input area)
|
| 32 |
-
initial_message = "Please write your prompt here and press 'enter'"
|
| 33 |
|
| 34 |
-
#Creating a gradio-chatbot User Interface with the label "Anonymous User".
|
| 35 |
-
Chatbot = gr.Chatbot(label="Anonymous User")
|
| 36 |
|
| 37 |
-
#This function creates a response for the chatbot to respond with.
|
| 38 |
-
|
| 39 |
-
message_history.append({"role": "user", "content": input}) #This adds the input from the user and saves it to the history before we do anything else.
|
| 40 |
|
| 41 |
-
#
|
| 42 |
-
|
| 43 |
-
model = "gpt-4o", #Change the message in the qoutes to change the model. Note: it must be a recognized model by openai's api (case sensetive).
|
| 44 |
messages = message_history,
|
| 45 |
)
|
| 46 |
|
| 47 |
-
#Variable for the reply prompt.
|
| 48 |
-
reply_prompt = create_prompt.choices[0].message.content
|
| 49 |
|
| 50 |
-
#Adds the chatbots response to the history as an "assistant". This is the role commonly used for chatbots.
|
| 51 |
-
message_history.append({"role": "assistant", "content": reply_prompt})
|
| 52 |
|
| 53 |
-
#Variable for the response
|
| 54 |
-
response = [(message_history[i]["content"], message_history[i+1]["content"]) for i in range(1, len(message_history)-1, 2)]
|
| 55 |
|
| 56 |
#This chunk of code basically loops through the message history so we can write it to a save file repository.
|
| 57 |
content = ''
|
|
@@ -63,11 +50,9 @@ with gr.Blocks(theme=gr.themes.Monochrome(),css="footer{display:none !important}
|
|
| 63 |
else:
|
| 64 |
content += ''
|
| 65 |
|
| 66 |
-
#Returning the response
|
| 67 |
-
return response
|
| 68 |
|
| 69 |
-
#Creating the Row for the chatbot conversation using gradio.
|
| 70 |
-
with gr.Row():
|
| 71 |
|
| 72 |
#Create Gradio Textbox for the user to type into
|
| 73 |
txt = gr.Textbox(
|
|
|
|
| 1 |
+
# Importing gradio for User Interface, importing openai api for the chatbot, importing os for retrieving the secrets from the environment to save as variables, importing uuid for creating a user id.
|
| 2 |
import gradio as gr
|
| 3 |
import openai
|
| 4 |
import os
|
| 5 |
from github import Github
|
| 6 |
import uuid
|
| 7 |
|
| 8 |
+
openai.api_key = os.getenv('OPENAI_KEY') # Initializing secrets
|
|
|
|
| 9 |
|
| 10 |
+
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.
|
| 11 |
+
user_id = gr.Markdown(visible=False) # Displaying the user id, it is made not-visible because the id hasnt been created yet.
|
| 12 |
+
|
| 13 |
+
@chatblock.load(outputs=[user_id]) # When the chatblock loads we send the information from the following function below called "load_user" to "user_id".
|
| 14 |
+
def load_user(): # When the chatblock loads, the message history is initialized, the instructions are initialized, and the users id is created, sent, and made visible to the ui variable above.
|
| 15 |
+
global message_history # Made global variables so we can use them in the "predict_prompt" function.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
global instructions
|
| 17 |
|
| 18 |
+
message_history = [] # List for message history.
|
| 19 |
+
instructions = 'You only speak in the form of periods and spaces. For example: "... .. .. ..... . ..."' # Change the message under the apostrophes to change the instructions you want the bot to inhibit.
|
| 20 |
+
message_history.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.
|
| 21 |
|
| 22 |
new_id = str(uuid.uuid4()) #id created by uuid.
|
| 23 |
return gr.Markdown(f"<h1><center> Session Id: {new_id} </center></h1>",visible=True) #Returning the "Markdown" object with what we want.
|
| 24 |
|
| 25 |
+
initial_message = "Please write your prompt here and press 'enter'" # Initial message for the User Interface (basically the message for the typing input area so change the message under the quotes if you want to change that)
|
|
|
|
| 26 |
|
| 27 |
+
Chatbot = gr.Chatbot(label="Anonymous User") # Creating a gradio-chatbot User Interface with the label "Anonymous User".
|
|
|
|
| 28 |
|
| 29 |
+
def predict_prompt(input): # This function creates a response for the chatbot to respond with.
|
| 30 |
+
message_history.append({"role": "user", "content": input}) # This adds the input from the user and saves it to the history before we do anything else.
|
|
|
|
| 31 |
|
| 32 |
+
create_prompt = openai.ChatCompletion.create( #A sking openai for a prompt based on the current message history.
|
| 33 |
+
model = "gpt-4o", # Change the message in the qoutes to change the model. Note: it must be a recognized model by openai's api (case sensetive).
|
|
|
|
| 34 |
messages = message_history,
|
| 35 |
)
|
| 36 |
|
| 37 |
+
reply_prompt = create_prompt.choices[0].message.content # Variable for the reply prompt.
|
|
|
|
| 38 |
|
| 39 |
+
message_history.append({"role": "assistant", "content": reply_prompt}) # Adds the chatbots response to the history as an "assistant". This is the role commonly used for chatbots.
|
|
|
|
| 40 |
|
| 41 |
+
response = [(message_history[i]["content"], message_history[i+1]["content"]) for i in range(1, len(message_history)-1, 2)] # Variable for the response
|
|
|
|
| 42 |
|
| 43 |
#This chunk of code basically loops through the message history so we can write it to a save file repository.
|
| 44 |
content = ''
|
|
|
|
| 50 |
else:
|
| 51 |
content += ''
|
| 52 |
|
| 53 |
+
return response #Returning the response
|
|
|
|
| 54 |
|
| 55 |
+
with gr.Row(): #Creating the Row for the chatbot conversation using gradio.
|
|
|
|
| 56 |
|
| 57 |
#Create Gradio Textbox for the user to type into
|
| 58 |
txt = gr.Textbox(
|