Spaces:
Runtime error
Runtime error
Eric Michael Martinez
commited on
Commit
•
39899ef
0
Parent(s):
adding chatbot
Browse files- app.py +99 -0
- requirements.txt +2 -0
app.py
ADDED
@@ -0,0 +1,99 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import openai
|
3 |
+
import examples as chatbot_examples
|
4 |
+
|
5 |
+
# Define a function to get the AI's reply using the OpenAI API
|
6 |
+
def get_ai_reply(model, system_message, message, history_state):
|
7 |
+
# Initialize the messages list with the system message
|
8 |
+
messages = [{"role": "system", "content": system_message}]
|
9 |
+
|
10 |
+
# Add the conversation history to the messages list
|
11 |
+
messages += history_state
|
12 |
+
|
13 |
+
# Add the user's message to the messages list
|
14 |
+
messages += [{"role": "user", "content": message}]
|
15 |
+
|
16 |
+
# Make an API call to the OpenAI ChatCompletion endpoint with the model and messages
|
17 |
+
completion = openai.ChatCompletion.create(
|
18 |
+
model=model,
|
19 |
+
messages=messages
|
20 |
+
)
|
21 |
+
|
22 |
+
# Extract and return the AI's response from the API response
|
23 |
+
return completion.choices[0].message.content
|
24 |
+
|
25 |
+
# Define a function to handle the chat interaction with the AI model
|
26 |
+
def chat(model, system_message, message, chatbot_messages, history_state):
|
27 |
+
# Initialize chatbot_messages and history_state if they are not provided
|
28 |
+
chatbot_messages = chatbot_messages or []
|
29 |
+
history_state = history_state or []
|
30 |
+
|
31 |
+
# Try to get the AI's reply using the get_ai_reply function
|
32 |
+
try:
|
33 |
+
ai_reply = get_ai_reply(model, system_message, message, history_state)
|
34 |
+
except:
|
35 |
+
# If an error occurs, return None and the current chatbot_messages and history_state
|
36 |
+
return None, chatbot_messages, history_state
|
37 |
+
|
38 |
+
# Append the user's message and the AI's reply to the chatbot_messages list
|
39 |
+
chatbot_messages.append((message, ai_reply))
|
40 |
+
|
41 |
+
# Append the user's message and the AI's reply to the history_state list
|
42 |
+
history_state.append({"role": "user", "content": message})
|
43 |
+
history_state.append({"role": "assistant", "content": ai_reply})
|
44 |
+
|
45 |
+
# Return None (empty out the user's message textbox), the updated chatbot_messages, and the updated history_state
|
46 |
+
return None, chatbot_messages, history_state
|
47 |
+
|
48 |
+
# Define a function to launch the chatbot interface using Gradio
|
49 |
+
def launch_chatbot(additional_examples=[], share=False):
|
50 |
+
# Load chatbot examples and merge with any additional examples provided
|
51 |
+
examples = chatbot_examples.load_examples(additional=additional_examples)
|
52 |
+
|
53 |
+
# Define a function to get the names of the examples
|
54 |
+
def get_examples():
|
55 |
+
return [example["name"] for example in examples]
|
56 |
+
|
57 |
+
# Define a function to choose an example based on the index
|
58 |
+
def choose_example(index):
|
59 |
+
system_message = examples[index]["system_message"].strip()
|
60 |
+
user_message = examples[index]["message"].strip()
|
61 |
+
return system_message, user_message, [], []
|
62 |
+
|
63 |
+
# Create the Gradio interface using the Blocks layout
|
64 |
+
with gr.Blocks() as demo:
|
65 |
+
with gr.Tab("Conversation"):
|
66 |
+
with gr.Row():
|
67 |
+
with gr.Column():
|
68 |
+
# Create a dropdown to select examples
|
69 |
+
example_dropdown = gr.Dropdown(get_examples(), label="Examples", type="index")
|
70 |
+
# Create a button to load the selected example
|
71 |
+
example_load_btn = gr.Button(value="Load")
|
72 |
+
# Create a textbox for the system message (prompt)
|
73 |
+
system_message = gr.Textbox(label="System Message (Prompt)", value="You are a helpful assistant.")
|
74 |
+
with gr.Column():
|
75 |
+
# Create a dropdown to select the AI model
|
76 |
+
model_selector = gr.Dropdown(
|
77 |
+
["gpt-3.5-turbo", "gpt-4"],
|
78 |
+
label="Model",
|
79 |
+
value="gpt-3.5-turbo"
|
80 |
+
)
|
81 |
+
# Create a chatbot interface for the conversation
|
82 |
+
chatbot = gr.Chatbot(label="Conversation")
|
83 |
+
# Create a textbox for the user's message
|
84 |
+
message = gr.Textbox(label="Message")
|
85 |
+
# Create a state object to store the conversation history
|
86 |
+
history_state = gr.State()
|
87 |
+
# Create a button to send the user's message
|
88 |
+
btn = gr.Button(value="Send")
|
89 |
+
|
90 |
+
# Connect the example load button to the choose_example function
|
91 |
+
example_load_btn.click(choose_example, inputs=[example_dropdown], outputs=[system_message, message, chatbot, history_state])
|
92 |
+
# Connect the send button to the chat function
|
93 |
+
btn.click(chat, inputs=[model_selector, system_message, message, chatbot, history_state], outputs=[message, chatbot, history_state])
|
94 |
+
# Launch the Gradio interface
|
95 |
+
demo.launch(share=share)
|
96 |
+
|
97 |
+
# Call the launch_chatbot function to start the chatbot interface using Gradio
|
98 |
+
# Set the share parameter to False, meaning the interface will not be publicly accessible
|
99 |
+
launch_chatbot(share=False)
|
requirements.txt
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
gradio
|
2 |
+
openai
|