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 = """ | |
.gradio-container { | |
background-color: #f0f0f0; | |
font-family: 'Arial', sans-serif; | |
padding: 20px; | |
border-radius: 10px; | |
box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1); | |
} | |
h2 { | |
color: #333; | |
text-align: center; | |
} | |
.input-container { | |
display: flex; | |
align-items: center; | |
} | |
.gr-button { | |
background-color: #4CAF50; | |
color: white; | |
border: none; | |
border-radius: 5px; | |
margin-left: 10px; | |
} | |
.gr-button:hover { | |
background-color: #45a049; | |
} | |
.response-box { | |
background-color: white; | |
border-radius: 5px; | |
padding: 10px; | |
margin-top: 10px; | |
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1); | |
height: 200px; /* Fixed height for response area */ | |
overflow-y: auto; /* Scroll if content exceeds height */ | |
} | |
.settings-container { | |
margin-top: 20px; | |
} | |
""" | |
# Create Gradio interface | |
def create_interface(): | |
with gr.Blocks(css=css) as demo: | |
gr.Markdown("## Chat with Random Server") | |
# Input container for message and send button | |
with gr.Row(): | |
message = gr.Textbox(label="Message", placeholder="Type your message here...", elem_id="message-input", lines=1) | |
submit_btn = gr.Button("Send", elem_id="send-button") | |
output = gr.Textbox(label="Response", interactive=False, elem_id="response-box", lines=5) | |
# Settings moved to the bottom | |
with gr.Accordion("Settings", open=False, elem_id="settings-container"): | |
model = gr.Dropdown(choices=["Model A", "Model B"], label="Select Model", value="Model A") | |
preset = gr.Dropdown(choices=["Fast", "Normal", "Quality"], label="Select Preset", value="Fast") | |
# Button click event with loading indicator | |
submit_btn.click(call_api, inputs=[message, model, preset], outputs=output) | |
return demo | |
# Launch the interface | |
if __name__ == "__main__": | |
interface = create_interface() | |
interface.launch() |