Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
Tobias Geisler
commited on
Commit
·
71736e8
1
Parent(s):
8379016
refactor
Browse files- app.py +39 -122
- database.py +1 -1
- requirements.txt +4 -0
- tabs/__init__.py +0 -0
- tabs/admin.py +48 -0
- tabs/chat.py +16 -0
- tabs/create_chatbot.py +23 -0
- utils/__init__.py +0 -0
- utils/openai_utils.py +26 -0
- utils.py → utils/utils.py +0 -0
app.py
CHANGED
@@ -1,128 +1,45 @@
|
|
1 |
import gradio as gr
|
2 |
-
import
|
3 |
-
from
|
4 |
-
from
|
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 |
-
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=
|
118 |
-
gr.update(value=
|
119 |
-
gr.update(value=
|
120 |
)
|
121 |
-
|
122 |
-
|
123 |
-
gr.update(value="", visible=True),
|
124 |
-
gr.update(value="")
|
125 |
-
)
|
126 |
|
127 |
if __name__ == "__main__":
|
|
|
128 |
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
+
from tabs.chat import create_chat_tab
|
3 |
+
from tabs.create_chatbot import create_chatbot_tab
|
4 |
+
from tabs.admin import create_admin_tab
|
5 |
+
|
6 |
+
def create_app():
|
7 |
+
with gr.Blocks(title="codora AI App Creator v0") as demo:
|
8 |
+
gr.Markdown("# codora AI App Creator v0")
|
9 |
+
chatbot_id_input = gr.Textbox(label="Enter Chatbot ID", visible=False)
|
10 |
+
|
11 |
+
with gr.Tabs():
|
12 |
+
with gr.Tab("Chat"):
|
13 |
+
chat_tab = create_chat_tab(chatbot_id_input)
|
14 |
+
|
15 |
+
with gr.Tab("Create Chatbot"):
|
16 |
+
create_chatbot_tab()
|
17 |
+
|
18 |
+
with gr.Tab("Admin"):
|
19 |
+
create_admin_tab()
|
20 |
+
|
21 |
+
@demo.load(inputs=[chatbot_id_input], outputs=[chat_tab['title'], chatbot_id_input, chat_tab['share_link']])
|
22 |
+
def load_chatbot(chatbot_id, request: gr.Request):
|
23 |
+
params = request.query_params
|
24 |
+
if "chatbot_id" in params:
|
25 |
+
chatbot_id = params["chatbot_id"]
|
26 |
+
|
27 |
+
from database import get_chatbot
|
28 |
+
chatbot = get_chatbot(chatbot_id)
|
29 |
+
if chatbot:
|
30 |
+
return (
|
31 |
+
gr.update(value=f"Welcome to {chatbot.name}"),
|
32 |
+
gr.update(value=chatbot_id, visible=True),
|
33 |
+
gr.update(value=f"https://huggingface.co/spaces/codora/ai-app-creator?chatbot_id={chatbot_id}")
|
34 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
35 |
return (
|
36 |
+
gr.update(value="Welcome to the Chatbot"),
|
37 |
+
gr.update(value="", visible=True),
|
38 |
+
gr.update(value="")
|
39 |
)
|
40 |
+
|
41 |
+
return demo
|
|
|
|
|
|
|
42 |
|
43 |
if __name__ == "__main__":
|
44 |
+
demo = create_app()
|
45 |
demo.launch()
|
database.py
CHANGED
@@ -3,7 +3,7 @@ import os
|
|
3 |
from sqlalchemy import create_engine, Column, Integer, String, Text, Boolean, DateTime, func
|
4 |
from sqlalchemy.ext.declarative import declarative_base
|
5 |
from sqlalchemy.orm import sessionmaker
|
6 |
-
from utils import get_secret
|
7 |
import random
|
8 |
from better_profanity import profanity
|
9 |
import logging
|
|
|
3 |
from sqlalchemy import create_engine, Column, Integer, String, Text, Boolean, DateTime, func
|
4 |
from sqlalchemy.ext.declarative import declarative_base
|
5 |
from sqlalchemy.orm import sessionmaker
|
6 |
+
from utils.utils import get_secret
|
7 |
import random
|
8 |
from better_profanity import profanity
|
9 |
import logging
|
requirements.txt
CHANGED
@@ -6,6 +6,7 @@ asttokens==2.4.1
|
|
6 |
attrs==23.2.0
|
7 |
backcall==0.2.0
|
8 |
beautifulsoup4==4.12.3
|
|
|
9 |
bleach==6.1.0
|
10 |
build==1.2.1
|
11 |
certifi==2024.7.4
|
@@ -29,6 +30,7 @@ fonttools==4.53.1
|
|
29 |
fsspec==2024.6.1
|
30 |
gradio==4.38.1
|
31 |
gradio_client==1.1.0
|
|
|
32 |
h11==0.14.0
|
33 |
httpcore==1.0.5
|
34 |
httptools==0.6.1
|
@@ -72,6 +74,7 @@ pydantic==2.8.2
|
|
72 |
pydantic_core==2.20.1
|
73 |
pydub==0.25.1
|
74 |
Pygments==2.18.0
|
|
|
75 |
pyparsing==3.1.2
|
76 |
pyproject_hooks==1.1.0
|
77 |
python-dateutil==2.9.0.post0
|
@@ -90,6 +93,7 @@ shellingham==1.5.4
|
|
90 |
six==1.16.0
|
91 |
sniffio==1.3.1
|
92 |
soupsieve==2.5
|
|
|
93 |
stack-data==0.6.3
|
94 |
starlette==0.37.2
|
95 |
tinycss2==1.3.0
|
|
|
6 |
attrs==23.2.0
|
7 |
backcall==0.2.0
|
8 |
beautifulsoup4==4.12.3
|
9 |
+
better-profanity==0.7.0
|
10 |
bleach==6.1.0
|
11 |
build==1.2.1
|
12 |
certifi==2024.7.4
|
|
|
30 |
fsspec==2024.6.1
|
31 |
gradio==4.38.1
|
32 |
gradio_client==1.1.0
|
33 |
+
greenlet==3.0.3
|
34 |
h11==0.14.0
|
35 |
httpcore==1.0.5
|
36 |
httptools==0.6.1
|
|
|
74 |
pydantic_core==2.20.1
|
75 |
pydub==0.25.1
|
76 |
Pygments==2.18.0
|
77 |
+
PyMySQL==1.1.1
|
78 |
pyparsing==3.1.2
|
79 |
pyproject_hooks==1.1.0
|
80 |
python-dateutil==2.9.0.post0
|
|
|
93 |
six==1.16.0
|
94 |
sniffio==1.3.1
|
95 |
soupsieve==2.5
|
96 |
+
SQLAlchemy==2.0.31
|
97 |
stack-data==0.6.3
|
98 |
starlette==0.37.2
|
99 |
tinycss2==1.3.0
|
tabs/__init__.py
ADDED
File without changes
|
tabs/admin.py
ADDED
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from utils.utils import get_secret
|
3 |
+
from database import get_all_chatbots, update_chatbot, delete_chatbot
|
4 |
+
|
5 |
+
ADMIN_PW = get_secret("ADMIN_PW")
|
6 |
+
|
7 |
+
def admin_view(password):
|
8 |
+
if password != ADMIN_PW:
|
9 |
+
return "Invalid admin password."
|
10 |
+
|
11 |
+
chatbots = get_all_chatbots()
|
12 |
+
return gr.Dataframe(
|
13 |
+
headers=["ID", "Name", "Custom Instruction", "Is Active"],
|
14 |
+
data=[[c.chatbot_id, c.name, c.custom_instruction, c.is_active] for c in chatbots]
|
15 |
+
)
|
16 |
+
|
17 |
+
def admin_action(action, chatbot_id, name, custom_instruction, is_active, password):
|
18 |
+
if password != ADMIN_PW:
|
19 |
+
return "Invalid admin password."
|
20 |
+
|
21 |
+
if action == "Edit":
|
22 |
+
update_chatbot(chatbot_id, name, custom_instruction, is_active)
|
23 |
+
return "Chatbot updated successfully."
|
24 |
+
elif action == "Delete":
|
25 |
+
delete_chatbot(chatbot_id)
|
26 |
+
return "Chatbot deleted successfully."
|
27 |
+
else:
|
28 |
+
return "Invalid action."
|
29 |
+
|
30 |
+
def create_admin_tab():
|
31 |
+
admin_password = gr.Textbox(label="Admin Password", type="password")
|
32 |
+
admin_button = gr.Button("View Chatbots")
|
33 |
+
admin_output = gr.Dataframe(headers=["ID", "Name", "Custom Instruction", "Is Active"])
|
34 |
+
admin_button.click(admin_view, inputs=[admin_password], outputs=admin_output)
|
35 |
+
|
36 |
+
with gr.Row():
|
37 |
+
admin_action_dropdown = gr.Dropdown(["Edit", "Delete"], label="Action")
|
38 |
+
admin_chatbot_id = gr.Textbox(label="Chatbot ID")
|
39 |
+
admin_name = gr.Textbox(label="Name")
|
40 |
+
admin_instruction = gr.Textbox(label="Custom Instruction")
|
41 |
+
admin_is_active = gr.Checkbox(label="Is Active")
|
42 |
+
admin_action_button = gr.Button("Perform Action")
|
43 |
+
admin_action_output = gr.Textbox(label="Action Result")
|
44 |
+
admin_action_button.click(
|
45 |
+
admin_action,
|
46 |
+
inputs=[admin_action_dropdown, admin_chatbot_id, admin_name, admin_instruction, admin_is_active, admin_password],
|
47 |
+
outputs=admin_action_output
|
48 |
+
)
|
tabs/chat.py
ADDED
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from utils.openai_utils import chat_with_bot
|
3 |
+
|
4 |
+
def create_chat_tab(chatbot_id_input):
|
5 |
+
chatbot_title = gr.Markdown("Welcome to the Chatbot")
|
6 |
+
chat_interface = gr.ChatInterface(
|
7 |
+
chat_with_bot,
|
8 |
+
additional_inputs=[chatbot_id_input],
|
9 |
+
)
|
10 |
+
share_link = gr.Textbox(label="Share Link", interactive=False)
|
11 |
+
|
12 |
+
return {
|
13 |
+
"title": chatbot_title,
|
14 |
+
"interface": chat_interface,
|
15 |
+
"share_link": share_link
|
16 |
+
}
|
tabs/create_chatbot.py
ADDED
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from utils.utils import get_secret
|
3 |
+
from database import create_chatbot, filter_profanity
|
4 |
+
|
5 |
+
CREATE_APP_PW = get_secret("CREATE_APP_PW")
|
6 |
+
|
7 |
+
def create_chatbot_interface(name, custom_instruction, password):
|
8 |
+
if password != CREATE_APP_PW:
|
9 |
+
return "Invalid password. Chatbot creation failed."
|
10 |
+
|
11 |
+
filtered_name = filter_profanity(name)
|
12 |
+
filtered_instruction = filter_profanity(custom_instruction)
|
13 |
+
|
14 |
+
chatbot = create_chatbot(filtered_name, filtered_instruction)
|
15 |
+
return f"Chatbot created with ID: {chatbot.chatbot_id}"
|
16 |
+
|
17 |
+
def create_chatbot_tab():
|
18 |
+
name = gr.Textbox(label="Chatbot Name")
|
19 |
+
instructions = gr.Textbox(label="Enter custom instructions for your chatbot")
|
20 |
+
create_password = gr.Textbox(label="Creation Password", type="password")
|
21 |
+
create_button = gr.Button("Create Chatbot")
|
22 |
+
create_output = gr.Textbox(label="Creation Result")
|
23 |
+
create_button.click(create_chatbot_interface, inputs=[name, instructions, create_password], outputs=create_output)
|
utils/__init__.py
ADDED
File without changes
|
utils/openai_utils.py
ADDED
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from openai import OpenAI
|
2 |
+
from utils.utils import get_secret
|
3 |
+
|
4 |
+
OPENAI_API_KEY = get_secret("OPENAI_API_KEY")
|
5 |
+
client = OpenAI(api_key=OPENAI_API_KEY)
|
6 |
+
|
7 |
+
def chat_with_bot(message, history, chatbot_id):
|
8 |
+
from database import get_chatbot
|
9 |
+
chatbot = get_chatbot(chatbot_id)
|
10 |
+
if not chatbot:
|
11 |
+
return "Invalid chatbot ID or chatbot not active"
|
12 |
+
|
13 |
+
conversation = [
|
14 |
+
{"role": "system", "content": chatbot.custom_instruction},
|
15 |
+
]
|
16 |
+
for human, assistant in history:
|
17 |
+
conversation.append({"role": "user", "content": human})
|
18 |
+
conversation.append({"role": "assistant", "content": assistant})
|
19 |
+
conversation.append({"role": "user", "content": message})
|
20 |
+
|
21 |
+
try:
|
22 |
+
response = client.chat.completions.create(model="gpt-3.5-turbo",
|
23 |
+
messages=conversation)
|
24 |
+
return response.choices[0].message.content
|
25 |
+
except Exception as e:
|
26 |
+
return f"An error occurred: {str(e)}"
|
utils.py → utils/utils.py
RENAMED
File without changes
|