File size: 9,760 Bytes
f2b2374
274299c
f2b2374
274299c
cb2cc09
f2b2374
cb2cc09
f2b2374
 
 
27ab62c
 
 
f2b2374
 
 
 
 
 
274299c
f2b2374
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
274299c
 
f2b2374
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b2607f4
 
f2b2374
 
 
274299c
b2607f4
f2b2374
 
 
 
 
 
 
b2607f4
 
 
f2b2374
274299c
b2607f4
 
 
 
 
274299c
 
b2607f4
 
 
274299c
 
b2607f4
f2b2374
b2607f4
 
 
 
274299c
 
 
 
 
 
 
 
f2b2374
274299c
b2607f4
274299c
 
b2607f4
 
 
 
 
 
f2b2374
 
 
 
 
 
 
 
 
b2607f4
f2b2374
 
 
 
 
 
 
cfff021
cb2cc09
 
f2b2374
 
 
274299c
f2b2374
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4968d23
 
 
 
f2b2374
 
 
274299c
 
 
4968d23
 
 
 
 
 
 
 
274299c
 
f2b2374
 
 
 
4968d23
274299c
f2b2374
274299c
f2b2374
4968d23
cb2cc09
274299c
 
 
 
f2b2374
274299c
f2b2374
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
from fastapi import FastAPI, UploadFile, File, HTTPException
from fastapi.responses import HTMLResponse, JSONResponse
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel
import pandas as pd
import openpyxl
import numpy as np
from sentence_transformers import SentenceTransformer
from typing import List
import io

app = FastAPI()

app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_methods=["*"],
    allow_headers=["*"]
)

model = SentenceTransformer('all-MiniLM-L6-v2')

HTML_CONTENT = """
<!DOCTYPE html>
<html>
<head>
    <title>Analytics Dashboard</title>
    <script src="https://unpkg.com/react@18/umd/react.development.js"></script>
    <script src="https://unpkg.com/react-dom@18/umd/react-dom.development.js"></script>
    <script src="https://unpkg.com/recharts@2.1.9/umd/Recharts.js"></script>
    <script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
    <script src="https://cdn.tailwindcss.com"></script>
</head>
<body>
    <div id="root"></div>
    <script type="text/babel">
        function App() {
            const [file, setFile] = React.useState(null);
            const [data, setData] = React.useState(null);
            const [query, setQuery] = React.useState('');
            const [insights, setInsights] = React.useState([]);
            const [loading, setLoading] = React.useState(false);
            const [error, setError] = React.useState(null);

            const handleFileChange = (event) => {
                setFile(event.target.files[0]);
                setError(null);
            };

            const handleUpload = async () => {
                if (!file) return;
                setLoading(true);
                setError(null);

                const formData = new FormData();
                formData.append('file', file);

                try {
                    const response = await fetch('/api/process-file', {
                        method: 'POST',
                        body: formData,
                    });

                    if (!response.ok) {
                        const error = await response.json();
                        throw new Error(error.detail || 'Upload failed');
                    }

                    const result = await response.json();
                    setData(result);
                } catch (err) {
                    setError(err.message);
                } finally {
                    setLoading(false);
                }
            };

            const handleQuery = async () => {
                if (!data || !query) return;
                setLoading(true);
                try {
                    const response = await fetch('/api/query', {
                        method: 'POST',
                        headers: {'Content-Type': 'application/json'},
                        body: JSON.stringify({query, embeddings: data.embeddings})
                    });

                    if (!response.ok) throw new Error('Query failed');

                    const result = await response.json();
                    setInsights(result.similar_indices.map(idx => data.raw_data[idx]));
                } catch (err) {
                    setError(err.message);
                } finally {
                    setLoading(false);
                }
            };

            return (
                <div className="p-6 max-w-6xl mx-auto">
                    <h1 className="text-3xl font-bold mb-8">Data Analytics Dashboard</h1>
                    
                    {error && (
                        <div className="bg-red-100 border border-red-400 text-red-700 px-4 py-3 rounded mb-4">
                            {error}
                        </div>
                    )}

                    <div className="space-y-4">
                        <div className="flex flex-col gap-2">
                            <input
                                type="file"
                                onChange={handleFileChange}
                                accept=".xlsx,.csv"
                                className="block w-full text-sm border rounded p-2"
                                disabled={loading}
                            />
                            <button
                                onClick={handleUpload}
                                disabled={!file || loading}
                                className="bg-blue-500 text-white px-4 py-2 rounded disabled:opacity-50"
                            >
                                {loading ? 'Processing...' : 'Upload'}
                            </button>
                        </div>

                        {data && (
                            <div className="space-y-4">
                                <div className="flex gap-2">
                                    <input
                                        type="text"
                                        value={query}
                                        onChange={(e) => setQuery(e.target.value)}
                                        placeholder="Ask a question about your data..."
                                        className="flex-1 p-2 border rounded"
                                        disabled={loading}
                                    />
                                    <button
                                        onClick={handleQuery}
                                        className="px-4 py-2 bg-blue-600 text-white rounded hover:bg-blue-700 disabled:opacity-50"
                                        disabled={loading || !query}
                                    >
                                        Search
                                    </button>
                                </div>

                                {insights.length > 0 && (
                                    <div className="mt-8">
                                        <h3 className="text-xl font-semibold mb-4">Related Insights</h3>
                                        <div className="grid gap-4">
                                            {insights.map((insight, idx) => (
                                                <div key={idx} className="p-4 bg-gray-50 rounded shadow">
                                                    {Object.entries(insight).map(([key, value]) => (
                                                        <div key={key} className="mb-1">
                                                            <span className="font-medium">{key}:</span>{' '}
                                                            {value}
                                                        </div>
                                                    ))}
                                                </div>
                                            ))}
                                        </div>
                                    </div>
                                )}
                            </div>
                        )}
                    </div>
                </div>
            );
        }

        ReactDOM.render(<App />, document.getElementById('root'));
    </script>
</body>
</html>
"""

class QueryRequest(BaseModel):
    query: str
    embeddings: List[List[float]]

@app.get("/")
async def root():
    return HTMLResponse(HTML_CONTENT)

@app.post("/api/process-file")
async def process_file(file: UploadFile = File(...)):
    if not file.filename:
        raise HTTPException(status_code=400, detail="No file provided")
    
    try:
        contents = await file.read()
        if file.filename.endswith('.xlsx'):
            wb = openpyxl.load_workbook(io.BytesIO(contents), read_only=True)
            sheet = wb.active
            data = []
            headers = next(sheet.rows)
            header_values = [cell.value for cell in headers]
            for row in sheet.rows:
                if row != headers:
                    data.append({h: cell.value for h, cell in zip(header_values, row)})
            df = pd.DataFrame(data)
        elif file.filename.endswith('.csv'):
            df = pd.read_csv(io.BytesIO(contents))
        else:
            raise HTTPException(status_code=400, detail="Unsupported file type")

        # Convert timestamps to strings
        for col in df.select_dtypes(include=['datetime64[ns]']).columns:
            df[col] = df[col].astype(str)

        df = df.replace({np.nan: None})
        text_reps = [" ".join(f"{col}: {str(val)}" for col, val in row.items() if val is not None)
                    for _, row in df.iterrows()]
        
        embeddings = model.encode(text_reps)
        
        # Convert DataFrame to JSON-serializable format
        raw_data = df.to_dict('records')
        # Convert any non-serializable types to strings
        for record in raw_data:
            for key, value in record.items():
                if not isinstance(value, (str, int, float, bool, type(None))):
                    record[key] = str(value)
        
        return JSONResponse({
            'embeddings': embeddings.tolist(),
            'metadata': {
                'columns': list(df.columns),
                'row_count': len(df)
            },
            'raw_data': raw_data
        })
        
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))
        
@app.post("/api/query")
async def query_data(request: QueryRequest):
    try:
        query_embedding = model.encode([request.query])[0]
        similarities = np.dot(request.embeddings, query_embedding)
        return JSONResponse({"similar_indices": np.argsort(similarities)[-5:][::-1].tolist()})
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))