-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.tsx
147 lines (134 loc) · 5.05 KB
/
App.tsx
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
import React, { useState } from 'react';
import { FileUpload } from './components/FileUpload';
import { GoogleSheetsInput } from './components/GoogleSheetsInput';
import { ColumnSelector } from './components/ColumnSelector';
import { QueryInput } from './components/QueryInput';
import { ResultsTable } from './components/ResultsTable';
import { ProcessedData, SearchResult } from './types';
import { ProcessingService } from './services/processingService';
import { Bot } from 'lucide-react';
import toast, { Toaster } from 'react-hot-toast';
function App() {
const [data, setData] = useState<ProcessedData | null>(null);
const [selectedColumn, setSelectedColumn] = useState('');
const [searchQuery, setSearchQuery] = useState('');
const [extractionPrompt, setExtractionPrompt] = useState('');
const [results, setResults] = useState<SearchResult[]>([]);
const [isProcessing, setIsProcessing] = useState(false);
const [dataSource, setDataSource] = useState<'file' | 'sheets' | null>(null);
const handleDataProcessed = (processedData: ProcessedData) => {
setData(processedData);
toast.success('Data loaded successfully!');
};
const handleProcess = async () => {
if (!data || !selectedColumn || !searchQuery || !extractionPrompt) {
toast.error('Please fill in all required fields');
return;
}
setIsProcessing(true);
const processingService = new ProcessingService();
const entities = data.rows.map(row => row[selectedColumn]);
try {
// Initialize results
setResults(
entities.map(entity => ({
entity,
extractedInfo: '',
status: 'pending',
}))
);
// Process entities and update results
await processingService.processEntities(
entities,
searchQuery,
extractionPrompt,
(result) => {
setResults(prev =>
prev.map(r =>
r.entity === result.entity ? result : r
)
);
}
);
toast.success('Processing completed!');
} catch (error) {
toast.error('Processing failed. Please try again.');
} finally {
setIsProcessing(false);
}
};
const handleDownload = () => {
const csv = [
['Entity', 'Extracted Information', 'Status'],
...results.map(r => [r.entity, r.extractedInfo, r.status])
].map(row => row.join(',')).join('\n');
const blob = new Blob([csv], { type: 'text/csv' });
const url = window.URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = 'results.csv';
a.click();
window.URL.revokeObjectURL(url);
};
return (
<div className="min-h-screen bg-gray-50">
<div className="max-w-7xl mx-auto py-12 px-4 sm:px-6 lg:px-8">
<div className="text-center mb-12">
<Bot className="mx-auto h-12 w-12 text-blue-600" />
<h1 className="mt-4 text-4xl font-bold text-gray-900">AI Data Extraction Agent</h1>
<p className="mt-2 text-lg text-gray-600">
Upload your data, define your search query, and let AI do the work
</p>
</div>
<div className="space-y-8">
{!data && !dataSource && (
<div className="grid gap-8 md:grid-cols-2">
<div className="bg-white rounded-lg shadow p-6">
<h2 className="text-lg font-semibold mb-4">Upload CSV File</h2>
<FileUpload onDataProcessed={(data) => {
setDataSource('file');
handleDataProcessed(data);
}} />
</div>
<div className="bg-white rounded-lg shadow p-6">
<h2 className="text-lg font-semibold mb-4">Connect Google Sheets</h2>
<GoogleSheetsInput onDataProcessed={(data) => {
setDataSource('sheets');
handleDataProcessed(data);
}} />
</div>
</div>
)}
{data && (
<div className="grid gap-8 md:grid-cols-2">
<div className="space-y-8">
<div className="bg-white rounded-lg shadow p-6">
<ColumnSelector
columns={data.columns}
selectedColumn={selectedColumn}
onColumnSelect={setSelectedColumn}
/>
</div>
<div className="bg-white rounded-lg shadow p-6">
<QueryInput
query={searchQuery}
extractionPrompt={extractionPrompt}
onQueryChange={setSearchQuery}
onExtractionPromptChange={setExtractionPrompt}
onSubmit={handleProcess}
isProcessing={isProcessing}
/>
</div>
</div>
<div className="bg-white rounded-lg shadow p-6">
<ResultsTable results={results} onDownload={handleDownload} />
</div>
</div>
)}
</div>
</div>
<Toaster position="top-right" />
</div>
);
}
export default App;