Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
Tobias Geisler
commited on
Commit
·
8379016
1
Parent(s):
61c774d
basic app
Browse files
app.py
CHANGED
@@ -1,30 +1,128 @@
|
|
1 |
import gradio as gr
|
|
|
2 |
from openai import OpenAI
|
3 |
from utils import get_secret
|
|
|
4 |
|
5 |
-
|
6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
|
8 |
-
def generate_response(prompt):
|
9 |
try:
|
10 |
-
response = client.chat.completions.create(
|
11 |
-
|
12 |
-
messages=[
|
13 |
-
{"role": "system", "content": "You are a helpful assistant."},
|
14 |
-
{"role": "user", "content": prompt}
|
15 |
-
]
|
16 |
-
)
|
17 |
return response.choices[0].message.content
|
18 |
except Exception as e:
|
19 |
-
return str(e)
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
|
29 |
if __name__ == "__main__":
|
30 |
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
+
import os
|
3 |
from openai import OpenAI
|
4 |
from utils import get_secret
|
5 |
+
from database import create_chatbot, get_chatbot, update_chatbot, delete_chatbot, get_all_chatbots, filter_profanity
|
6 |
|
7 |
+
CREATE_APP_PW = get_secret("CREATE_APP_PW")
|
8 |
+
ADMIN_PW = get_secret("ADMIN_PW")
|
9 |
+
OPENAI_API_KEY = get_secret("OPENAI_API_KEY")
|
10 |
+
|
11 |
+
client = OpenAI(api_key=OPENAI_API_KEY)
|
12 |
+
|
13 |
+
|
14 |
+
def create_chatbot_interface(name, custom_instruction, password):
|
15 |
+
if password != CREATE_APP_PW:
|
16 |
+
return "Invalid password. Chatbot creation failed."
|
17 |
+
|
18 |
+
filtered_name = filter_profanity(name)
|
19 |
+
filtered_instruction = filter_profanity(custom_instruction)
|
20 |
+
|
21 |
+
chatbot = create_chatbot(filtered_name, filtered_instruction)
|
22 |
+
return f"Chatbot created with ID: {chatbot.chatbot_id}"
|
23 |
+
|
24 |
+
def chat_with_bot(message, history, chatbot_id):
|
25 |
+
chatbot = get_chatbot(chatbot_id)
|
26 |
+
if not chatbot:
|
27 |
+
return "Invalid chatbot ID or chatbot not active"
|
28 |
+
|
29 |
+
# Prepare the conversation history for OpenAI
|
30 |
+
conversation = [
|
31 |
+
{"role": "system", "content": chatbot.custom_instruction},
|
32 |
+
]
|
33 |
+
for human, assistant in history:
|
34 |
+
conversation.append({"role": "user", "content": human})
|
35 |
+
conversation.append({"role": "assistant", "content": assistant})
|
36 |
+
conversation.append({"role": "user", "content": message})
|
37 |
|
|
|
38 |
try:
|
39 |
+
response = client.chat.completions.create(model="gpt-3.5-turbo",
|
40 |
+
messages=conversation)
|
|
|
|
|
|
|
|
|
|
|
41 |
return response.choices[0].message.content
|
42 |
except Exception as e:
|
43 |
+
return f"An error occurred: {str(e)}"
|
44 |
+
|
45 |
+
def admin_view(password):
|
46 |
+
if password != ADMIN_PW:
|
47 |
+
return "Invalid admin password."
|
48 |
+
|
49 |
+
chatbots = get_all_chatbots()
|
50 |
+
return gr.Dataframe(
|
51 |
+
headers=["ID", "Name", "Custom Instruction", "Is Active"],
|
52 |
+
data=[[c.chatbot_id, c.name, c.custom_instruction, c.is_active] for c in chatbots]
|
53 |
+
)
|
54 |
+
|
55 |
+
def admin_action(action, chatbot_id, name, custom_instruction, is_active, password):
|
56 |
+
if password != ADMIN_PW:
|
57 |
+
return "Invalid admin password."
|
58 |
+
|
59 |
+
if action == "Edit":
|
60 |
+
update_chatbot(chatbot_id, name, custom_instruction, is_active)
|
61 |
+
return "Chatbot updated successfully."
|
62 |
+
elif action == "Delete":
|
63 |
+
delete_chatbot(chatbot_id)
|
64 |
+
return "Chatbot deleted successfully."
|
65 |
+
else:
|
66 |
+
return "Invalid action."
|
67 |
+
|
68 |
+
with gr.Blocks() as demo:
|
69 |
+
chatbot_id_input = gr.Textbox(label="Enter Chatbot ID", visible=False)
|
70 |
+
|
71 |
+
with gr.Tab("Chat"):
|
72 |
+
chatbot_title = gr.Markdown("Welcome to the Chatbot")
|
73 |
+
chat_interface = gr.ChatInterface(
|
74 |
+
chat_with_bot,
|
75 |
+
additional_inputs=[chatbot_id_input],
|
76 |
+
)
|
77 |
+
share_link = gr.Textbox(label="Share Link", interactive=False)
|
78 |
+
|
79 |
+
with gr.Tab("Create Chatbot"):
|
80 |
+
name = gr.Textbox(label="Chatbot Name")
|
81 |
+
instructions = gr.Textbox(label="Enter custom instructions for your chatbot")
|
82 |
+
create_password = gr.Textbox(label="Creation Password", type="password")
|
83 |
+
create_button = gr.Button("Create Chatbot")
|
84 |
+
create_output = gr.Textbox(label="Creation Result")
|
85 |
+
create_button.click(create_chatbot_interface, inputs=[name, instructions, create_password], outputs=create_output)
|
86 |
+
|
87 |
+
with gr.Tab("Admin"):
|
88 |
+
admin_password = gr.Textbox(label="Admin Password", type="password")
|
89 |
+
admin_button = gr.Button("View Chatbots")
|
90 |
+
admin_output = gr.Dataframe(headers=["ID", "Name", "Custom Instruction", "Is Active"])
|
91 |
+
admin_button.click(admin_view, inputs=[admin_password], outputs=admin_output)
|
92 |
+
|
93 |
+
with gr.Row():
|
94 |
+
admin_action_dropdown = gr.Dropdown(["Edit", "Delete"], label="Action")
|
95 |
+
admin_chatbot_id = gr.Textbox(label="Chatbot ID")
|
96 |
+
admin_name = gr.Textbox(label="Name")
|
97 |
+
admin_instruction = gr.Textbox(label="Custom Instruction")
|
98 |
+
admin_is_active = gr.Checkbox(label="Is Active")
|
99 |
+
admin_action_button = gr.Button("Perform Action")
|
100 |
+
admin_action_output = gr.Textbox(label="Action Result")
|
101 |
+
admin_action_button.click(
|
102 |
+
admin_action,
|
103 |
+
inputs=[admin_action_dropdown, admin_chatbot_id, admin_name, admin_instruction, admin_is_active, admin_password],
|
104 |
+
outputs=admin_action_output
|
105 |
+
)
|
106 |
+
|
107 |
+
@demo.load(inputs=[chatbot_id_input], outputs=[chatbot_title, chatbot_id_input, share_link])
|
108 |
+
def load_chatbot(chatbot_id, request: gr.Request):
|
109 |
+
# Check if there's a chatbot_id in the URL parameters
|
110 |
+
params = request.query_params
|
111 |
+
if "chatbot_id" in params:
|
112 |
+
chatbot_id = params["chatbot_id"]
|
113 |
+
|
114 |
+
chatbot = get_chatbot(chatbot_id)
|
115 |
+
if chatbot:
|
116 |
+
return (
|
117 |
+
gr.update(value=f"Welcome to {chatbot.name}"),
|
118 |
+
gr.update(value=chatbot_id, visible=True),
|
119 |
+
gr.update(value=f"https://huggingface.co/spaces/codora/ai-app-creator?chatbot_id={chatbot_id}")
|
120 |
+
)
|
121 |
+
return (
|
122 |
+
gr.update(value="Welcome to the Chatbot"),
|
123 |
+
gr.update(value="", visible=True),
|
124 |
+
gr.update(value="")
|
125 |
+
)
|
126 |
|
127 |
if __name__ == "__main__":
|
128 |
demo.launch()
|