| | import json |
| | import requests |
| | import PyPDF2 |
| | import gradio as gr |
| | from io import BytesIO |
| |
|
| | |
| | json_filename = "nexus_eter_pdf.json" |
| |
|
| | |
| | try: |
| | with open(json_filename, "r", encoding="utf-8") as file: |
| | nexus_eter_data = json.load(file) |
| | except FileNotFoundError: |
| | nexus_eter_data = {"error": "❌ Arquivo JSON não encontrado!"} |
| | except json.JSONDecodeError: |
| | nexus_eter_data = {"error": "❌ Erro ao carregar JSON!"} |
| |
|
| | |
| | def baixar_pdf(pdf_url): |
| | try: |
| | pdf_url = pdf_url.replace("blob/", "") |
| | response = requests.get(pdf_url) |
| | if response.status_code == 200: |
| | return BytesIO(response.content) |
| | else: |
| | return None |
| | except: |
| | return None |
| |
|
| | |
| | def extrair_texto_pdf(pdf_url): |
| | pdf_file = baixar_pdf(pdf_url) |
| | if pdf_file is None: |
| | return "" |
| |
|
| | pdf_reader = PyPDF2.PdfReader(pdf_file) |
| | texto_extraido = "" |
| |
|
| | for page in pdf_reader.pages: |
| | texto_extraido += page.extract_text() + "\n" |
| |
|
| | return texto_extraido |
| |
|
| | |
| | texto_total = "" |
| | for pdf in nexus_eter_data["Nexus_Eter"]["PDF_Fontes"]: |
| | texto_total += f"\n📜 **Fonte: {pdf['Nome']}**\n" |
| | texto_total += extrair_texto_pdf(pdf["Link"]) |
| |
|
| | |
| | def consultar_nexus_eter(pergunta): |
| | if not texto_total.strip(): |
| | return "Erro: Nenhum conhecimento foi carregado." |
| |
|
| | resultados = [linha for linha in texto_total.split("\n") if pergunta.lower() in linha.lower()] |
| |
|
| | if resultados: |
| | return "🔍 Nexus Éter encontrou:\n\n" + "\n".join(resultados[:10]) |
| | else: |
| | return "Nexus Éter responde: não encontrei essa resposta nos documentos." |
| |
|
| | |
| | interface = gr.Interface( |
| | fn=consultar_nexus_eter, |
| | inputs=gr.Textbox(label="Pergunte qualquer coisa ao Nexus Éter"), |
| | outputs="text", |
| | title="Consulta Nexus Éter Expandido", |
| | description="Nexus Éter agora busca respostas de múltiplos documentos hospedados." |
| | ) |
| |
|
| | interface.launch() |
| |
|