fredcaixeta commited on
Commit
d043f81
·
1 Parent(s): 8f90e63
Files changed (5) hide show
  1. .python-version +1 -0
  2. agent.py +1 -1
  3. app.py +34 -20
  4. main.py +6 -0
  5. pyproject.toml +7 -0
.python-version ADDED
@@ -0,0 +1 @@
 
 
1
+ 3.12
agent.py CHANGED
@@ -33,7 +33,7 @@ model = GroqModel(
33
  )
34
 
35
 
36
- def start_convo(user_input, messages_history):
37
  search_agent = Agent(
38
  model,
39
  system_prompt=DEFAULT_SYSTEM_PROMPT
 
33
  )
34
 
35
 
36
+ def 'start_convo(user_input, messages_history):
37
  search_agent = Agent(
38
  model,
39
  system_prompt=DEFAULT_SYSTEM_PROMPT
app.py CHANGED
@@ -2,45 +2,59 @@ import gradio as gr
2
  from ocr_script import ocr_tesseract_only
3
  import uuid
4
  from agent import start_convo
5
- # def ocr_tesseract_only(img): # img pode ser PIL/np/str conforme 'type'
6
- # # chamar seu OCR aqui e retornar texto
7
- # return "texto extraído"
8
-
9
- def respond(message, history, user_id):
10
- return "mock response"
11
- # pydantic_history = convert_to_pydantic_history(history)
12
- # tools_instance = SearchingTools()
13
- # deps = SearchAgentDeps(tools=tools_instance)
14
- # agent_config = start_convo(user_input=str(message), messages_history=pydantic_history)
15
- # result = agent_config.run_sync(
16
- # str(message),
17
- # deps=deps,
18
- # usage_limits=usage_limits,
19
- # message_history=pydantic_history,
20
- # )
 
 
 
 
 
 
 
21
  return result.output
22
 
23
  with gr.Blocks() as demo:
24
  with gr.Tabs():
25
  with gr.Tab("Text OCR Tesseract only"):
 
 
26
  with gr.Row():
27
  img_in = gr.Image(label="Imagem (png, jpg, jpeg)", type="pil")
28
  txt_out = gr.Textbox(label="Texto OCR", lines=12)
29
- img_in.change(fn=ocr_tesseract_only, inputs=img_in, outputs=txt_out)
 
 
 
 
 
30
 
31
  with gr.Tab("Chat"):
32
  user_id = gr.State(str(uuid.uuid4()))
33
  gr.ChatInterface(
34
  fn=respond,
35
- additional_inputs=[user_id],
36
  type="messages",
37
  title="Chat with AI Agent with Access to Extracted Data",
38
  description="Envie perguntas sobre os dados extraídos.",
39
  save_history=True,
40
- examples =[
41
  ["What is the name of the invoice document available?"],
42
  ["Which document has the ID aZwfUT2Zs?"]
43
- ],
44
  cache_examples=True,
45
  )
46
  demo.launch()
 
2
  from ocr_script import ocr_tesseract_only
3
  import uuid
4
  from agent import start_convo
5
+
6
+ import os
7
+ from dotenv import load_dotenv
8
+ from pydantic_ai import Agent, RunContext
9
+ from pydantic_ai.usage import UsageLimits
10
+ from pydantic_ai.models.groq import GroqModel
11
+
12
+ load_dotenv()
13
+
14
+ api_key = os.getenv("GROQ_API_KEY")
15
+
16
+ # Modelo Groq via Pydantic AI
17
+ model = GroqModel(model_name="openai/gpt-oss-120b")
18
+
19
+ def respond(message, history, user_id, ocr_text):
20
+ # Garantir que o system prompt seja o texto OCR atual
21
+ system_prompt_text = ocr_text or "Nenhum texto OCR disponível."
22
+ search_agent = Agent(model, system_prompt=system_prompt_text)
23
+
24
+ # Se usar seu start_convo, injete o mesmo prompt no agente interno, ou remova se for redundante
25
+ # agent_config = start_convo(user_input=str(message), messages_history=history, system_prompt=system_prompt_text)
26
+
27
+ result = search_agent.run_sync(str(message))
28
  return result.output
29
 
30
  with gr.Blocks() as demo:
31
  with gr.Tabs():
32
  with gr.Tab("Text OCR Tesseract only"):
33
+ ocr_state = gr.State("") # Armazena o texto OCR para uso no chat
34
+
35
  with gr.Row():
36
  img_in = gr.Image(label="Imagem (png, jpg, jpeg)", type="pil")
37
  txt_out = gr.Textbox(label="Texto OCR", lines=12)
38
+
39
+ def run_ocr(img):
40
+ text = ocr_tesseract_only(img)
41
+ return text, text
42
+
43
+ img_in.change(fn=run_ocr, inputs=img_in, outputs=[txt_out, ocr_state])
44
 
45
  with gr.Tab("Chat"):
46
  user_id = gr.State(str(uuid.uuid4()))
47
  gr.ChatInterface(
48
  fn=respond,
49
+ additional_inputs=[user_id, ocr_state], # injeta o texto OCR no fn
50
  type="messages",
51
  title="Chat with AI Agent with Access to Extracted Data",
52
  description="Envie perguntas sobre os dados extraídos.",
53
  save_history=True,
54
+ examples=[
55
  ["What is the name of the invoice document available?"],
56
  ["Which document has the ID aZwfUT2Zs?"]
57
+ ],
58
  cache_examples=True,
59
  )
60
  demo.launch()
main.py ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ def main():
2
+ print("Hello from extractor!")
3
+
4
+
5
+ if __name__ == "__main__":
6
+ main()
pyproject.toml ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ [project]
2
+ name = "extractor"
3
+ version = "0.1.0"
4
+ description = "Add your description here"
5
+ readme = "README.md"
6
+ requires-python = ">=3.12"
7
+ dependencies = []