KVMKASH commited on
Commit
4bdd0ed
·
verified ·
1 Parent(s): a999c9e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -60
app.py CHANGED
@@ -7,56 +7,54 @@ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
7
 
8
  class BasicAgent:
9
  def __init__(self):
10
- self.api_url = "https://router.huggingface.co/v1/chat/completions"
11
-
12
  hf_token = os.getenv("HF_TOKEN")
13
 
14
  if not hf_token:
15
  raise ValueError("HF_TOKEN is missing")
16
 
17
  self.headers = {
18
- "Authorization": f"Bearer {hf_token}",
19
- "Content-Type": "application/json"
20
  }
21
 
22
- self.model = "meta-llama/Llama-3.1-8B-Instruct"
 
 
 
23
 
24
  print("BasicAgent initialized.")
25
 
26
  def __call__(self, question: str) -> str:
27
  try:
28
  payload = {
29
- "model": self.model,
30
- "messages": [
31
- {
32
- "role": "system",
33
- "content": (
34
- "You are a GAIA benchmark solving agent. "
35
- "Solve carefully and return only the exact final answer. "
36
- "Do not explain anything. "
37
- "Do not add extra text."
38
- )
39
- },
40
- {
41
- "role": "user",
42
- "content": question
43
- }
44
- ],
45
- "temperature": 0
46
  }
47
 
48
  response = requests.post(
49
- self.api_url,
50
  headers=self.headers,
51
  json=payload,
52
- timeout=60
53
  )
54
 
55
  response.raise_for_status()
56
 
57
  result = response.json()
58
 
59
- answer = result["choices"][0]["message"]["content"].strip()
 
 
 
 
 
60
 
61
  print(f"Answer: {answer}")
62
 
@@ -71,13 +69,11 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
71
 
72
  if profile:
73
  username = profile.username
74
- print(f"User logged in: {username}")
75
  else:
76
- return "Please Login to Hugging Face with the button.", None
77
 
78
- api_url = DEFAULT_API_URL
79
- questions_url = f"{api_url}/questions"
80
- submit_url = f"{api_url}/submit"
81
 
82
  try:
83
  agent = BasicAgent()
@@ -106,35 +102,22 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
106
  if not task_id or not question_text:
107
  continue
108
 
109
- try:
110
- submitted_answer = agent(question_text)
111
-
112
- answers_payload.append(
113
- {
114
- "task_id": task_id,
115
- "submitted_answer": submitted_answer
116
- }
117
- )
118
-
119
- results_log.append(
120
- {
121
- "Task ID": task_id,
122
- "Question": question_text,
123
- "Submitted Answer": submitted_answer
124
- }
125
- )
126
 
127
- except Exception as e:
128
- results_log.append(
129
- {
130
- "Task ID": task_id,
131
- "Question": question_text,
132
- "Submitted Answer": f"ERROR: {e}"
133
- }
134
- )
135
 
136
- if not answers_payload:
137
- return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
 
 
 
 
 
138
 
139
  submission_data = {
140
  "username": username.strip(),
@@ -175,7 +158,7 @@ with gr.Blocks() as demo:
175
 
176
  gr.LoginButton()
177
 
178
- run_button = gr.Button("Run Evaluation & Submit All Answers")
179
 
180
  status_output = gr.Textbox(
181
  label="Submission Result",
@@ -184,7 +167,7 @@ with gr.Blocks() as demo:
184
  )
185
 
186
  results_table = gr.DataFrame(
187
- label="Questions and Agent Answers",
188
  wrap=True
189
  )
190
 
@@ -194,5 +177,5 @@ with gr.Blocks() as demo:
194
  )
195
 
196
  if __name__ == "__main__":
197
- demo.launch(debug=True, share=False)
198
 
 
7
 
8
  class BasicAgent:
9
  def __init__(self):
 
 
10
  hf_token = os.getenv("HF_TOKEN")
11
 
12
  if not hf_token:
13
  raise ValueError("HF_TOKEN is missing")
14
 
15
  self.headers = {
16
+ "Authorization": f"Bearer {hf_token}"
 
17
  }
18
 
19
+ self.model_url = (
20
+ "https://api-inference.huggingface.co/models/"
21
+ "meta-llama/Llama-3.1-8B-Instruct"
22
+ )
23
 
24
  print("BasicAgent initialized.")
25
 
26
  def __call__(self, question: str) -> str:
27
  try:
28
  payload = {
29
+ "inputs": (
30
+ "You are a GAIA solving agent.\n"
31
+ "Return only the exact final answer.\n"
32
+ "Do not explain anything.\n\n"
33
+ f"Question: {question}"
34
+ ),
35
+ "parameters": {
36
+ "max_new_tokens": 128,
37
+ "temperature": 0
38
+ }
 
 
 
 
 
 
 
39
  }
40
 
41
  response = requests.post(
42
+ self.model_url,
43
  headers=self.headers,
44
  json=payload,
45
+ timeout=120
46
  )
47
 
48
  response.raise_for_status()
49
 
50
  result = response.json()
51
 
52
+ if isinstance(result, list):
53
+ answer = result[0]["generated_text"]
54
+ else:
55
+ answer = str(result)
56
+
57
+ answer = answer.strip()
58
 
59
  print(f"Answer: {answer}")
60
 
 
69
 
70
  if profile:
71
  username = profile.username
 
72
  else:
73
+ return "Please Login to Hugging Face.", None
74
 
75
+ questions_url = f"{DEFAULT_API_URL}/questions"
76
+ submit_url = f"{DEFAULT_API_URL}/submit"
 
77
 
78
  try:
79
  agent = BasicAgent()
 
102
  if not task_id or not question_text:
103
  continue
104
 
105
+ submitted_answer = agent(question_text)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
106
 
107
+ answers_payload.append(
108
+ {
109
+ "task_id": task_id,
110
+ "submitted_answer": submitted_answer
111
+ }
112
+ )
 
 
113
 
114
+ results_log.append(
115
+ {
116
+ "Task ID": task_id,
117
+ "Question": question_text,
118
+ "Submitted Answer": submitted_answer
119
+ }
120
+ )
121
 
122
  submission_data = {
123
  "username": username.strip(),
 
158
 
159
  gr.LoginButton()
160
 
161
+ run_button = gr.Button("Run Evaluation & Submit")
162
 
163
  status_output = gr.Textbox(
164
  label="Submission Result",
 
167
  )
168
 
169
  results_table = gr.DataFrame(
170
+ label="Results",
171
  wrap=True
172
  )
173
 
 
177
  )
178
 
179
  if __name__ == "__main__":
180
+ demo.launch(debug=True)
181