Spaces:
Sleeping
Sleeping
refactor: atualizar configurações de API e ajustar extração de referências
Browse files
app.py
CHANGED
|
@@ -2,6 +2,7 @@ import gradio as gr
|
|
| 2 |
import pymupdf # PyMuPDF
|
| 3 |
import pandas as pd
|
| 4 |
from pydantic_ai import Agent
|
|
|
|
| 5 |
from pydantic import BaseModel
|
| 6 |
from typing import List, Optional
|
| 7 |
import google.generativeai as genai
|
|
@@ -59,8 +60,8 @@ def extract_references_with_llm(text, model_name):
|
|
| 59 |
# Determinar se é modelo Google ou OpenAI
|
| 60 |
if model_name.startswith('gemini'):
|
| 61 |
# Configurar a API key do Google
|
| 62 |
-
genai.configure(api_key=os.getenv("
|
| 63 |
-
api_key = os.getenv("
|
| 64 |
else:
|
| 65 |
# Usar OpenAI
|
| 66 |
api_key = os.getenv("OPENAI_API_KEY")
|
|
@@ -71,6 +72,9 @@ def extract_references_with_llm(text, model_name):
|
|
| 71 |
# Criar o agente Pydantic AI
|
| 72 |
agent = Agent(
|
| 73 |
model_name,
|
|
|
|
|
|
|
|
|
|
| 74 |
result_type=ReferencesResponse,
|
| 75 |
system_prompt="""
|
| 76 |
Você é um especialista em análise de artigos científicos.
|
|
@@ -124,7 +128,7 @@ def extract_references_with_regex(text):
|
|
| 124 |
# Padrões melhorados para extrair referências individuais
|
| 125 |
patterns = [
|
| 126 |
# Padrão 0 (novo): Referências numeradas multilinhas com autores múltiplos
|
| 127 |
-
|
| 128 |
|
| 129 |
# Padrão 1: Autor(es). (Ano). Título. Journal/Editora.
|
| 130 |
r'^([A-Z][A-Za-z\s,&.-]+?)\.\s*\((\d{4}[a-z]?)\)\.\s*([^.]+?)\.\s*([^.]+?)\.?\s*$',
|
|
@@ -144,10 +148,11 @@ def extract_references_with_regex(text):
|
|
| 144 |
# Padrão 6: Múltiplos autores com &
|
| 145 |
r'^([A-Z][A-Za-z\s,&.-]+?&[A-Za-z\s,&.-]+?)\.\s*\((\d{4}[a-z]?)\)\.\s*([^.]+?)\.\s*([^.]+?)\.?\s*$'
|
| 146 |
]
|
|
|
|
| 147 |
|
| 148 |
# Processar cada padrão
|
| 149 |
for pattern_index, pattern in enumerate(patterns):
|
| 150 |
-
reflist =
|
| 151 |
|
| 152 |
if reflist:
|
| 153 |
for ref_match in reflist:
|
|
@@ -171,11 +176,6 @@ def extract_references_with_regex(text):
|
|
| 171 |
volume = ""
|
| 172 |
|
| 173 |
|
| 174 |
-
|
| 175 |
-
# Verificar se não é uma linha de cabeçalho ou rodapé
|
| 176 |
-
if re.search(r'(page|vol|volume|number|issue)\s*\d+', journal, re.IGNORECASE):
|
| 177 |
-
continue
|
| 178 |
-
|
| 179 |
# Extrair DOI se presente
|
| 180 |
doi_match = re.search(r'doi[:\s]*([^\s,]+)', journal, re.IGNORECASE)
|
| 181 |
doi = doi_match.group(1) if doi_match else ""
|
|
@@ -402,7 +402,7 @@ def main():
|
|
| 402 |
load_dotenv() # Carrega variáveis de ambiente do arquivo .env
|
| 403 |
|
| 404 |
# Verificar se as chaves das APIs estão configuradas
|
| 405 |
-
google_key = os.getenv("
|
| 406 |
openai_key = os.getenv("OPENAI_API_KEY")
|
| 407 |
|
| 408 |
if not google_key and not openai_key:
|
|
|
|
| 2 |
import pymupdf # PyMuPDF
|
| 3 |
import pandas as pd
|
| 4 |
from pydantic_ai import Agent
|
| 5 |
+
from pydantic_ai.settings import ModelSettings
|
| 6 |
from pydantic import BaseModel
|
| 7 |
from typing import List, Optional
|
| 8 |
import google.generativeai as genai
|
|
|
|
| 60 |
# Determinar se é modelo Google ou OpenAI
|
| 61 |
if model_name.startswith('gemini'):
|
| 62 |
# Configurar a API key do Google
|
| 63 |
+
genai.configure(api_key=os.getenv("GEMINI_API_KEY"))
|
| 64 |
+
api_key = os.getenv("GEMINI_API_KEY")
|
| 65 |
else:
|
| 66 |
# Usar OpenAI
|
| 67 |
api_key = os.getenv("OPENAI_API_KEY")
|
|
|
|
| 72 |
# Criar o agente Pydantic AI
|
| 73 |
agent = Agent(
|
| 74 |
model_name,
|
| 75 |
+
model_settings=ModelSettings(
|
| 76 |
+
timeout=30,
|
| 77 |
+
),
|
| 78 |
result_type=ReferencesResponse,
|
| 79 |
system_prompt="""
|
| 80 |
Você é um especialista em análise de artigos científicos.
|
|
|
|
| 128 |
# Padrões melhorados para extrair referências individuais
|
| 129 |
patterns = [
|
| 130 |
# Padrão 0 (novo): Referências numeradas multilinhas com autores múltiplos
|
| 131 |
+
r'^\d+\.\s*([A-Z][A-Za-z\s,&.-]*?(?:Jr|Sr)?[,\s]*(?:[A-Z][A-Za-z\s,&.-]*?)*?):\s*([^.]+?)\.\s*([^.]+?)\s+(\d{4}),?\s*(\d+):(\d+[-–]\d+)\.',
|
| 132 |
|
| 133 |
# Padrão 1: Autor(es). (Ano). Título. Journal/Editora.
|
| 134 |
r'^([A-Z][A-Za-z\s,&.-]+?)\.\s*\((\d{4}[a-z]?)\)\.\s*([^.]+?)\.\s*([^.]+?)\.?\s*$',
|
|
|
|
| 148 |
# Padrão 6: Múltiplos autores com &
|
| 149 |
r'^([A-Z][A-Za-z\s,&.-]+?&[A-Za-z\s,&.-]+?)\.\s*\((\d{4}[a-z]?)\)\.\s*([^.]+?)\.\s*([^.]+?)\.?\s*$'
|
| 150 |
]
|
| 151 |
+
patterns = [re.compile(pat) for pat in patterns]
|
| 152 |
|
| 153 |
# Processar cada padrão
|
| 154 |
for pattern_index, pattern in enumerate(patterns):
|
| 155 |
+
reflist = pattern.findall(text, re.MULTILINE | re.UNICODE | re.DOTALL)
|
| 156 |
|
| 157 |
if reflist:
|
| 158 |
for ref_match in reflist:
|
|
|
|
| 176 |
volume = ""
|
| 177 |
|
| 178 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 179 |
# Extrair DOI se presente
|
| 180 |
doi_match = re.search(r'doi[:\s]*([^\s,]+)', journal, re.IGNORECASE)
|
| 181 |
doi = doi_match.group(1) if doi_match else ""
|
|
|
|
| 402 |
load_dotenv() # Carrega variáveis de ambiente do arquivo .env
|
| 403 |
|
| 404 |
# Verificar se as chaves das APIs estão configuradas
|
| 405 |
+
google_key = os.getenv("GEMINI_API_KEY")
|
| 406 |
openai_key = os.getenv("OPENAI_API_KEY")
|
| 407 |
|
| 408 |
if not google_key and not openai_key:
|