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: #121212; | |
font-family: 'Inter', sans-serif; | |
padding: 25px; | |
border-radius: 12px; | |
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.25); | |
color: #FFFFFF; | |
} | |
h2 { | |
color: #E0E0E0; | |
text-align: center; | |
margin-bottom: 15px; | |
} | |
.input-container { | |
display: flex; | |
align-items: center; | |
justify-content: space-between; | |
gap: 10px; | |
} | |
.gr-button { | |
background-color: #1E88E5; | |
color: white; | |
font-weight: bold; | |
border: none; | |
border-radius: 8px; | |
padding: 8px 16px; | |
transition: background-color 0.3s ease-in-out, box-shadow 0.3s ease-in-out; | |
} | |
.gr-button:hover { | |
background-color: #1565C0; | |
box-shadow: 0 4px 8px rgba(21, 101, 192, 0.3); | |
} | |
.response-box { | |
background-color: #1E1E1E; | |
border: 1px solid #333; | |
border-radius: 8px; | |
padding: 15px; | |
margin-top: 15px; | |
box-shadow: inset 0 2px 4px rgba(0, 0, 0, 0.2); | |
height: 250px; /* Fixed height for response area */ | |
overflow-y: auto; /* Scroll if content exceeds height */ | |
} | |
.settings-container { | |
margin-top: 25px; | |
background-color: #1E1E1E; | |
border: 1px solid #333; | |
border-radius: 8px; | |
padding: 15px; | |
box-shadow: inset 0 2px 4px rgba(0, 0, 0, 0.2); | |
} | |
""" | |
# 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(elem_id="input-container"): | |
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=10, | |
show_label=False | |
) | |
# Settings section | |
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" | |
) | |
# 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() |