Spaces:
No application file
No application file
File size: 2,573 Bytes
b2e460b |
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 |
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, load_model
import requests
# --- Pre-trained Model (Simple LSTM) ---
def build_model():
model = Sequential([
tf.keras.layers.LSTM(32, input_shape=(30, 1)),
tf.keras.layers.Dense(1)
])
model.compile(loss='mse', optimizer='adam')
return model
# --- Core Functions ---
def analyze_data(data_url, prediction_days=30):
try:
# 1. Fetch data
df = pd.read_csv(data_url)
dates = df.columns[4:] # COVID data format
values = df.drop(columns=['Province/State', 'Country/Region', 'Lat', 'Long']).sum(axis=0)[4:].values.astype(float)
# 2. Detect cycles
N = len(values)
yf = fft(values)
xf = fftfreq(N, 1)[:N//2]
dominant_freq = xf[np.argmax(np.abs(yf[0:N//2]))]
cycle_days = int(1/dominant_freq)
# 3. Make predictions (simplified)
scaler = MinMaxScaler()
scaled = scaler.fit_transform(values.reshape(-1, 1))
model = build_model()
model.fit(scaled[:-10], scaled[10:], epochs=5, verbose=0) # Quick training
preds = model.predict(scaled[-30:].reshape(1, 30, 1))
preds = scaler.inverse_transform(preds).flatten().tolist()
# 4. Generate insights
insights = [
f"Dominant cycle: {cycle_days} days",
f"Next {prediction_days}-day trend: {'β Upward' if preds[-1] > preds[0] else 'β Downward'}",
"Action: Monitor closely around cycle peaks"
]
# Simple plot
plot = pd.DataFrame({
'Historical': values,
'Predicted': [None]*(len(values)) + preds
}).plot(title="Cases Analysis").figure
return plot, insights
except Exception as e:
return None, [f"Error: {str(e)}"]
# --- Gradio Interface ---
interface = gr.Interface(
fn=analyze_data,
inputs=[
gr.Textbox(label="Data URL",
value="https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/data/time_series_covid19_confirmed_global.csv"),
gr.Number(label="Days to Predict", value=30)
],
outputs=[
gr.Plot(label="Analysis"),
gr.JSON(label="Insights")
],
title="DeepSeek Lite Analyzer",
description="Analyze time-series data from public URLs. Works best with COVID-19 format data."
)
interface.launch() |