Spaces:
Build error
Build error
Tobias Geisler commited on
Commit ·
5d557f1
1
Parent(s): 71736e8
fix chatbot loading and share link
Browse files- app.py +27 -17
- database.py +15 -3
- tabs/chat.py +38 -6
app.py
CHANGED
|
@@ -1,16 +1,32 @@
|
|
| 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(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
gr.Markdown("# codora AI App Creator v0")
|
| 9 |
-
chatbot_id_input = gr.Textbox(label="Enter Chatbot ID"
|
|
|
|
|
|
|
| 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()
|
|
@@ -18,28 +34,22 @@ def create_app():
|
|
| 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
|
| 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
|
| 45 |
-
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from tabs.chat import create_chat_tab, load_chatbot
|
| 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(
|
| 8 |
+
title="codora AI App Creator v0",
|
| 9 |
+
head="""
|
| 10 |
+
<script>
|
| 11 |
+
function copyToClipboard() {
|
| 12 |
+
var copyText = document.querySelector("#share_link textarea").value;
|
| 13 |
+
navigator.clipboard.writeText(copyText).then(function() {
|
| 14 |
+
alert("Copied the text: " + copyText);
|
| 15 |
+
}, function(err) {
|
| 16 |
+
console.error("Could not copy text: ", err);
|
| 17 |
+
});
|
| 18 |
+
}
|
| 19 |
+
</script>
|
| 20 |
+
"""
|
| 21 |
+
) as demo:
|
| 22 |
gr.Markdown("# codora AI App Creator v0")
|
| 23 |
+
chatbot_id_input = gr.Textbox(label="Enter Chatbot ID")
|
| 24 |
+
load_button = gr.Button("Load Chatbot")
|
| 25 |
+
load_message = gr.Textbox(label="Load Message", interactive=False)
|
| 26 |
|
| 27 |
with gr.Tabs():
|
| 28 |
with gr.Tab("Chat"):
|
| 29 |
+
chat_tab = create_chat_tab(chatbot_id_input, load_button, load_message)
|
| 30 |
|
| 31 |
with gr.Tab("Create Chatbot"):
|
| 32 |
create_chatbot_tab()
|
|
|
|
| 34 |
with gr.Tab("Admin"):
|
| 35 |
create_admin_tab()
|
| 36 |
|
| 37 |
+
@demo.load(inputs=[chatbot_id_input], outputs=[chat_tab['title'], chatbot_id_input, chat_tab['share_link'], load_message])
|
| 38 |
+
def load_chatbot_on_start(chatbot_id, request: gr.Request):
|
| 39 |
params = request.query_params
|
| 40 |
if "chatbot_id" in params:
|
| 41 |
chatbot_id = params["chatbot_id"]
|
| 42 |
+
return load_chatbot(chatbot_id)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
return (
|
| 44 |
gr.update(value="Welcome to the Chatbot"),
|
| 45 |
gr.update(value="", visible=True),
|
| 46 |
+
gr.update(value=""),
|
| 47 |
gr.update(value="")
|
| 48 |
)
|
| 49 |
|
| 50 |
return demo
|
| 51 |
|
| 52 |
+
demo = create_app()
|
| 53 |
+
|
| 54 |
if __name__ == "__main__":
|
| 55 |
+
demo.launch()
|
|
|
database.py
CHANGED
|
@@ -22,7 +22,7 @@ logger = logging.getLogger(__name__)
|
|
| 22 |
|
| 23 |
try:
|
| 24 |
logger.info("Connecting to database at %s", DB_HOST)
|
| 25 |
-
engine = create_engine(DATABASE_URL)
|
| 26 |
# Test the connection
|
| 27 |
with engine.connect() as connection:
|
| 28 |
logger.info("Successfully connected to the database")
|
|
@@ -30,7 +30,6 @@ except Exception as e:
|
|
| 30 |
logger.error(f"Error connecting to the database: {str(e)}")
|
| 31 |
raise e
|
| 32 |
|
| 33 |
-
#engine = create_engine(DATABASE_URL)
|
| 34 |
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
|
| 35 |
|
| 36 |
Base = declarative_base()
|
|
@@ -75,7 +74,7 @@ def generate_chatbot_id():
|
|
| 75 |
'elephant', 'penguin', 'lion', 'dolphin', 'koala', 'panda', 'tiger', 'whale',
|
| 76 |
'butterfly', 'eagle', 'peacock', 'unicorn', 'phoenix', 'otter', 'swan',
|
| 77 |
'chameleon', 'ladybug', 'flamingo', 'puppy', 'kitten', 'fawn', 'hummingbird',
|
| 78 |
-
'koala', 'firefly', 'bunny', 'goldfish', 'puffin', 'orca', '
|
| 79 |
'parrot', 'owl', 'seahorse', 'hedgehog', 'sloth', 'duckling', 'starfish',
|
| 80 |
'gazelle', 'panther', 'robin', 'seal', 'lynx', 'jellyfish', 'gecko',
|
| 81 |
'kangaroo', 'lemur', 'meerkat', 'platypus', 'quokka', 'squirrel', 'toucan'
|
|
@@ -92,6 +91,9 @@ def create_chatbot(name, custom_instruction):
|
|
| 92 |
db.commit()
|
| 93 |
db.refresh(new_chatbot)
|
| 94 |
return new_chatbot
|
|
|
|
|
|
|
|
|
|
| 95 |
finally:
|
| 96 |
db.close()
|
| 97 |
|
|
@@ -99,6 +101,8 @@ def get_chatbot(chatbot_id):
|
|
| 99 |
db = SessionLocal()
|
| 100 |
try:
|
| 101 |
return db.query(Chatbot).filter(Chatbot.chatbot_id == chatbot_id, Chatbot.is_active == True).first()
|
|
|
|
|
|
|
| 102 |
finally:
|
| 103 |
db.close()
|
| 104 |
|
|
@@ -116,6 +120,9 @@ def update_chatbot(chatbot_id, name=None, custom_instruction=None, is_active=Non
|
|
| 116 |
db.commit()
|
| 117 |
db.refresh(chatbot)
|
| 118 |
return chatbot
|
|
|
|
|
|
|
|
|
|
| 119 |
finally:
|
| 120 |
db.close()
|
| 121 |
|
|
@@ -128,6 +135,9 @@ def delete_chatbot(chatbot_id):
|
|
| 128 |
db.commit()
|
| 129 |
return True
|
| 130 |
return False
|
|
|
|
|
|
|
|
|
|
| 131 |
finally:
|
| 132 |
db.close()
|
| 133 |
|
|
@@ -135,5 +145,7 @@ def get_all_chatbots():
|
|
| 135 |
db = SessionLocal()
|
| 136 |
try:
|
| 137 |
return db.query(Chatbot).all()
|
|
|
|
|
|
|
| 138 |
finally:
|
| 139 |
db.close()
|
|
|
|
| 22 |
|
| 23 |
try:
|
| 24 |
logger.info("Connecting to database at %s", DB_HOST)
|
| 25 |
+
engine = create_engine(DATABASE_URL, connect_args={"connect_timeout": 10})
|
| 26 |
# Test the connection
|
| 27 |
with engine.connect() as connection:
|
| 28 |
logger.info("Successfully connected to the database")
|
|
|
|
| 30 |
logger.error(f"Error connecting to the database: {str(e)}")
|
| 31 |
raise e
|
| 32 |
|
|
|
|
| 33 |
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
|
| 34 |
|
| 35 |
Base = declarative_base()
|
|
|
|
| 74 |
'elephant', 'penguin', 'lion', 'dolphin', 'koala', 'panda', 'tiger', 'whale',
|
| 75 |
'butterfly', 'eagle', 'peacock', 'unicorn', 'phoenix', 'otter', 'swan',
|
| 76 |
'chameleon', 'ladybug', 'flamingo', 'puppy', 'kitten', 'fawn', 'hummingbird',
|
| 77 |
+
'koala', 'firefly', 'bunny', 'goldfish', 'puffin', 'orca', 'red_panda', 'turtle',
|
| 78 |
'parrot', 'owl', 'seahorse', 'hedgehog', 'sloth', 'duckling', 'starfish',
|
| 79 |
'gazelle', 'panther', 'robin', 'seal', 'lynx', 'jellyfish', 'gecko',
|
| 80 |
'kangaroo', 'lemur', 'meerkat', 'platypus', 'quokka', 'squirrel', 'toucan'
|
|
|
|
| 91 |
db.commit()
|
| 92 |
db.refresh(new_chatbot)
|
| 93 |
return new_chatbot
|
| 94 |
+
except SQLAlchemyError as e:
|
| 95 |
+
db.rollback()
|
| 96 |
+
raise e
|
| 97 |
finally:
|
| 98 |
db.close()
|
| 99 |
|
|
|
|
| 101 |
db = SessionLocal()
|
| 102 |
try:
|
| 103 |
return db.query(Chatbot).filter(Chatbot.chatbot_id == chatbot_id, Chatbot.is_active == True).first()
|
| 104 |
+
except SQLAlchemyError as e:
|
| 105 |
+
raise e
|
| 106 |
finally:
|
| 107 |
db.close()
|
| 108 |
|
|
|
|
| 120 |
db.commit()
|
| 121 |
db.refresh(chatbot)
|
| 122 |
return chatbot
|
| 123 |
+
except SQLAlchemyError as e:
|
| 124 |
+
db.rollback()
|
| 125 |
+
raise e
|
| 126 |
finally:
|
| 127 |
db.close()
|
| 128 |
|
|
|
|
| 135 |
db.commit()
|
| 136 |
return True
|
| 137 |
return False
|
| 138 |
+
except SQLAlchemyError as e:
|
| 139 |
+
db.rollback()
|
| 140 |
+
raise e
|
| 141 |
finally:
|
| 142 |
db.close()
|
| 143 |
|
|
|
|
| 145 |
db = SessionLocal()
|
| 146 |
try:
|
| 147 |
return db.query(Chatbot).all()
|
| 148 |
+
except SQLAlchemyError as e:
|
| 149 |
+
raise e
|
| 150 |
finally:
|
| 151 |
db.close()
|
tabs/chat.py
CHANGED
|
@@ -1,16 +1,48 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from
|
| 3 |
|
| 4 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from database import get_chatbot
|
| 3 |
|
| 4 |
+
def load_chatbot(chatbot_id):
|
| 5 |
+
chatbot = get_chatbot(chatbot_id)
|
| 6 |
+
if chatbot:
|
| 7 |
+
return (
|
| 8 |
+
gr.update(value=f"Welcome to {chatbot.name}"),
|
| 9 |
+
gr.update(value=chatbot_id, visible=True),
|
| 10 |
+
gr.update(value=f"https://huggingface.co/spaces/codora/ai-app-creator?chatbot_id={chatbot_id}"),
|
| 11 |
+
gr.update(value="Chatbot loaded successfully.")
|
| 12 |
+
)
|
| 13 |
+
return (
|
| 14 |
+
gr.update(value="Welcome to the Chatbot"),
|
| 15 |
+
gr.update(value="", visible=True),
|
| 16 |
+
gr.update(value=""),
|
| 17 |
+
gr.update(value="Invalid chatbot ID or chatbot not active.")
|
| 18 |
+
)
|
| 19 |
+
|
| 20 |
+
def create_chat_tab(chatbot_id_input, load_button, load_message):
|
| 21 |
chatbot_title = gr.Markdown("Welcome to the Chatbot")
|
| 22 |
chat_interface = gr.ChatInterface(
|
| 23 |
chat_with_bot,
|
| 24 |
additional_inputs=[chatbot_id_input],
|
| 25 |
)
|
| 26 |
+
share_link = gr.Textbox(label="Share Link", interactive=False, elem_id="share_link")
|
| 27 |
+
|
| 28 |
+
load_button.click(
|
| 29 |
+
fn=load_chatbot,
|
| 30 |
+
inputs=[chatbot_id_input],
|
| 31 |
+
outputs=[chatbot_title, chatbot_id_input, share_link, load_message]
|
| 32 |
+
)
|
| 33 |
+
|
| 34 |
+
# Add a custom HTML button for copying the share URL to the clipboard
|
| 35 |
+
copy_button = gr.HTML("""
|
| 36 |
+
<button onclick="copyToClipboard()">Copy to Clipboard</button>
|
| 37 |
+
""")
|
| 38 |
+
|
| 39 |
return {
|
| 40 |
"title": chatbot_title,
|
| 41 |
"interface": chat_interface,
|
| 42 |
+
"share_link": share_link,
|
| 43 |
+
"copy_button": copy_button
|
| 44 |
+
}
|
| 45 |
+
|
| 46 |
+
def chat_with_bot(message, history, chatbot_id):
|
| 47 |
+
from utils.openai_utils import chat_with_bot as openai_chat_with_bot
|
| 48 |
+
return openai_chat_with_bot(message, history, chatbot_id)
|