ai-app-creator / app.py
Tobias Geisler
basic app
8379016
raw
history blame
5.12 kB
import gradio as gr
import os
from openai import OpenAI
from utils import get_secret
from database import create_chatbot, get_chatbot, update_chatbot, delete_chatbot, get_all_chatbots, filter_profanity
CREATE_APP_PW = get_secret("CREATE_APP_PW")
ADMIN_PW = get_secret("ADMIN_PW")
OPENAI_API_KEY = get_secret("OPENAI_API_KEY")
client = OpenAI(api_key=OPENAI_API_KEY)
def create_chatbot_interface(name, custom_instruction, password):
if password != CREATE_APP_PW:
return "Invalid password. Chatbot creation failed."
filtered_name = filter_profanity(name)
filtered_instruction = filter_profanity(custom_instruction)
chatbot = create_chatbot(filtered_name, filtered_instruction)
return f"Chatbot created with ID: {chatbot.chatbot_id}"
def chat_with_bot(message, history, chatbot_id):
chatbot = get_chatbot(chatbot_id)
if not chatbot:
return "Invalid chatbot ID or chatbot not active"
# Prepare the conversation history for OpenAI
conversation = [
{"role": "system", "content": chatbot.custom_instruction},
]
for human, assistant in history:
conversation.append({"role": "user", "content": human})
conversation.append({"role": "assistant", "content": assistant})
conversation.append({"role": "user", "content": message})
try:
response = client.chat.completions.create(model="gpt-3.5-turbo",
messages=conversation)
return response.choices[0].message.content
except Exception as e:
return f"An error occurred: {str(e)}"
def admin_view(password):
if password != ADMIN_PW:
return "Invalid admin password."
chatbots = get_all_chatbots()
return gr.Dataframe(
headers=["ID", "Name", "Custom Instruction", "Is Active"],
data=[[c.chatbot_id, c.name, c.custom_instruction, c.is_active] for c in chatbots]
)
def admin_action(action, chatbot_id, name, custom_instruction, is_active, password):
if password != ADMIN_PW:
return "Invalid admin password."
if action == "Edit":
update_chatbot(chatbot_id, name, custom_instruction, is_active)
return "Chatbot updated successfully."
elif action == "Delete":
delete_chatbot(chatbot_id)
return "Chatbot deleted successfully."
else:
return "Invalid action."
with gr.Blocks() as demo:
chatbot_id_input = gr.Textbox(label="Enter Chatbot ID", visible=False)
with gr.Tab("Chat"):
chatbot_title = gr.Markdown("Welcome to the Chatbot")
chat_interface = gr.ChatInterface(
chat_with_bot,
additional_inputs=[chatbot_id_input],
)
share_link = gr.Textbox(label="Share Link", interactive=False)
with gr.Tab("Create Chatbot"):
name = gr.Textbox(label="Chatbot Name")
instructions = gr.Textbox(label="Enter custom instructions for your chatbot")
create_password = gr.Textbox(label="Creation Password", type="password")
create_button = gr.Button("Create Chatbot")
create_output = gr.Textbox(label="Creation Result")
create_button.click(create_chatbot_interface, inputs=[name, instructions, create_password], outputs=create_output)
with gr.Tab("Admin"):
admin_password = gr.Textbox(label="Admin Password", type="password")
admin_button = gr.Button("View Chatbots")
admin_output = gr.Dataframe(headers=["ID", "Name", "Custom Instruction", "Is Active"])
admin_button.click(admin_view, inputs=[admin_password], outputs=admin_output)
with gr.Row():
admin_action_dropdown = gr.Dropdown(["Edit", "Delete"], label="Action")
admin_chatbot_id = gr.Textbox(label="Chatbot ID")
admin_name = gr.Textbox(label="Name")
admin_instruction = gr.Textbox(label="Custom Instruction")
admin_is_active = gr.Checkbox(label="Is Active")
admin_action_button = gr.Button("Perform Action")
admin_action_output = gr.Textbox(label="Action Result")
admin_action_button.click(
admin_action,
inputs=[admin_action_dropdown, admin_chatbot_id, admin_name, admin_instruction, admin_is_active, admin_password],
outputs=admin_action_output
)
@demo.load(inputs=[chatbot_id_input], outputs=[chatbot_title, chatbot_id_input, share_link])
def load_chatbot(chatbot_id, request: gr.Request):
# Check if there's a chatbot_id in the URL parameters
params = request.query_params
if "chatbot_id" in params:
chatbot_id = params["chatbot_id"]
chatbot = get_chatbot(chatbot_id)
if chatbot:
return (
gr.update(value=f"Welcome to {chatbot.name}"),
gr.update(value=chatbot_id, visible=True),
gr.update(value=f"https://huggingface.co/spaces/codora/ai-app-creator?chatbot_id={chatbot_id}")
)
return (
gr.update(value="Welcome to the Chatbot"),
gr.update(value="", visible=True),
gr.update(value="")
)
if __name__ == "__main__":
demo.launch()