Spaces:
Sleeping
Sleeping
import random | |
from gradio_client import Client | |
import gradio as gr | |
# List of available servers (replace with actual server endpoints) | |
servers = [ | |
"BICORP/GOGOGOGO", | |
"BICORP/server-2", | |
"BICORP/server-3", | |
"BICORP/server-4", | |
"BICORP/server-5" | |
] | |
# Function to call the API with error handling | |
def call_api(message, model, preset): | |
selected_server = random.choice(servers) # Randomly select a server | |
client = Client(selected_server) # Create a client for the selected server | |
try: | |
# Call the /chat endpoint with the provided parameters | |
result = client.predict( | |
message=message, | |
param_2=model, | |
param_3=preset, | |
api_name="/chat" | |
) | |
return result | |
except Exception as e: | |
return f"Error: {str(e)}" | |
# Custom CSS for styling | |
css = """ | |
/* Light Mode */ | |
:root { | |
--bg-color: #ffffff; | |
--text-color: #333; | |
--input-bg: #f8f8f8; | |
--border-color: #ddd; | |
--button-bg: #007bff; | |
--button-text: white; | |
} | |
/* Dark Mode */ | |
[data-theme='dark'] { | |
--bg-color: #1e1e1e; | |
--text-color: #f8f8f8; | |
--input-bg: #333; | |
--border-color: #555; | |
--button-bg: #0d6efd; | |
--button-text: white; | |
} | |
body { | |
background-color: var(--bg-color); | |
color: var(--text-color); | |
} | |
.gradio-container { | |
max-width: 600px; | |
margin: auto; | |
padding: 20px; | |
} | |
.chatbox { | |
border: 1px solid var(--border-color); | |
border-radius: 10px; | |
padding: 10px; | |
background-color: var(--input-bg); | |
min-height: 300px; | |
overflow-y: auto; | |
} | |
.input-row { | |
display: flex; | |
align-items: center; | |
border: 1px solid var(--border-color); | |
border-radius: 10px; | |
padding: 5px; | |
background-color: var(--input-bg); | |
} | |
#message-input { | |
flex-grow: 1; | |
border: none; | |
background: transparent; | |
color: var(--text-color); | |
} | |
#send-button { | |
background-color: var(--button-bg); | |
color: var(--button-text); | |
border-radius: 50%; | |
width: 40px; | |
height: 40px; | |
display: flex; | |
align-items: center; | |
justify-content: center; | |
} | |
#send-button:hover { | |
opacity: 0.8; | |
} | |
#settings-container { | |
border: 1px solid var(--border-color); | |
border-radius: 10px; | |
padding: 10px; | |
margin-top: 10px; | |
} | |
""" | |
# Function to toggle dark mode | |
def toggle_dark_mode(mode): | |
return gr.update(theme="dark" if mode else "default") | |
# Create Gradio interface | |
def create_interface(): | |
with gr.Blocks(css=css) as demo: | |
gr.Markdown("## 💬 Chatbot") | |
# Chatbox | |
chat_output = gr.HTML("", elem_classes="chatbox") | |
# Input container for message and send button | |
with gr.Row(elem_classes="input-row"): | |
message = gr.Textbox( | |
placeholder="Type a message...", | |
show_label=False, | |
elem_id="message-input" | |
) | |
submit_btn = gr.Button("➤", elem_id="send-button") | |
# Settings Panel | |
with gr.Accordion("⚙️ Settings", open=False, elem_id="settings-container"): | |
model = gr.Dropdown(choices=["Lake 1 Base"], label="Select Model", value="Lake 1 Base") | |
preset = gr.Dropdown(choices=["Fast", "Normal", "Quality", "Unreal Performance"], label="Select Preset", value="Fast") | |
dark_mode = gr.Checkbox(label="Dark Mode", value=False) | |
# Button click event | |
submit_btn.click(call_api, inputs=[message, model, preset], outputs=chat_output) | |
# Toggle dark mode | |
dark_mode.change(toggle_dark_mode, inputs=[dark_mode]) | |
return demo | |
# Launch the interface | |
if __name__ == "__main__": | |
interface = create_interface() | |
interface.launch() |