Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,64 +1,77 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
):
|
| 18 |
-
messages = [{"role": "system", "content": system_message}]
|
| 19 |
-
|
| 20 |
-
for val in history:
|
| 21 |
-
if val[0]:
|
| 22 |
-
messages.append({"role": "user", "content": val[0]})
|
| 23 |
-
if val[1]:
|
| 24 |
-
messages.append({"role": "assistant", "content": val[1]})
|
| 25 |
-
|
| 26 |
-
messages.append({"role": "user", "content": message})
|
| 27 |
-
|
| 28 |
-
response = ""
|
| 29 |
-
|
| 30 |
-
for message in client.chat_completion(
|
| 31 |
-
messages,
|
| 32 |
-
max_tokens=max_tokens,
|
| 33 |
-
stream=True,
|
| 34 |
-
temperature=temperature,
|
| 35 |
-
top_p=top_p,
|
| 36 |
-
):
|
| 37 |
-
token = message.choices[0].delta.content
|
| 38 |
-
|
| 39 |
-
response += token
|
| 40 |
-
yield response
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
"""
|
| 44 |
-
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
| 45 |
-
"""
|
| 46 |
-
demo = gr.ChatInterface(
|
| 47 |
-
respond,
|
| 48 |
-
additional_inputs=[
|
| 49 |
-
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
| 50 |
-
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 51 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 52 |
-
gr.Slider(
|
| 53 |
-
minimum=0.1,
|
| 54 |
-
maximum=1.0,
|
| 55 |
-
value=0.95,
|
| 56 |
-
step=0.05,
|
| 57 |
-
label="Top-p (nucleus sampling)",
|
| 58 |
-
),
|
| 59 |
],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
)
|
| 61 |
|
| 62 |
-
|
| 63 |
-
if __name__ == "__main__":
|
| 64 |
-
demo.launch()
|
|
|
|
| 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()
|
|
|
|
|
|