File size: 5,121 Bytes
0a440b6
8379016
0a440b6
 
8379016
0a440b6
8379016
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0a440b6
 
8379016
 
0a440b6
 
8379016
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0a440b6
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
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()