File size: 1,928 Bytes
d2d8ce0
09381b1
b5614b9
d043f81
 
 
 
 
 
 
 
 
 
 
 
7efb907
d043f81
 
 
 
 
 
 
7bbd250
 
84a0fc4
7bbd250
 
d043f81
 
b83e38c
a5edea0
7bbd250
d043f81
 
 
 
 
 
7bbd250
b83e38c
 
 
 
d043f81
b83e38c
 
 
 
d043f81
c91aeff
 
d043f81
b83e38c
 
9a56428
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import gradio as gr
from ocr_script import ocr_tesseract_only
import uuid

import os
from dotenv import load_dotenv
from pydantic_ai import Agent, RunContext
from pydantic_ai.usage import UsageLimits
from pydantic_ai.models.groq import GroqModel

load_dotenv()

api_key = os.getenv("GROQ_API_KEY")

# Modelo Groq via Pydantic AI
model = GroqModel(model_name="openai/gpt-oss-20b")

def respond(message, history, user_id, ocr_text):
    # Garantir que o system prompt seja o texto OCR atual
    system_prompt_text = ocr_text or "Nenhum texto OCR disponível."
    search_agent = Agent(model, system_prompt=system_prompt_text)

    result = search_agent.run_sync(str(message))
    return result.output

with gr.Blocks() as demo:
    with gr.Tabs():
        with gr.Tab("Text OCR Tesseract only"):
            ocr_state = gr.State("")  # Armazena o texto OCR para uso no chat

            with gr.Row():
                img_in = gr.Image(label="Imagem (png, jpg, jpeg)", type="pil")
                txt_out = gr.Textbox(label="Texto OCR", lines=12)

            def run_ocr(img):
                text = ocr_tesseract_only(img)
                return text, text

            img_in.change(fn=run_ocr, inputs=img_in, outputs=[txt_out, ocr_state])

        with gr.Tab("Chat"):
            user_id = gr.State(str(uuid.uuid4()))
            gr.ChatInterface(
                fn=respond,
                additional_inputs=[user_id, ocr_state],  # injeta o texto OCR no fn
                type="messages",
                title="Chat with AI Agent with Access to Extracted Data",
                description="Envie perguntas sobre os dados extraídos.",
                save_history=True,
                examples=[
                    ["What is the name of the invoice document available?"],
                    ["Which document has the ID aZwfUT2Zs?"]
                ],
                cache_examples=True,
            )
demo.launch()