File size: 1,997 Bytes
d412a1a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import pandas as pd
import numpy as np
from scipy.fft import fft, fftfreq

# Core Analysis Functions
def detect_cycles(data):
    N = len(data)
    yf = fft(data)
    xf = fftfreq(N, 1)[:N//2]
    dominant_freq = xf[np.argmax(np.abs(yf[0:N//2]))]
    return int(1/dominant_freq)

def analyze_data(url):
    try:
        df = pd.read_csv(url)
        values = df.sum(numeric_only=True).diff().fillna(0).values
        return {
            "cycles": detect_cycles(values),
            "trend": "↑ Increasing" if values[-1] > values[-30] else "↓ Decreasing"
        }
    except Exception as e:
        return {"error": str(e)}

# Chatbot Logic
def respond(message, history):
    if "analyze" in message.lower():
        url = message.split()[-1]
        result = analyze_data(url)
        
        if "error" in result:
            return f"❌ Error: {result['error']}"
            
        return f"""**Analysis Results:**
        - πŸ”„ Dominant Cycle: {result['cycles']} days
        - πŸ“ˆ 30-Day Trend: {result['trend']}
        """

    elif any(w in message.lower() for w in ["hi", "hello", "help"]):
        return """**Welcome to DeepSeek Analyst!** πŸ€–
        Send me a CSV URL to analyze time-series data. Example:
        `analyze https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/data/time_series_covid19_confirmed_global.csv`
        """
        
    return "I specialize in data analysis. Send me a CSV URL to get started!"

# Gradio Interface
gr.ChatInterface(
    respond,
    chatbot=gr.Chatbot(height=400),
    textbox=gr.Textbox(placeholder="Paste CSV URL here...", scale=7),
    title="DeepSeek Analysis Chatbot",
    description="Upload or paste CSV URLs to detect cycles and trends",
    examples=[[
        "analyze https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/data/time_series_covid19_confirmed_global.csv"
    ]],
    theme="soft",
    retry_btn=None,
    undo_btn=None,
    clear_btn="πŸ—‘οΈ Clear"
).launch()