Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
#Importing
|
| 2 |
import gradio as gr
|
| 3 |
import openai
|
| 4 |
import os
|
|
@@ -16,37 +16,44 @@ with gr.Blocks(theme=gr.themes.Monochrome(),css="footer{display:none !important}
|
|
| 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 |
def load_user():
|
| 20 |
-
global message_history
|
| 21 |
global instructions
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
|
|
|
| 25 |
|
| 26 |
-
new_id = str(uuid.uuid4())
|
| 27 |
-
return gr.Markdown(f"<h1><center> Session Id: {new_id} </center></h1>",visible=True)
|
| 28 |
|
| 29 |
-
#
|
| 30 |
initial_message = "Please write your prompt here and press 'enter'"
|
| 31 |
-
|
|
|
|
| 32 |
Chatbot = gr.Chatbot(label="Anonymous User")
|
| 33 |
|
| 34 |
-
|
| 35 |
def predict_prompt(input):
|
| 36 |
-
message_history.append({"role": "user", "content": input})
|
|
|
|
|
|
|
| 37 |
create_prompt = openai.ChatCompletion.create(
|
| 38 |
-
model = "gpt-4o",
|
| 39 |
messages = message_history,
|
| 40 |
)
|
| 41 |
|
| 42 |
-
#
|
| 43 |
reply_prompt = create_prompt.choices[0].message.content
|
| 44 |
|
| 45 |
-
#
|
| 46 |
message_history.append({"role": "assistant", "content": reply_prompt})
|
|
|
|
|
|
|
| 47 |
response = [(message_history[i]["content"], message_history[i+1]["content"]) for i in range(1, len(message_history)-1, 2)]
|
| 48 |
|
| 49 |
-
#
|
| 50 |
content = ''
|
| 51 |
for i in range(1,len(message_history), 1):
|
| 52 |
if (i % 2 == 1) and (message_history[i]["role"] == "user"):
|
|
@@ -56,15 +63,20 @@ with gr.Blocks(theme=gr.themes.Monochrome(),css="footer{display:none !important}
|
|
| 56 |
else:
|
| 57 |
content += ''
|
| 58 |
|
| 59 |
-
#
|
| 60 |
return response
|
| 61 |
-
|
|
|
|
| 62 |
with gr.Row():
|
|
|
|
|
|
|
| 63 |
txt = gr.Textbox(
|
| 64 |
show_label = False,
|
| 65 |
placeholder = initial_message,
|
| 66 |
)
|
| 67 |
-
|
|
|
|
| 68 |
txt.submit(predict_prompt, txt, Chatbot)
|
| 69 |
txt.submit(None, None, txt, js="() => {''}")
|
| 70 |
-
|
|
|
|
|
|
| 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
|
|
|
|
| 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 |
def predict_prompt(input):
|
| 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 |
+
#Asking openai for a prompt based on the current message history.
|
| 42 |
create_prompt = openai.ChatCompletion.create(
|
| 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 = ''
|
| 58 |
for i in range(1,len(message_history), 1):
|
| 59 |
if (i % 2 == 1) and (message_history[i]["role"] == "user"):
|
|
|
|
| 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(
|
| 74 |
show_label = False,
|
| 75 |
placeholder = initial_message,
|
| 76 |
)
|
| 77 |
+
|
| 78 |
+
#Adding the messages into the row with gradio.
|
| 79 |
txt.submit(predict_prompt, txt, Chatbot)
|
| 80 |
txt.submit(None, None, txt, js="() => {''}")
|
| 81 |
+
|
| 82 |
+
chatblock.launch() #launch!
|