|
|
import faiss |
|
|
import gradio as gr |
|
|
from typing import Any, Generator, Iterator |
|
|
from sentence_transformers import SentenceTransformer |
|
|
from utils.llm_response import generate_response_with_llm |
|
|
from utils.phrase_extractor import process_file_content |
|
|
|
|
|
|
|
|
from .strings import STRINGS |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def process_phrases_with_rag_llm( |
|
|
input_phrases_text: str, rag_docs: list[str], rag_index: faiss.Index, rag_embedder: SentenceTransformer |
|
|
) -> Iterator[tuple[gr.Textbox, gr.Textbox, gr.Tabs, gr.TabItem]]: |
|
|
""" |
|
|
Receives a block of text (phrases separated by newlines) and processes it |
|
|
with the RAG+LLM API (`res_generate_API`) using a multiple-context strategy. |
|
|
Returns a status textbox, a formatted responses textbox, and updates tabs to switch to the results tab. |
|
|
""" |
|
|
print(f'Processando o bloco de frases para geração de resposta: "{input_phrases_text[:100]}..."') |
|
|
current_symbol = " ♾️" |
|
|
|
|
|
|
|
|
|
|
|
yield ( |
|
|
gr.update(value=STRINGS["TXTBOX_STATUS_IDLE"], interactive=False), |
|
|
gr.update(value="", interactive=False), |
|
|
gr.update(selected=1), |
|
|
gr.update(label=STRINGS["TAB_1_TITLE"] + current_symbol, interactive=True), |
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
try: |
|
|
|
|
|
|
|
|
|
|
|
llm_response = generate_response_with_llm( |
|
|
input_phrase=input_phrases_text, |
|
|
documents=rag_docs, |
|
|
index=rag_index, |
|
|
embedder=rag_embedder, |
|
|
llm_choice="gemini", |
|
|
rag_strategy="multiple", |
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
status_message = STRINGS["TXTBOX_STATUS_OK"] |
|
|
formatted_output = f"--- Resposta Fornecida pela LLM ---\n{llm_response}\n" |
|
|
current_symbol = " ✅" |
|
|
|
|
|
except Exception as e: |
|
|
status_message = STRINGS["TXTBOX_STATUS_ERROR"] |
|
|
formatted_output = f"\n{STRINGS['--- Erro ---']}\nDetalhes: {e}" |
|
|
current_symbol = " ⚠️" |
|
|
|
|
|
|
|
|
|
|
|
yield ( |
|
|
gr.update(value=status_message, interactive=False), |
|
|
gr.update(value=formatted_output, interactive=False), |
|
|
gr.update(), |
|
|
gr.update(label=STRINGS["TAB_1_TITLE"] + current_symbol, interactive=True), |
|
|
) |
|
|
|