PREDICTOR / Core Code
aiwhisperer33's picture
Create Core Code
b2e460b verified
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()