Spaces:
Running
Running
| import matplotlib.pyplot as plt | |
| import matplotlib | |
| import pandas as pd | |
| import gradio as gr | |
| import random | |
| from utils import logger | |
| # Configure matplotlib to prevent memory warnings and set dark background | |
| matplotlib.rcParams['figure.facecolor'] = '#000000' | |
| matplotlib.rcParams['axes.facecolor'] = '#000000' | |
| matplotlib.rcParams['savefig.facecolor'] = '#000000' | |
| plt.ioff() # Turn off interactive mode to prevent figure accumulation | |
| # Function to generate random data for the plot | |
| def generate_random_data(): | |
| """Generate random data points for the scatter plot.""" | |
| n_points = random.randint(20, 50) # Random number of points | |
| x_data = [random.uniform(-100, 100) for _ in range(n_points)] | |
| y_data = [random.uniform(-100, 100) for _ in range(n_points)] | |
| return pd.DataFrame({"x": x_data, "y": y_data}) | |
| # Function to get current description text | |
| def get_description_text(): | |
| """Get description text.""" | |
| msg = [ | |
| "Random Data Dashboard", | |
| "-", | |
| "Click summary to refresh data", | |
| "Demo visualization", | |
| ] | |
| msg = ["**" + x + "**" for x in msg] + [""] | |
| msg.append("*Random scatter plot data*<br>*Updated on each click*") | |
| return "<br>".join(msg) | |
| # Load CSS from external file | |
| def load_css(): | |
| try: | |
| with open("styles.css", "r") as f: | |
| css_content = f.read() | |
| return css_content | |
| except FileNotFoundError: | |
| logger.warning("styles.css not found, using minimal default styles") | |
| return "body { background: #000; color: #fff; }" | |
| # Create the Gradio interface with sidebar and dark theme | |
| with gr.Blocks(title="Model Test Results Dashboard", css=load_css(), fill_height=True, fill_width=True) as demo: | |
| with gr.Row(): | |
| # Sidebar for model selection | |
| with gr.Column(scale=1, elem_classes=["sidebar"]): | |
| gr.Markdown("# 🤖 TCID", elem_classes=["sidebar-title"]) | |
| # Description with integrated last update time | |
| description_text = get_description_text() | |
| description_display = gr.Markdown(description_text, elem_classes=["sidebar-description"]) | |
| # Summary button | |
| summary_button = gr.Button( | |
| "summary\n📊", | |
| variant="primary", | |
| size="lg", | |
| elem_classes=["summary-button"] | |
| ) | |
| # Main content area | |
| with gr.Column(elem_classes=["main-content"]): | |
| # Summary display (default view) | |
| summary_display = gr.ScatterPlot( | |
| generate_random_data(), | |
| x = "x", | |
| y = "y", | |
| height="100vh", | |
| container=False, | |
| show_fullscreen_button=True, | |
| elem_classes=["plot-container"], | |
| ) | |
| # Summary button click handler | |
| def refresh_plot_data(): | |
| """Generate new random data for the plot.""" | |
| new_data = generate_random_data() | |
| return new_data, get_description_text() | |
| summary_button.click( | |
| fn=refresh_plot_data, | |
| outputs=[summary_display, description_display] | |
| ) | |
| # Auto-update description when the interface loads | |
| demo.load( | |
| fn=get_description_text, | |
| outputs=[description_display] | |
| ) | |
| # Gradio entrypoint | |
| if __name__ == "__main__": | |
| demo.launch() | |