Spaces:
No application file
No application file
Create Core Code
Browse files
Core Code
ADDED
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import pandas as pd
|
3 |
+
import numpy as np
|
4 |
+
from scipy.fft import fft, fftfreq
|
5 |
+
from sklearn.preprocessing import MinMaxScaler
|
6 |
+
from tensorflow.keras.models import Sequential, load_model
|
7 |
+
import requests
|
8 |
+
|
9 |
+
# --- Pre-trained Model (Simple LSTM) ---
|
10 |
+
def build_model():
|
11 |
+
model = Sequential([
|
12 |
+
tf.keras.layers.LSTM(32, input_shape=(30, 1)),
|
13 |
+
tf.keras.layers.Dense(1)
|
14 |
+
])
|
15 |
+
model.compile(loss='mse', optimizer='adam')
|
16 |
+
return model
|
17 |
+
|
18 |
+
# --- Core Functions ---
|
19 |
+
def analyze_data(data_url, prediction_days=30):
|
20 |
+
try:
|
21 |
+
# 1. Fetch data
|
22 |
+
df = pd.read_csv(data_url)
|
23 |
+
dates = df.columns[4:] # COVID data format
|
24 |
+
values = df.drop(columns=['Province/State', 'Country/Region', 'Lat', 'Long']).sum(axis=0)[4:].values.astype(float)
|
25 |
+
|
26 |
+
# 2. Detect cycles
|
27 |
+
N = len(values)
|
28 |
+
yf = fft(values)
|
29 |
+
xf = fftfreq(N, 1)[:N//2]
|
30 |
+
dominant_freq = xf[np.argmax(np.abs(yf[0:N//2]))]
|
31 |
+
cycle_days = int(1/dominant_freq)
|
32 |
+
|
33 |
+
# 3. Make predictions (simplified)
|
34 |
+
scaler = MinMaxScaler()
|
35 |
+
scaled = scaler.fit_transform(values.reshape(-1, 1))
|
36 |
+
|
37 |
+
model = build_model()
|
38 |
+
model.fit(scaled[:-10], scaled[10:], epochs=5, verbose=0) # Quick training
|
39 |
+
|
40 |
+
preds = model.predict(scaled[-30:].reshape(1, 30, 1))
|
41 |
+
preds = scaler.inverse_transform(preds).flatten().tolist()
|
42 |
+
|
43 |
+
# 4. Generate insights
|
44 |
+
insights = [
|
45 |
+
f"Dominant cycle: {cycle_days} days",
|
46 |
+
f"Next {prediction_days}-day trend: {'↑ Upward' if preds[-1] > preds[0] else '↓ Downward'}",
|
47 |
+
"Action: Monitor closely around cycle peaks"
|
48 |
+
]
|
49 |
+
|
50 |
+
# Simple plot
|
51 |
+
plot = pd.DataFrame({
|
52 |
+
'Historical': values,
|
53 |
+
'Predicted': [None]*(len(values)) + preds
|
54 |
+
}).plot(title="Cases Analysis").figure
|
55 |
+
|
56 |
+
return plot, insights
|
57 |
+
|
58 |
+
except Exception as e:
|
59 |
+
return None, [f"Error: {str(e)}"]
|
60 |
+
|
61 |
+
# --- Gradio Interface ---
|
62 |
+
interface = gr.Interface(
|
63 |
+
fn=analyze_data,
|
64 |
+
inputs=[
|
65 |
+
gr.Textbox(label="Data URL",
|
66 |
+
value="https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/data/time_series_covid19_confirmed_global.csv"),
|
67 |
+
gr.Number(label="Days to Predict", value=30)
|
68 |
+
],
|
69 |
+
outputs=[
|
70 |
+
gr.Plot(label="Analysis"),
|
71 |
+
gr.JSON(label="Insights")
|
72 |
+
],
|
73 |
+
title="DeepSeek Lite Analyzer",
|
74 |
+
description="Analyze time-series data from public URLs. Works best with COVID-19 format data."
|
75 |
+
)
|
76 |
+
|
77 |
+
interface.launch()
|