Spaces:
Runtime error
Runtime error
| import gradio as gr | |
| import pandas as pd | |
| import numpy as np | |
| from scipy.fft import fft, fftfreq | |
| from sklearn.preprocessing import MinMaxScaler | |
| from tensorflow.keras.models import Sequential | |
| from tensorflow.keras.layers import LSTM, Dense | |
| import matplotlib.pyplot as plt | |
| def load_data(input_source): | |
| """Handle both uploaded files and URLs""" | |
| if isinstance(input_source, str) and input_source.startswith("http"): | |
| # Load from URL | |
| df = pd.read_csv( | |
| input_source, | |
| engine='python', | |
| on_bad_lines='warn', | |
| encoding='utf-8' | |
| ) | |
| else: | |
| # Load from uploaded file | |
| df = pd.read_csv( | |
| input_source.name, | |
| engine='python', | |
| on_bad_lines='warn', | |
| encoding='utf-8' | |
| ) | |
| # Common cleaning steps | |
| df = df.drop(columns=['Province/State', 'Lat', 'Long'], errors='ignore') | |
| df = df.groupby('Country/Region').sum().T | |
| df.index = pd.to_datetime(df.index) | |
| df['Global'] = df.sum(axis=1) | |
| return df['Global'].diff().fillna(0) | |
| def analyze_data(input_source): | |
| try: | |
| data = load_data(input_source) | |
| # Analysis logic | |
| N = len(data) | |
| yf = fft(data.values) | |
| xf = fftfreq(N, 1)[:N//2] | |
| cycle_days = int(1/xf[np.argmax(np.abs(yf[0:N//2]))]) | |
| # Create plot | |
| fig, ax = plt.subplots() | |
| ax.plot(data.index, data.values) | |
| ax.set_title("COVID-19 Daily New Cases Analysis") | |
| return ( | |
| f"๐ฎ Analysis Results:\n" | |
| f"- Cycle: {cycle_days} days\n" | |
| f"- Latest 30-day average: {data[-30:].mean():.1f} cases/day\n" | |
| f"- Current trend: {'โ Rising' if data[-1] > data[-7] else 'โ Falling'}", | |
| fig | |
| ) | |
| except Exception as e: | |
| return f"โ Error: {str(e)}", None | |
| # Create hybrid interface with chat and file upload | |
| with gr.Blocks(theme=gr.themes.Soft()) as app: | |
| gr.Markdown("# ๐ Data Analysis Bot") | |
| gr.Markdown("Upload a CSV file or paste a COVID data URL") | |
| with gr.Row(): | |
| with gr.Column(): | |
| file_upload = gr.File(label="Upload CSV", file_count=1) | |
| url_input = gr.Textbox(label="Or paste URL here") | |
| submit_btn = gr.Button("Analyze") | |
| with gr.Column(): | |
| chat = gr.Chatbot(height=400) | |
| plot_output = gr.Plot() | |
| # Handle both input methods | |
| submit_btn.click( | |
| fn=analyze_data, | |
| inputs=[gr.combine(file_upload, url_input)], | |
| outputs=[chat, plot_output] | |
| ) | |
| # Example inputs | |
| gr.Examples( | |
| examples=[ | |
| ["https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/data/time_series_covid19_confirmed_global.csv"], | |
| ["sample_data.csv"] # Upload this via Hugging Face | |
| ], | |
| inputs=[url_input] | |
| ) | |
| if __name__ == "__main__": | |
| app.launch() |