Spaces:
Runtime error
Runtime error
Create Chatbot interface
Browse files- Chatbot interface +61 -0
Chatbot interface
ADDED
@@ -0,0 +1,61 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import pandas as pd
|
3 |
+
import numpy as np
|
4 |
+
from scipy.fft import fft, fftfreq
|
5 |
+
|
6 |
+
# Core Analysis Functions
|
7 |
+
def detect_cycles(data):
|
8 |
+
N = len(data)
|
9 |
+
yf = fft(data)
|
10 |
+
xf = fftfreq(N, 1)[:N//2]
|
11 |
+
dominant_freq = xf[np.argmax(np.abs(yf[0:N//2]))]
|
12 |
+
return int(1/dominant_freq)
|
13 |
+
|
14 |
+
def analyze_data(url):
|
15 |
+
try:
|
16 |
+
df = pd.read_csv(url)
|
17 |
+
values = df.sum(numeric_only=True).diff().fillna(0).values
|
18 |
+
return {
|
19 |
+
"cycles": detect_cycles(values),
|
20 |
+
"trend": "β Increasing" if values[-1] > values[-30] else "β Decreasing"
|
21 |
+
}
|
22 |
+
except Exception as e:
|
23 |
+
return {"error": str(e)}
|
24 |
+
|
25 |
+
# Chatbot Logic
|
26 |
+
def respond(message, history):
|
27 |
+
if "analyze" in message.lower():
|
28 |
+
url = message.split()[-1]
|
29 |
+
result = analyze_data(url)
|
30 |
+
|
31 |
+
if "error" in result:
|
32 |
+
return f"β Error: {result['error']}"
|
33 |
+
|
34 |
+
return f"""**Analysis Results:**
|
35 |
+
- π Dominant Cycle: {result['cycles']} days
|
36 |
+
- π 30-Day Trend: {result['trend']}
|
37 |
+
"""
|
38 |
+
|
39 |
+
elif any(w in message.lower() for w in ["hi", "hello", "help"]):
|
40 |
+
return """**Welcome to DeepSeek Analyst!** π€
|
41 |
+
Send me a CSV URL to analyze time-series data. Example:
|
42 |
+
`analyze https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/data/time_series_covid19_confirmed_global.csv`
|
43 |
+
"""
|
44 |
+
|
45 |
+
return "I specialize in data analysis. Send me a CSV URL to get started!"
|
46 |
+
|
47 |
+
# Gradio Interface
|
48 |
+
gr.ChatInterface(
|
49 |
+
respond,
|
50 |
+
chatbot=gr.Chatbot(height=400),
|
51 |
+
textbox=gr.Textbox(placeholder="Paste CSV URL here...", scale=7),
|
52 |
+
title="DeepSeek Analysis Chatbot",
|
53 |
+
description="Upload or paste CSV URLs to detect cycles and trends",
|
54 |
+
examples=[[
|
55 |
+
"analyze https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/data/time_series_covid19_confirmed_global.csv"
|
56 |
+
]],
|
57 |
+
theme="soft",
|
58 |
+
retry_btn=None,
|
59 |
+
undo_btn=None,
|
60 |
+
clear_btn="ποΈ Clear"
|
61 |
+
).launch()
|