{ "cells": [ { "cell_type": "code", "execution_count": null, "id": "d2f58b84", "metadata": {}, "outputs": [], "source": [ "# LLM Quest — Interface de Reformulação Epistêmica\n", "# Execute esta célula e a seguinte. A interface abre logo abaixo." ] }, { "cell_type": "code", "execution_count": 3, "id": "d83415b3", "metadata": {}, "outputs": [], "source": [ "import os, sys, warnings\n", "warnings.filterwarnings('ignore')\n", "\n", "project_root = os.path.abspath('')\n", "if project_root not in sys.path:\n", " sys.path.insert(0, project_root)\n", "\n", "from dotenv import load_dotenv\n", "load_dotenv(override=True)\n", "\n", "os.environ['INFERENCE_BACKEND'] = 'claude'" ] }, { "cell_type": "code", "execution_count": 4, "id": "afa0dc7a", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" }, { "name": "stdout", "output_type": "stream", "text": [ "Indice BM25 carregado do cache.\n", " [1/3] Traduzindo entrada (pt -> en)...\n", " -> What is the impact of ancestry?\n", " [2/3] Reformulando (pipeline EE)...\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Warning: You are sending unauthenticated requests to the HF Hub. Please set a HF_TOKEN to enable higher rate limits and faster downloads.\n" ] } ], "source": [ "import gradio as gr\n", "\n", "_initialized = False\n", "\n", "def _init():\n", " global _initialized\n", " if _initialized:\n", " return\n", " from src.rl.inference import _get_index\n", " _get_index()\n", " _initialized = True\n", "\n", "\n", "def reformular_ui(pergunta, idioma):\n", " if not pergunta.strip():\n", " return '

Digite uma pergunta.

'\n", "\n", " _init()\n", "\n", " from src.rl.inference import reformular, reformular_ptbr\n", " ptbr = (idioma == 'Português')\n", "\n", " try:\n", " r = reformular_ptbr(pergunta, n=8) if ptbr else reformular(pergunta, n=8)\n", " except Exception as e:\n", " return f'

Erro: {e}

'\n", "\n", " if ptbr:\n", " entrada, saida = r.q_bad_pt, r.best_pt\n", " sub_in = f'
🔤 {r.q_bad_en}
'\n", " sub_out = f'
🔤 {r.best_en}
'\n", " cands, ee_bad, ee_best, score, passed = r.candidates, r.ee_bad, r.ee_best, r.score_best, r.stage1_pass\n", " else:\n", " entrada, saida = r.q_bad, r.best\n", " sub_in = sub_out = ''\n", " cands, ee_bad, ee_best, score, passed = r.candidates, r.ee_bad, r.ee_best, r.score_best, r.stage1_pass\n", "\n", " delta = ee_best - ee_bad\n", " ganho_pct = delta / max(ee_bad, 0.001) * 100\n", " filtro = '✅ PASS' if passed else '⚠️ Fallback'\n", " fc = '#27ae60' if passed else '#e67e22'\n", "\n", " def bar(v):\n", " pct = min(int(v * 100), 100)\n", " c = '#2ecc71' if pct >= 70 else '#f39c12' if pct >= 40 else '#e74c3c'\n", " return (f'
'\n", " f'
'\n", " f'
'\n", " f'{v:.3f}
')\n", "\n", " top = sorted(cands, key=lambda c: c['score'], reverse=True)\n", " rows = ''.join(\n", " f''\n", " f'{\"🟢\" if c[\"ee\"]>ee_bad+0.05 else \"🔴\"} {i}'\n", " f'{c[\"ee\"]:.3f}'\n", " f'{c[\"text\"]}'\n", " for i, c in enumerate(top, 1)\n", " )\n", "\n", " return f\"\"\"\n", "
\n", "
\n", "
Pergunta original
\n", "
{entrada}
{sub_in}\n", "
\n", "
\n", "
Reformulação epistêmica
\n", "
{saida}
{sub_out}\n", "
\n", "
\n", "
EE ANTES
{bar(ee_bad)}
\n", "
EE DEPOIS
{bar(ee_best)}
\n", "
GANHO
\n", " +{delta:.3f} ({ganho_pct:.0f}%)
\n", "
FILTRO
\n", " {filtro}
\n", "
\n", "
\n", " ▶ Ver {len(cands)} candidatos\n", " \n", " \n", " \n", " \n", " \n", " \n", " {rows}\n", "
#EEReformulação
\n", "
\n", "
\n", "\"\"\"\n", "\n", "\n", "with gr.Blocks(title='LLM Quest', theme=gr.themes.Soft()) as app:\n", "\n", " gr.Markdown(\"## 🔬 LLM Quest — Reformulação Epistêmica\")\n", "\n", " with gr.Row():\n", " with gr.Column(scale=3):\n", " inp_q = gr.Textbox(\n", " label='Pergunta de pesquisa',\n", " placeholder='Ex: O que causa o envelhecimento biológico?',\n", " lines=3\n", " )\n", " with gr.Column(scale=1):\n", " inp_idioma = gr.Radio(\n", " choices=['Português', 'English'],\n", " value='Português',\n", " label='Idioma'\n", " )\n", "\n", " btn = gr.Button('🔄 Reformular', variant='primary', size='lg')\n", " out = gr.HTML()\n", "\n", " btn.click(fn=reformular_ui, inputs=[inp_q, inp_idioma], outputs=out)\n", "\n", " gr.Examples(\n", " examples=[\n", " ['O que causa o envelhecimento biológico?', 'Português'],\n", " ['O livre-arbítrio existe?', 'Português'],\n", " ['Qual é a natureza da consciência?', 'Português'],\n", " ['What is the nature of dark matter?', 'English' ],\n", " ['Does consciousness arise from physical processes?', 'English' ],\n", " ],\n", " inputs=[inp_q, inp_idioma],\n", " label='Exemplos'\n", " )\n", "\n", "app.launch(inline=True, share=False, quiet=True)" ] }, { "cell_type": "code", "execution_count": null, "id": "ac60bae8-d704-44e0-b6bb-82c5b9271518", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "LLM Quest (.venv)", "language": "python", "name": "llm_quest_venv" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.11.9" } }, "nbformat": 4, "nbformat_minor": 5 }