GilbertoEwaldFilho commited on
Commit
46ca44e
·
verified ·
1 Parent(s): 12537dd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -24
app.py CHANGED
@@ -1,13 +1,11 @@
1
  import os
2
- import gradio as gr
3
  import requests
4
  import pandas as pd
5
- import re
6
 
7
  from huggingface_hub import InferenceClient
8
 
9
- from smolagents import CodeAgent, DuckDuckGoSearchTool, InferenceClientModel
10
-
11
  # --- Constants ---
12
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
13
 
@@ -72,24 +70,22 @@ SYSTEM_PROMPT = (
72
 
73
  class BasicAgent:
74
  """
75
- Agente simples que usa diretamente o InferenceClient do Hugging Face
76
- para responder as questões do GAIA.
77
-
78
- Não usa ferramentas externas (search), mas é estável no Space.
79
  """
80
 
81
  def __init__(self):
82
- print("Initializing Simple GAIA Agent with InferenceClient...")
83
 
84
  hf_token = os.getenv("HF_TOKEN")
85
  if not hf_token:
86
  raise ValueError(
87
- "HF_TOKEN not found! Configure it como Secret em Settings → Variables."
88
  )
89
 
90
- # 🔹 Modelo que suporta text-generation via Inference API
91
  self.client = InferenceClient(
92
- model="mistralai/Mixtral-8x7B-Instruct-v0.1",
93
  token=hf_token,
94
  )
95
 
@@ -106,28 +102,40 @@ class BasicAgent:
106
  def __call__(self, question: str) -> str:
107
  print(f"\n=== NEW QUESTION ===\n{question}\n")
108
 
109
- prompt = (
110
- self.system_instructions
111
- + "\nQuestion:\n"
112
- + question
113
- + "\n\nAnswer (remember: ONLY the final answer):"
114
- )
 
 
 
 
115
 
116
  try:
117
- # usamos text_generation para evitar problemas com chat_completion
118
- raw = self.client.text_generation(
119
- prompt,
120
- max_new_tokens=64,
121
  temperature=0.1,
122
  top_p=0.9,
123
- stop_sequences=["\n"], # para não vir um parágrafo gigante
124
  )
 
 
 
 
 
 
 
 
 
125
  print("RAW MODEL OUTPUT:", repr(raw))
126
  final = clean_answer(raw)
127
  print("CLEANED ANSWER:", repr(final))
128
  return final
 
129
  except Exception as e:
130
- print("ERROR calling InferenceClient:", e)
131
  return ""
132
 
133
  # =========================================================
 
1
  import os
2
+ import re
3
  import requests
4
  import pandas as pd
5
+ import gradio as gr
6
 
7
  from huggingface_hub import InferenceClient
8
 
 
 
9
  # --- Constants ---
10
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
11
 
 
70
 
71
  class BasicAgent:
72
  """
73
+ Agente simples usando InferenceClient.chat_completion
74
+ para responder as questões do GAIA em modo conversacional.
 
 
75
  """
76
 
77
  def __init__(self):
78
+ print("Initializing Simple GAIA Agent with chat_completion...")
79
 
80
  hf_token = os.getenv("HF_TOKEN")
81
  if not hf_token:
82
  raise ValueError(
83
+ "HF_TOKEN not found! Crie um Secret chamado HF_TOKEN em Settings → Variables."
84
  )
85
 
86
+ # Modelo que sabemos ser suportado como 'conversational'
87
  self.client = InferenceClient(
88
+ model="Qwen/Qwen2.5-72B-Instruct", # o mesmo que a infra mostrou no log
89
  token=hf_token,
90
  )
91
 
 
102
  def __call__(self, question: str) -> str:
103
  print(f"\n=== NEW QUESTION ===\n{question}\n")
104
 
105
+ messages = [
106
+ {"role": "system", "content": self.system_instructions},
107
+ {
108
+ "role": "user",
109
+ "content": (
110
+ question
111
+ + "\n\nRemember: reply ONLY with the final answer, nothing else."
112
+ ),
113
+ },
114
+ ]
115
 
116
  try:
117
+ completion = self.client.chat_completion(
118
+ messages=messages,
119
+ max_tokens=64,
 
120
  temperature=0.1,
121
  top_p=0.9,
 
122
  )
123
+
124
+ # compatível com os dois formatos (.message["content"] ou .message.content)
125
+ choice = completion.choices[0]
126
+ message = choice.message
127
+ if isinstance(message, dict):
128
+ raw = message.get("content", "")
129
+ else:
130
+ raw = getattr(message, "content", "")
131
+
132
  print("RAW MODEL OUTPUT:", repr(raw))
133
  final = clean_answer(raw)
134
  print("CLEANED ANSWER:", repr(final))
135
  return final
136
+
137
  except Exception as e:
138
+ print("ERROR calling InferenceClient.chat_completion:", e)
139
  return ""
140
 
141
  # =========================================================