Spaces:
Sleeping
Sleeping
| import os | |
| import json | |
| import google.generativeai as genai | |
| from typing import Dict | |
| import logging | |
| from .base_analyzer import BaseAnalyzer | |
| logger = logging.getLogger(__name__) | |
| class GeminiAnalyzer(BaseAnalyzer): | |
| def __init__(self): | |
| self.api_key = os.getenv("GOOGLE_API_KEY") | |
| if not self.api_key: | |
| logger.error("GOOGLE_API_KEY não encontrada nas variáveis de ambiente") | |
| raise ValueError("GOOGLE_API_KEY não configurada") | |
| genai.configure(api_key=self.api_key) | |
| logger.info("API Gemini configurada com sucesso") | |
| def _extract_json_from_response(self, response_text: str) -> str: | |
| json_content = response_text.strip() | |
| if json_content.startswith('```'): | |
| json_content = json_content.split('\n', 1)[1] | |
| if json_content.endswith('```'): | |
| json_content = json_content.rsplit('\n', 1)[0] | |
| return json_content.strip() | |
| def analyze(self, text: str) -> Dict: | |
| logger.info("Iniciando extração de representantes legais com Gemini") | |
| try: | |
| model = genai.GenerativeModel('gemini-pro') | |
| prompt = """ | |
| Analise o seguinte contrato social e extraia: | |
| 1. Todos os sócios e seus percentuais de participação | |
| 2. Todos os administradores mencionados | |
| Formate a resposta como um dicionário JSON com as seguintes chaves: | |
| - "socios": lista de dicionários com "nome" e "participacao" | |
| - "administradores": lista de nomes | |
| Contrato Social: | |
| {contract_text} | |
| """ | |
| response = model.generate_content(prompt.format(contract_text=text)) | |
| json_content = self._extract_json_from_response(response.text) | |
| result = json.loads(json_content) | |
| return result | |
| except Exception as e: | |
| logger.error(f"Erro na análise Gemini: {str(e)}") | |
| return { | |
| "socios": [], | |
| "administradores": [], | |
| "erro": str(e) | |
| } | |
| def format_output(self, analysis_result: Dict) -> str: | |
| output = "ANÁLISE DO CONTRATO SOCIAL (Gemini)\n\n" | |
| output += "SÓCIOS:\n" | |
| for socio in analysis_result.get("socios", []): | |
| participacao = socio.get('participacao', 'Não especificada') | |
| participacao_str = f"{participacao}%" if participacao is not None else "Participação não especificada" | |
| output += f"- {socio['nome']}: {participacao_str}\n" | |
| output += "\nADMINISTRADORES:\n" | |
| for admin in analysis_result.get("administradores", []): | |
| output += f"- {admin}\n" | |
| if "erro" in analysis_result: | |
| output += f"\nERRO: {analysis_result['erro']}" | |
| return output |