Deploy Inicial do SteelCheck com Docker Build Automatizado
This commit is contained in:
174
services/geminiService.ts
Normal file
174
services/geminiService.ts
Normal file
@@ -0,0 +1,174 @@
|
||||
import { GoogleGenAI, Type } from "@google/genai";
|
||||
import type { ReportData } from '../types';
|
||||
|
||||
const fileToGenerativePart = async (file: File) => {
|
||||
const base64EncodedDataPromise = new Promise<string>((resolve) => {
|
||||
const reader = new FileReader();
|
||||
reader.onloadend = () => resolve((reader.result as string).split(',')[1]);
|
||||
reader.readAsDataURL(file);
|
||||
});
|
||||
return {
|
||||
inlineData: { data: await base64EncodedDataPromise, mimeType: file.type },
|
||||
};
|
||||
};
|
||||
|
||||
/**
|
||||
* Cleans and parses a JSON string, removing potential markdown code blocks.
|
||||
* @param jsonString The raw string from the AI response.
|
||||
* @returns The parsed JSON object.
|
||||
*/
|
||||
const cleanAndParseJson = (jsonString: string): any => {
|
||||
const cleanedString = jsonString
|
||||
.replace(/^```json\s*/, '')
|
||||
.replace(/```$/, '')
|
||||
.trim();
|
||||
|
||||
try {
|
||||
return JSON.parse(cleanedString);
|
||||
} catch (error) {
|
||||
console.error("Failed to parse cleaned JSON. Original string:", jsonString);
|
||||
console.error("Cleaned string:", cleanedString);
|
||||
// Re-throw the parsing error to be caught by the caller
|
||||
throw new Error("A resposta da IA não estava no formato JSON esperado, mesmo após a limpeza.");
|
||||
}
|
||||
}
|
||||
|
||||
const reportSchema = {
|
||||
type: Type.OBJECT,
|
||||
properties: {
|
||||
confidence: { type: Type.NUMBER, description: "Grau de Confiança da Análise em percentual (ex: 98)." },
|
||||
identification: {
|
||||
type: Type.OBJECT,
|
||||
properties: {
|
||||
product: { type: Type.STRING, description: "Nome do produto/material (ex: Chapa de Aço)." },
|
||||
standards: { type: Type.STRING, description: "Norma(s) principal(is) citada(s) no certificado." },
|
||||
manufacturer: { type: Type.STRING, description: "Nome do fabricante ou revendedor." },
|
||||
certificateNumber: { type: Type.STRING, description: "Número do certificado." },
|
||||
certificateDate: { type: Type.STRING, description: "Data do certificado." },
|
||||
batches: { type: Type.STRING, description: "Lista de lotes (batches)." },
|
||||
heats: { type: Type.STRING, description: "Lista de corridas (heats)." },
|
||||
quantity: { type: Type.STRING, description: "Volume, peso ou medida do material." },
|
||||
},
|
||||
required: ["product", "standards", "manufacturer", "certificateNumber", "certificateDate", "batches", "heats", "quantity"]
|
||||
},
|
||||
compliance: {
|
||||
type: Type.OBJECT,
|
||||
properties: {
|
||||
status: { type: Type.STRING, enum: ["CONFORME", "NÃO CONFORME"], description: "Status geral de conformidade." },
|
||||
mechanical: {
|
||||
type: Type.ARRAY,
|
||||
items: {
|
||||
type: Type.OBJECT,
|
||||
properties: {
|
||||
property: { type: Type.STRING, description: "Propriedade mecânica (ex: Limite de Escoamento)." },
|
||||
norm: { type: Type.STRING, description: "Valor exigido pela norma (ex: Mín: 350 MPa)." },
|
||||
certificate: { type: Type.STRING, description: "Valor encontrado no certificado." },
|
||||
status: { type: Type.STRING, enum: ["OK", "FALHA"], description: "Status da propriedade." },
|
||||
},
|
||||
required: ["property", "norm", "certificate", "status"]
|
||||
}
|
||||
},
|
||||
chemical: {
|
||||
type: Type.ARRAY,
|
||||
items: {
|
||||
type: Type.OBJECT,
|
||||
properties: {
|
||||
element: { type: Type.STRING, description: "Elemento químico (ex: Carbono (C))." },
|
||||
norm: { type: Type.STRING, description: "Valor exigido pela norma (ex: Máx: 0.25%)." },
|
||||
certificate: { type: Type.STRING, description: "Valor encontrado no certificado." },
|
||||
status: { type: Type.STRING, enum: ["OK", "FALHA"], description: "Status do elemento." },
|
||||
},
|
||||
required: ["element", "norm", "certificate", "status"]
|
||||
}
|
||||
},
|
||||
otherTests: {
|
||||
type: Type.ARRAY,
|
||||
items: {
|
||||
type: Type.OBJECT,
|
||||
properties: {
|
||||
test: { type: Type.STRING, description: "Outro teste relevante (ex: Teste de Impacto Charpy)." },
|
||||
norm: { type: Type.STRING, description: "Valor exigido pela norma." },
|
||||
certificate: { type: Type.STRING, description: "Valor encontrado no certificado." },
|
||||
status: { type: Type.STRING, enum: ["OK", "FALHA"], description: "Status do teste." },
|
||||
},
|
||||
required: ["test", "norm", "certificate", "status"]
|
||||
}
|
||||
}
|
||||
},
|
||||
required: ["status", "mechanical", "chemical", "otherTests"]
|
||||
},
|
||||
overPerformance: {
|
||||
type: Type.ARRAY,
|
||||
items: {
|
||||
type: Type.OBJECT,
|
||||
properties: {
|
||||
property: { type: Type.STRING, description: "Ponto-chave de superdesempenho." },
|
||||
value: { type: Type.STRING, description: "Percentual acima do mínimo normativo (ex: 12.5%)." },
|
||||
},
|
||||
required: ["property", "value"]
|
||||
},
|
||||
description: "Lista de pontos onde o material excede os requisitos mínimos. Preencher apenas se o status for 'CONFORME'."
|
||||
},
|
||||
equivalents: {
|
||||
type: Type.ARRAY,
|
||||
items: {
|
||||
type: Type.OBJECT,
|
||||
properties: {
|
||||
system: { type: Type.STRING, description: "Sistema da norma (ex: EN (Europa))." },
|
||||
norm: { type: Type.STRING, description: "Norma equivalente (ex: S355J2)." },
|
||||
},
|
||||
required: ["system", "norm"]
|
||||
},
|
||||
description: "Lista de normas internacionais equivalentes."
|
||||
}
|
||||
},
|
||||
required: ["confidence", "identification", "compliance", "overPerformance", "equivalents"]
|
||||
};
|
||||
|
||||
export const analyzeCertificate = async (file: File, apiKey: string): Promise<ReportData> => {
|
||||
if (!apiKey) {
|
||||
throw new Error("A chave de API do Gemini não foi fornecida.");
|
||||
}
|
||||
|
||||
const ai = new GoogleGenAI({ apiKey: apiKey });
|
||||
const imagePart = await fileToGenerativePart(file);
|
||||
|
||||
const prompt = `Você é o "SteelCheck", um especialista sênior em engenharia de materiais, metalurgia e controle de qualidade, com profundo conhecimento em aços estruturais (ASTM, EN, DIN), parafusos (ASTM, ISO), consumíveis de soldagem (AWS, ISO) e revestimentos industriais (ISO, NACE, SSPC).
|
||||
|
||||
Sua tarefa é analisar o Certificado de Qualidade fornecido (que pode ser uma imagem ou um PDF) e gerar um "Relatório de Análise Técnica" em formato JSON, seguindo rigorosamente o schema fornecido.
|
||||
|
||||
**Instrução Crítica:** Antes de qualquer outra coisa, verifique a orientação do documento. Se ele estiver rotacionado (de lado ou de cabeça para baixo), ajuste-o mentalmente para a posição de leitura correta antes de iniciar a extração de dados. A análise correta depende da orientação adequada.
|
||||
|
||||
Execute as seguintes etapas:
|
||||
1. **Extração de Dados (OCR):** Extraia todos os dados primários do certificado, já considerando a orientação correta.
|
||||
2. **Identificação de Normas:** Identifique a(s) norma(s) que o certificado alega atender.
|
||||
3. **Análise de Conformidade:** Baseado no seu conhecimento profundo, compare os resultados dos testes (Propriedades Mecânicas, Composição Química, etc.) com os requisitos mínimos e máximos especificados pela(s) norma(s). Determine o status como "OK" ou "FALHA" para cada item e um "status" geral como "CONFORME" ou "NÃO CONFORME".
|
||||
4. **Análise de Superdesempenho:** Se o status geral for "CONFORME", calcule o quanto (em porcentagem) ele excede os requisitos-chave (ex: "18% acima do mínimo"). Se for "NÃO CONFORME", deixe a lista de superdesempenho vazia.
|
||||
5. **Análise de Equivalência:** Pesquise e liste materiais funcionalmente equivalentes de outras normas importantes (EN, DIN, JIS, NBR, etc.).
|
||||
6. **Grau de Confiança:** Forneça um Grau de Confiança (em %) sobre a precisão da sua análise, baseado na qualidade do documento.
|
||||
|
||||
Retorne APENAS o objeto JSON, sem nenhum texto ou formatação adicional.`;
|
||||
|
||||
const response = await ai.models.generateContent({
|
||||
model: 'gemini-2.5-flash',
|
||||
contents: { parts: [imagePart, { text: prompt }] },
|
||||
config: {
|
||||
responseMimeType: "application/json",
|
||||
responseSchema: reportSchema,
|
||||
temperature: 0.1,
|
||||
}
|
||||
});
|
||||
|
||||
try {
|
||||
const text = response.text;
|
||||
const data = cleanAndParseJson(text);
|
||||
return data as ReportData;
|
||||
} catch (e) {
|
||||
console.error("Failed to parse JSON response:", e);
|
||||
// Lançar o erro original ou um erro mais específico
|
||||
if (e instanceof Error) {
|
||||
throw e;
|
||||
}
|
||||
throw new Error("A resposta da IA não estava no formato JSON esperado.");
|
||||
}
|
||||
};
|
||||
59
services/pdfService.ts
Normal file
59
services/pdfService.ts
Normal file
@@ -0,0 +1,59 @@
|
||||
import jsPDF from 'jspdf';
|
||||
import html2canvas from 'html2canvas';
|
||||
|
||||
export const exportAsPdf = async (elementId: string, fileName: string, action: 'preview' | 'download'): Promise<void> => {
|
||||
const input = document.getElementById(elementId);
|
||||
if (!input) {
|
||||
console.error(`Element with id ${elementId} not found.`);
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const canvas = await html2canvas(input, {
|
||||
scale: 2, // Higher scale improves quality
|
||||
useCORS: true,
|
||||
logging: false,
|
||||
});
|
||||
|
||||
const imgData = canvas.toDataURL('image/png');
|
||||
|
||||
// A4 dimensions in mm: 210 x 297
|
||||
const pdf = new jsPDF({
|
||||
orientation: 'portrait',
|
||||
unit: 'mm',
|
||||
format: 'a4',
|
||||
});
|
||||
|
||||
const pdfWidth = pdf.internal.pageSize.getWidth();
|
||||
const pdfHeight = pdf.internal.pageSize.getHeight();
|
||||
const canvasWidth = canvas.width;
|
||||
const canvasHeight = canvas.height;
|
||||
|
||||
// Calculate the aspect ratio
|
||||
const ratio = canvasWidth / canvasHeight;
|
||||
let imgWidth = pdfWidth;
|
||||
let imgHeight = imgWidth / ratio;
|
||||
|
||||
// If the calculated height is greater than the page height, scale down
|
||||
if (imgHeight > pdfHeight) {
|
||||
imgHeight = pdfHeight;
|
||||
imgWidth = imgHeight * ratio;
|
||||
}
|
||||
|
||||
const x = (pdfWidth - imgWidth) / 2;
|
||||
const y = 0;
|
||||
|
||||
pdf.addImage(imgData, 'PNG', x, y, imgWidth, imgHeight);
|
||||
|
||||
if (action === 'download') {
|
||||
pdf.save(`${fileName}.pdf`);
|
||||
} else {
|
||||
const pdfBlob = pdf.output('blob');
|
||||
const pdfUrl = URL.createObjectURL(pdfBlob);
|
||||
window.open(pdfUrl, '_blank');
|
||||
URL.revokeObjectURL(pdfUrl);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("Error generating PDF:", error);
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user