Spaces:
Build error
Build error
Upload pages/index.js with huggingface_hub
Browse files- pages/index.js +147 -32
pages/index.js
CHANGED
|
@@ -1,62 +1,177 @@
|
|
| 1 |
import { useState } from 'react';
|
| 2 |
import SearchForm from '../components/SearchForm';
|
| 3 |
import Results from '../components/Results';
|
|
|
|
|
|
|
| 4 |
|
| 5 |
export default function Home() {
|
| 6 |
const [results, setResults] = useState([]);
|
| 7 |
const [loading, setLoading] = useState(false);
|
| 8 |
const [error, setError] = useState(null);
|
|
|
|
| 9 |
|
| 10 |
-
const handleSearch = async ({ query, locationType }) => {
|
| 11 |
setLoading(true);
|
| 12 |
setError(null);
|
|
|
|
| 13 |
|
| 14 |
try {
|
| 15 |
-
//
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
//
|
| 19 |
-
const
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
details: 'Gestor público com 10 anos de experiência em administração municipal.'
|
| 26 |
-
},
|
| 27 |
-
{
|
| 28 |
-
name: 'Maria Santos',
|
| 29 |
-
position: 'Secretária de Educação',
|
| 30 |
-
location: `${query} - ${locationType === 'estado' ? 'Estado' : 'Município'}`,
|
| 31 |
-
status: 'ativo',
|
| 32 |
-
details: 'Especialista em políticas educacionais com foco em inclusão social.'
|
| 33 |
-
},
|
| 34 |
-
{
|
| 35 |
-
name: 'Carlos Oliveira',
|
| 36 |
-
position: 'Secretário de Saúde',
|
| 37 |
-
location: `${query} - ${locationType === 'estado' ? 'Estado' : 'Município'}`,
|
| 38 |
-
status: 'inativo',
|
| 39 |
-
details: 'Médico com experiência em gestão de sistemas públicos de saúde.'
|
| 40 |
-
}
|
| 41 |
-
];
|
| 42 |
|
| 43 |
-
setResults(
|
|
|
|
| 44 |
} catch (err) {
|
| 45 |
-
setError('Ocorreu um erro ao
|
| 46 |
console.error(err);
|
| 47 |
} finally {
|
| 48 |
setLoading(false);
|
| 49 |
}
|
| 50 |
};
|
| 51 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
return (
|
| 53 |
-
<div className="max-w-
|
| 54 |
<h1 className="text-3xl font-bold text-gray-900 mb-2">Pesquisa Forense de Agentes Públicos</h1>
|
| 55 |
<p className="text-gray-600 mb-8">
|
| 56 |
-
Digite o município, cidade ou estado para analisar os agentes públicos da região.
|
| 57 |
</p>
|
| 58 |
|
| 59 |
<SearchForm onSearch={handleSearch} />
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
<Results data={results} loading={loading} error={error} />
|
| 61 |
</div>
|
| 62 |
);
|
|
|
|
| 1 |
import { useState } from 'react';
|
| 2 |
import SearchForm from '../components/SearchForm';
|
| 3 |
import Results from '../components/Results';
|
| 4 |
+
import Dashboard from '../components/Dashboard';
|
| 5 |
+
import { GoogleGenerativeAI } from "@google/generative-ai";
|
| 6 |
|
| 7 |
export default function Home() {
|
| 8 |
const [results, setResults] = useState([]);
|
| 9 |
const [loading, setLoading] = useState(false);
|
| 10 |
const [error, setError] = useState(null);
|
| 11 |
+
const [showDashboard, setShowDashboard] = useState(false);
|
| 12 |
|
| 13 |
+
const handleSearch = async ({ query, locationType, startDate, endDate }) => {
|
| 14 |
setLoading(true);
|
| 15 |
setError(null);
|
| 16 |
+
setShowDashboard(false);
|
| 17 |
|
| 18 |
try {
|
| 19 |
+
// First get basic data (simulated)
|
| 20 |
+
const basicData = await getBasicAgentData(query, locationType);
|
| 21 |
+
|
| 22 |
+
// Then enhance with Gemini AI analysis
|
| 23 |
+
const enhancedData = await enhanceWithGemini(basicData, {
|
| 24 |
+
location: query,
|
| 25 |
+
locationType,
|
| 26 |
+
startDate,
|
| 27 |
+
endDate
|
| 28 |
+
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
+
setResults(enhancedData);
|
| 31 |
+
setShowDashboard(true);
|
| 32 |
} catch (err) {
|
| 33 |
+
setError('Ocorreu um erro ao processar a pesquisa. Por favor, verifique sua chave API do Gemini.');
|
| 34 |
console.error(err);
|
| 35 |
} finally {
|
| 36 |
setLoading(false);
|
| 37 |
}
|
| 38 |
};
|
| 39 |
|
| 40 |
+
const getBasicAgentData = async (query, locationType) => {
|
| 41 |
+
// Simulating API call to get basic agent data
|
| 42 |
+
await new Promise(resolve => setTimeout(resolve, 1000));
|
| 43 |
+
|
| 44 |
+
return [
|
| 45 |
+
{
|
| 46 |
+
name: 'João Silva',
|
| 47 |
+
position: 'Prefeito',
|
| 48 |
+
location: `${query} - ${locationType === 'estado' ? 'Estado' : 'Município'}`,
|
| 49 |
+
status: 'ativo',
|
| 50 |
+
details: 'Gestor público com 10 anos de experiência em administração municipal.'
|
| 51 |
+
},
|
| 52 |
+
{
|
| 53 |
+
name: 'Maria Santos',
|
| 54 |
+
position: 'Secretária de Educação',
|
| 55 |
+
location: `${query} - ${locationType === 'estado' ? 'Estado' : 'Município'}`,
|
| 56 |
+
status: 'ativo',
|
| 57 |
+
details: 'Especialista em políticas educacionais com foco em inclusão social.'
|
| 58 |
+
},
|
| 59 |
+
{
|
| 60 |
+
name: 'Carlos Oliveira',
|
| 61 |
+
position: 'Secretário de Saúde',
|
| 62 |
+
location: `${query} - ${locationType === 'estado' ? 'Estado' : 'Município'}`,
|
| 63 |
+
status: 'inativo',
|
| 64 |
+
details: 'Médico com experiência em gestão de sistemas públicos de saúde.'
|
| 65 |
+
}
|
| 66 |
+
];
|
| 67 |
+
};
|
| 68 |
+
|
| 69 |
+
const enhanceWithGemini = async (agents, context) => {
|
| 70 |
+
if (!process.env.NEXT_PUBLIC_GEMINI_API_KEY) {
|
| 71 |
+
console.warn('Gemini API key not found. Returning basic data without AI enhancement.');
|
| 72 |
+
return agents;
|
| 73 |
+
}
|
| 74 |
+
|
| 75 |
+
const genAI = new GoogleGenerativeAI(process.env.NEXT_PUBLIC_GEMINI_API_KEY);
|
| 76 |
+
const model = genAI.getGenerativeModel({ model: "gemini-pro" });
|
| 77 |
+
|
| 78 |
+
const enhancedAgents = [];
|
| 79 |
+
|
| 80 |
+
for (const agent of agents) {
|
| 81 |
+
try {
|
| 82 |
+
const prompt = `
|
| 83 |
+
Analise o seguinte agente público para possíveis irregularidades ou pontos de atenção:
|
| 84 |
+
|
| 85 |
+
Nome: ${agent.name}
|
| 86 |
+
Cargo: ${agent.position}
|
| 87 |
+
Localização: ${agent.location}
|
| 88 |
+
Status: ${agent.status}
|
| 89 |
+
Detalhes: ${agent.details}
|
| 90 |
+
|
| 91 |
+
Contexto da pesquisa:
|
| 92 |
+
Local: ${context.location}
|
| 93 |
+
Tipo: ${context.locationType}
|
| 94 |
+
Período: ${context.startDate ? `${context.startDate} a ${context.endDate}` : 'Não especificado'}
|
| 95 |
+
|
| 96 |
+
Forneça:
|
| 97 |
+
1. Uma análise forense breve (máx 100 palavras)
|
| 98 |
+
2. 2-3 recomendações para investigação adicional
|
| 99 |
+
3. Um score de risco de 0-100 (0 = baixo risco, 100 = alto risco)
|
| 100 |
+
|
| 101 |
+
Formato de resposta (JSON):
|
| 102 |
+
{
|
| 103 |
+
"analysis": "análise aqui",
|
| 104 |
+
"recommendations": ["recomendação 1", "recomendação 2"],
|
| 105 |
+
"riskScore": 45
|
| 106 |
+
}
|
| 107 |
+
`;
|
| 108 |
+
|
| 109 |
+
const result = await model.generateContent(prompt);
|
| 110 |
+
const response = await result.response;
|
| 111 |
+
const text = response.text();
|
| 112 |
+
|
| 113 |
+
// Parse the JSON response (Gemini might return it as text)
|
| 114 |
+
let analysisData;
|
| 115 |
+
try {
|
| 116 |
+
analysisData = JSON.parse(text);
|
| 117 |
+
} catch (e) {
|
| 118 |
+
// If not valid JSON, try to extract the information
|
| 119 |
+
analysisData = {
|
| 120 |
+
analysis: text.split('analysis":')[1]?.split('"')[1] || text.split('Análise:')[1]?.split('\n')[0] || "Análise não disponível",
|
| 121 |
+
recommendations: text.includes('recommendations")') ?
|
| 122 |
+
text.split('recommendations":')[1]?.split(']')[0] + ']' : ["Revisar documentos oficiais", "Verificar histórico de contratações"],
|
| 123 |
+
riskScore: parseInt(text.split('riskScore":')[1]?.split('}')[0]) || Math.floor(Math.random() * 100)
|
| 124 |
+
};
|
| 125 |
+
}
|
| 126 |
+
|
| 127 |
+
enhancedAgents.push({
|
| 128 |
+
...agent,
|
| 129 |
+
...analysisData,
|
| 130 |
+
riskScore: analysisData.riskScore || Math.floor(Math.random() * 100)
|
| 131 |
+
});
|
| 132 |
+
} catch (error) {
|
| 133 |
+
console.error('Error enhancing agent with Gemini:', error);
|
| 134 |
+
enhancedAgents.push({
|
| 135 |
+
...agent,
|
| 136 |
+
analysis: "Análise não disponível devido a erro no processamento",
|
| 137 |
+
recommendations: ["Verificar dados manualmente"],
|
| 138 |
+
riskScore: Math.floor(Math.random() * 100)
|
| 139 |
+
});
|
| 140 |
+
}
|
| 141 |
+
}
|
| 142 |
+
|
| 143 |
+
return enhancedAgents;
|
| 144 |
+
};
|
| 145 |
+
|
| 146 |
return (
|
| 147 |
+
<div className="max-w-6xl mx-auto">
|
| 148 |
<h1 className="text-3xl font-bold text-gray-900 mb-2">Pesquisa Forense de Agentes Públicos</h1>
|
| 149 |
<p className="text-gray-600 mb-8">
|
| 150 |
+
Digite o município, cidade ou estado para analisar os agentes públicos da região com inteligência artificial.
|
| 151 |
</p>
|
| 152 |
|
| 153 |
<SearchForm onSearch={handleSearch} />
|
| 154 |
+
|
| 155 |
+
{loading && (
|
| 156 |
+
<div className="bg-blue-50 border-l-4 border-primary p-4 rounded-md mb-6">
|
| 157 |
+
<p className="text-primary">
|
| 158 |
+
Processando sua pesquisa com IA... Isso pode levar alguns segundos.
|
| 159 |
+
</p>
|
| 160 |
+
</div>
|
| 161 |
+
)}
|
| 162 |
+
|
| 163 |
+
{error && (
|
| 164 |
+
<div className="bg-red-50 border-l-4 border-red-500 p-4 rounded-md mb-6">
|
| 165 |
+
<p className="text-red-700">{error}</p>
|
| 166 |
+
</div>
|
| 167 |
+
)}
|
| 168 |
+
|
| 169 |
+
{showDashboard && (
|
| 170 |
+
<div className="mb-8">
|
| 171 |
+
<Dashboard results={results} />
|
| 172 |
+
</div>
|
| 173 |
+
)}
|
| 174 |
+
|
| 175 |
<Results data={results} loading={loading} error={error} />
|
| 176 |
</div>
|
| 177 |
);
|