Spaces:
Sleeping
Sleeping
Commit ·
4360e04
1
Parent(s): f2cfb58
feat: adicionar suporte a modelos OpenAI e dropdown de seleção
Browse filesCo-authored-by: aider (anthropic/claude-sonnet-4-20250514) <aider@aider.chat>
Files changed (2)
- app.py+60-21
- pyproject.toml+1
4 unmodified lines56789101141 unmodified lines535455555656575859605859616263646566676869707172637374757612 unmodified lines8990918283929394959697989914 unmodified lines1141151161041171181061191201211224 unmodified lines12712812913013113213311813413513613710 unmodified lines14814915015113513613713813915215315415515615715815916016116216316416516616716816917017117217311 unmodified lines1851861871571881891901912 unmodified lines1941951961661671681691701971981992002012022032042052062072082092102112124 unmodified linesfrom pydantic import BaseModelfrom typing import List, Optionalimport google.generativeai as genaiimport openaiimport osfrom dotenv import load_dotenvimport io41 unmodified lines except Exception as e: return None, {"error": f"Erro ao processar PDF: {str(e)}"}
def extract_references_with_llm(text): """Usa Pydantic AI com Gemini para extrair e estruturar referências"""def extract_references_with_llm(text, model_name): """Usa Pydantic AI com diferentes modelos para extrair e estruturar referências""" try: # Determinar se é modelo Google ou OpenAI if model_name.startswith('gemini'): # Configurar a API key do Google genai.configure(api_key=os.getenv("GOOGLE_API_KEY")) # Configurar a API key do Google genai.configure(api_key=os.getenv("GOOGLE_API_KEY")) api_key = os.getenv("GOOGLE_API_KEY") else: # Usar OpenAI api_key = os.getenv("OPENAI_API_KEY") if not api_key: return [{"error": f"Chave da API não encontrada para o modelo {model_name}"}] # Criar o agente Pydantic AI agent = Agent( 'gemini-2.5-pro', # Modelo Gemini 2.0 Flash model_name, result_type=ReferencesResponse, system_prompt=""" Você é um especialista em análise de artigos científicos. 12 unmodified lines """ ) # Limitar o texto para evitar exceder limites da API limited_text = text[:150000] # Gemini tem limite maior que GPT # Ajustar limite de texto baseado no modelo if model_name.startswith('gemini'): limited_text = text[:150000] # Gemini tem limite maior else: limited_text = text[:50000] # OpenAI tem limite menor # Executar o agente result = agent.run_sync(f"Extraia as referências bibliográficas do seguinte texto de artigo científico:\n\n{limited_text}")14 unmodified lines return references_list except Exception as e: return [{"error": f"Erro ao processar com LLM: {str(e)}"}] return [{"error": f"Erro ao processar com LLM ({model_name}): {str(e)}"}]
def process_pdf(pdf_file):def process_pdf(pdf_file, model_name): """Função principal que processa o PDF e retorna resultados""" if pdf_file is None: return {"error": "Nenhum arquivo enviado"}, pd.DataFrame()4 unmodified lines if text is None: return metadata, pd.DataFrame() # Adicionar modelo selecionado aos metadados metadata["modelo_usado"] = model_name # Extrair referências com LLM references = extract_references_with_llm(text) references = extract_references_with_llm(text, model_name) # Converter para DataFrame if references and not any("error" in ref for ref in references):10 unmodified lines gr.Markdown("Faça upload de um PDF de artigo científico para extrair automaticamente a lista de referências.") with gr.Row(): with gr.Column(): pdf_input = gr.File( label="📄 Upload do PDF", file_types=[".pdf"], type="binary" ) pdf_input = gr.File( label="📄 Upload do PDF", file_types=[".pdf"], type="binary" ) with gr.Column(): model_dropdown = gr.Dropdown( choices=[ "gemini-2.0-flash-exp", "gemini-1.5-pro", "gemini-1.5-flash", "gpt-4o", "gpt-4o-mini", "gpt-3.5-turbo" ], value="gemini-2.0-flash-exp", label="🤖 Modelo de IA", info="Selecione o modelo para extrair as referências" ) extract_btn = gr.Button("🔍 Extrair Referências", variant="primary") 11 unmodified lines extract_btn.click( process_pdf, inputs=[pdf_input], inputs=[pdf_input, model_dropdown], outputs=[metadata_output, references_output] ) 2 unmodified linesdef main(): load_dotenv() # Carrega variáveis de ambiente do arquivo .env # Verificar se a chave da API está configurada if not os.getenv("GEMINI_API_KEY"): print("⚠️ AVISO: Chave da API Google não encontrada!") print("Crie um arquivo .env com: GEMINI_API_KEY=sua_chave_aqui") print("Obtenha sua chave em: https://aistudio.google.com/app/apikey") # Verificar se as chaves das APIs estão configuradas google_key = os.getenv("GOOGLE_API_KEY") openai_key = os.getenv("OPENAI_API_KEY") if not google_key and not openai_key: print("⚠️ AVISO: Nenhuma chave de API encontrada!") print("Configure pelo menos uma das seguintes no arquivo .env:") print("- GOOGLE_API_KEY=sua_chave_do_google") print("- OPENAI_API_KEY=sua_chave_da_openai") elif not google_key: print("ℹ️ Apenas OpenAI configurado. Modelos Gemini não funcionarão.") elif not openai_key: print("ℹ️ Apenas Google configurado. Modelos OpenAI não funcionarão.") interface = create_interface() interface.launch(share=True)11 unmodified lines121314151611 unmodified lines "google-generativeai>=0.8.0", "python-dotenv>=1.0.0", "pandas-stubs==2.3.2.250827", "openai>=1.0.0",]app.py+60-21
pyproject.toml+1