import gradio as gr import pandas as pd import plotly.express as px import io import requests df_global = pd.DataFrame() # Load from file def load_data(file): try: if file is None: return pd.DataFrame() ext = file.name.split('.')[-1] if ext == 'csv': return pd.read_csv(file.name) elif ext in ['xls', 'xlsx']: return pd.read_excel(file.name) elif ext == 'json': return pd.read_json(file.name) except Exception as e: print("File load error:", e) return pd.DataFrame() # Load from URL def load_url(url): try: response = requests.get(url) response.raise_for_status() if url.endswith(".csv"): return pd.read_csv(io.StringIO(response.text)) elif url.endswith(".json"): return pd.read_json(io.StringIO(response.text)) elif url.endswith(".xlsx") or url.endswith(".xls"): return pd.read_excel(io.BytesIO(response.content)) except Exception as e: print("URL load error:", e) return pd.DataFrame() # Process inputs (file or URL) def process_inputs(file, url): global df_global df = load_url(url) if url else load_data(file) if df.empty: return pd.DataFrame(), gr.update(choices=[], value=None), gr.update(choices=[], value=None) df_global = df all_cols = list(df.columns) num_cols = list(df.select_dtypes(include="number").columns) default_x = all_cols[0] if all_cols else None default_y = num_cols[0] if num_cols else None return df.head(), gr.update(choices=all_cols, value=default_x), gr.update(choices=num_cols, value=default_y) # Plot and generate summary def update_plot(chart_type, x, y): global df_global if df_global.empty: return None, "No data loaded." try: chart = None if chart_type == "Bar Chart": chart = px.bar(df_global, x=x, y=y) elif chart_type == "Line Chart": chart = px.line(df_global, x=x, y=y) elif chart_type == "Scatter Plot": chart = px.scatter(df_global, x=x, y=y) elif chart_type == "Pie Chart": chart = px.pie(df_global, names=x, values=y) elif chart_type == "Box Plot": chart = px.box(df_global, x=x, y=y) summary_text = f"Chart Type: {chart_type}\nX-axis: {x}\nY-axis: {y}\n" if y in df_global.columns: stats = df_global[y].describe() summary_text += "\n" + stats.to_string() return chart, summary_text except Exception as e: print("Plot error:", e) return None, f"Error: {str(e)}" # Themed UI with gr.Blocks(css=""" body { margin: 0; padding: 0; font-family: 'Segoe UI', sans-serif; background: #0f2027; background: linear-gradient(to right, #2c5364, #203a43, #0f2027); color: skyblue; } #bg-theme { position: fixed; top: 0; left: 0; width: 100%; height: 100%; z-index: -2; background: linear-gradient(120deg, rgba(33,147,176,0.6), rgba(109,213,237,0.6)); background-size: 200% 200%; animation: bgMove 20s ease infinite; filter: blur(100px); } @keyframes bgMove { 0% { background-position: 0% 50%; } 50% { background-position: 100% 50%; } 100% { background-position: 0% 50%; } } .fade-in { animation: fadeInUp 1s ease both; } @keyframes fadeInUp { 0% { opacity: 0; transform: translateY(20px); } 100% { opacity: 1; transform: translateY(0); } } """) as demo: gr.HTML('
') # Themed animated background gr.Markdown( """