MF180 commited on
Commit
2f6fa5e
·
verified ·
1 Parent(s): 8a58eda

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -9
app.py CHANGED
@@ -4,23 +4,51 @@ import requests
4
  import inspect
5
  import pandas as pd
6
  import spaces
 
7
 
8
  # (Keep Constants as is)
9
  # --- Constants ---
10
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
11
 
12
  # --- Basic Agent Definition ---
13
- # ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
14
  class BasicAgent:
15
  def __init__(self):
16
  print("BasicAgent initialized.")
 
 
 
 
 
 
17
  def __call__(self, question: str) -> str:
18
  print(f"Agent received question (first 50 chars): {question[:50]}...")
19
- fixed_answer = "This is a default answer."
20
- print(f"Agent returning fixed answer: {fixed_answer}")
21
- return fixed_answer
22
 
23
- @spaces.GPU
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24
  def run_and_submit_all( profile: gr.OAuthProfile | None):
25
  """
26
  Fetches all questions, runs the BasicAgent on them, submits all answers,
@@ -163,7 +191,6 @@ with gr.Blocks() as demo:
163
  run_button = gr.Button("Run Evaluation & Submit All Answers")
164
 
165
  status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
166
- # Removed max_rows=10 from DataFrame constructor
167
  results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
168
 
169
  run_button.click(
@@ -173,9 +200,8 @@ with gr.Blocks() as demo:
173
 
174
  if __name__ == "__main__":
175
  print("\n" + "-"*30 + " App Starting " + "-"*30)
176
- # Check for SPACE_HOST and SPACE_ID at startup for information
177
  space_host_startup = os.getenv("SPACE_HOST")
178
- space_id_startup = os.getenv("SPACE_ID") # Get SPACE_ID at startup
179
 
180
  if space_host_startup:
181
  print(f"✅ SPACE_HOST found: {space_host_startup}")
@@ -183,7 +209,7 @@ if __name__ == "__main__":
183
  else:
184
  print("ℹ️ SPACE_HOST environment variable not found (running locally?).")
185
 
186
- if space_id_startup: # Print repo URLs if SPACE_ID is found
187
  print(f"✅ SPACE_ID found: {space_id_startup}")
188
  print(f" Repo URL: https://huggingface.co/spaces/{space_id_startup}")
189
  print(f" Repo Tree URL: https://huggingface.co/spaces/{space_id_startup}/tree/main")
 
4
  import inspect
5
  import pandas as pd
6
  import spaces
7
+ from huggingface_hub import InferenceClient
8
 
9
  # (Keep Constants as is)
10
  # --- Constants ---
11
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
12
 
13
  # --- Basic Agent Definition ---
 
14
  class BasicAgent:
15
  def __init__(self):
16
  print("BasicAgent initialized.")
17
+ # Uses your HF_TOKEN secret to call a free-tier hosted model
18
+ self.client = InferenceClient(
19
+ model="Qwen/Qwen2.5-72B-Instruct",
20
+ token=os.environ.get("HF_TOKEN")
21
+ )
22
+
23
  def __call__(self, question: str) -> str:
24
  print(f"Agent received question (first 50 chars): {question[:50]}...")
 
 
 
25
 
26
+ system_prompt = (
27
+ "You are a general AI assistant answering benchmark questions. "
28
+ "Report your final answer as a concise string: a number, a short phrase, "
29
+ "or a comma separated list, with no explanation, no extra words, "
30
+ "no units unless explicitly asked for, and no leading phrases like 'The answer is'. "
31
+ "Answer with ONLY the final answer, nothing else."
32
+ )
33
+
34
+ try:
35
+ response = self.client.chat_completion(
36
+ messages=[
37
+ {"role": "system", "content": system_prompt},
38
+ {"role": "user", "content": question},
39
+ ],
40
+ max_tokens=512,
41
+ temperature=0,
42
+ )
43
+ answer = response.choices[0].message.content.strip()
44
+ except Exception as e:
45
+ print(f"Error calling LLM: {e}")
46
+ answer = "Error"
47
+
48
+ print(f"Agent returning answer: {answer}")
49
+ return answer
50
+
51
+
52
  def run_and_submit_all( profile: gr.OAuthProfile | None):
53
  """
54
  Fetches all questions, runs the BasicAgent on them, submits all answers,
 
191
  run_button = gr.Button("Run Evaluation & Submit All Answers")
192
 
193
  status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
 
194
  results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
195
 
196
  run_button.click(
 
200
 
201
  if __name__ == "__main__":
202
  print("\n" + "-"*30 + " App Starting " + "-"*30)
 
203
  space_host_startup = os.getenv("SPACE_HOST")
204
+ space_id_startup = os.getenv("SPACE_ID")
205
 
206
  if space_host_startup:
207
  print(f"✅ SPACE_HOST found: {space_host_startup}")
 
209
  else:
210
  print("ℹ️ SPACE_HOST environment variable not found (running locally?).")
211
 
212
+ if space_id_startup:
213
  print(f"✅ SPACE_ID found: {space_id_startup}")
214
  print(f" Repo URL: https://huggingface.co/spaces/{space_id_startup}")
215
  print(f" Repo Tree URL: https://huggingface.co/spaces/{space_id_startup}/tree/main")