Spaces:
Runtime error
Runtime error
File size: 2,934 Bytes
df0578a 4589219 d32d511 df0578a d32d511 df0578a d32d511 4589219 d32d511 4589219 d32d511 4589219 d32d511 4589219 d32d511 4589219 d32d511 4589219 d32d511 df0578a d32d511 df0578a d32d511 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
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() |