Annessha18 commited on
Commit
1ef4d7b
·
verified ·
1 Parent(s): 9a446a5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +66 -85
app.py CHANGED
@@ -2,78 +2,76 @@ import os
2
  import gradio as gr
3
  import requests
4
  import pandas as pd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
 
6
- API_URL = "https://agents-course-unit4-scoring.hf.space"
7
-
8
-
9
- # --------------------------
10
- # SMART RULE-BASED AGENT
11
- # --------------------------
12
- def agent_answer(question: str) -> str:
13
- q = question.lower()
14
-
15
- # 1️⃣ Mercedes Sosa
16
- if "mercedes sosa" in q and "studio albums" in q:
17
- return "4"
18
-
19
- # 2️⃣ 1928 Olympics – least athletes
20
- if "1928 summer olympics" in q and "least number of athletes" in q:
21
- return "AFG"
22
-
23
- # 3️⃣ Opposite of left
24
- if "opposite of left" in q:
25
- return "right"
26
-
27
- # 4️⃣ Malko Competition
28
- if "malko competition" in q and "first name" in q:
29
- return "Erik"
30
-
31
- # 5️⃣ Bird species in video
32
- if "bird species" in q:
33
- return "4"
34
-
35
- # 6️⃣ Chess move fallback
36
- if "chess" in q:
37
- return "Qh5"
38
-
39
- # 7️⃣ Excel sales question (safe numeric format)
40
- if "excel file" in q and "total sales" in q:
41
- return "1234.56"
42
-
43
- # 8️⃣ Pitcher question (safe format)
44
- if "pitcher" in q and "taishō tamai" in q:
45
- return "Suzuki, Tanaka"
46
 
47
- # ---- DEFAULT FALLBACK ----
48
- return "I don't know"
 
49
 
 
50
 
51
- # --------------------------
52
- # RUN + SUBMIT
53
- # --------------------------
54
- def run_and_submit(profile: gr.OAuthProfile | None):
55
  if not profile:
56
- return "❌ Please login to Hugging Face.", None
57
 
58
  username = profile.username.strip()
59
- space_id = os.getenv("SPACE_ID", "UNKNOWN")
60
- agent_code = f"https://huggingface.co/spaces/{space_id}"
61
 
62
  # Fetch questions
63
- try:
64
- questions = requests.get(f"{API_URL}/questions", timeout=20).json()
65
- except Exception as e:
66
- return f"❌ Error fetching questions: {e}", None
67
 
68
  answers = []
69
  logs = []
70
 
71
  for q in questions:
72
- ans = agent_answer(q["question"])
 
 
 
 
73
  answers.append({
74
  "task_id": q["task_id"],
75
  "submitted_answer": ans
76
  })
 
77
  logs.append({
78
  "Task ID": q["task_id"],
79
  "Question": q["question"],
@@ -86,46 +84,29 @@ def run_and_submit(profile: gr.OAuthProfile | None):
86
  "answers": answers
87
  }
88
 
89
- try:
90
- response = requests.post(
91
- f"{API_URL}/submit",
92
- json=payload,
93
- timeout=60
94
- )
95
- response.raise_for_status()
96
- result = response.json()
97
- except Exception as e:
98
- return f"❌ Submission failed: {e}", pd.DataFrame(logs)
99
 
100
  status = (
101
  f"✅ Submission Successful!\n"
102
- f"User: {result.get('username')}\n"
103
- f"Score: {result.get('score')}%\n"
104
- f"Correct: {result.get('correct_count')}/{result.get('total_attempted')}\n"
105
- f"Message: {result.get('message')}"
106
  )
107
 
108
  return status, pd.DataFrame(logs)
109
 
110
-
111
- # --------------------------
112
- # GRADIO UI
113
- # --------------------------
114
  with gr.Blocks() as demo:
115
- gr.Markdown("# 🤖 GAIA Level-1 Agent (Version 2)")
116
- gr.Markdown("Login → Run → Submit")
117
-
118
  gr.LoginButton()
119
- submit_btn = gr.Button("Run Evaluation & Submit")
120
 
121
- status_box = gr.Textbox(label="Submission Result", lines=6)
122
- log_table = gr.DataFrame(label="Agent Answers", wrap=True)
123
-
124
- submit_btn.click(
125
- fn=run_and_submit,
126
- outputs=[status_box, log_table]
127
- )
128
 
 
129
 
130
- if __name__ == "__main__":
131
- demo.launch()
 
2
  import gradio as gr
3
  import requests
4
  import pandas as pd
5
+ from openai import OpenAI
6
+
7
+ # =============================
8
+ # CONSTANTS (DO NOT CHANGE)
9
+ # =============================
10
+ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
11
+
12
+ SYSTEM_PROMPT = """
13
+ You are a general AI assistant.
14
+ Answer the question and finish your response with:
15
+
16
+ FINAL ANSWER: <answer>
17
+
18
+ Rules:
19
+ - If number: no commas, no units unless specified
20
+ - If string: no articles, no abbreviations
21
+ - If list: comma-separated, minimal words
22
+ """
23
+
24
+ # =============================
25
+ # MODEL
26
+ # =============================
27
+ client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
28
+
29
+ def llm_answer(question: str) -> str:
30
+ response = client.chat.completions.create(
31
+ model="gpt-4o-mini",
32
+ messages=[
33
+ {"role": "system", "content": SYSTEM_PROMPT},
34
+ {"role": "user", "content": question}
35
+ ],
36
+ temperature=0
37
+ )
38
 
39
+ text = response.choices[0].message.content
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40
 
41
+ # Extract FINAL ANSWER only
42
+ if "FINAL ANSWER:" in text:
43
+ return text.split("FINAL ANSWER:")[-1].strip()
44
 
45
+ return text.strip()
46
 
47
+ # =============================
48
+ # GAIA PIPELINE
49
+ # =============================
50
+ def run_and_submit_all(profile: gr.OAuthProfile | None):
51
  if not profile:
52
+ return "❌ Please login to Hugging Face", None
53
 
54
  username = profile.username.strip()
55
+ space_id = os.getenv("SPACE_ID")
56
+ agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
57
 
58
  # Fetch questions
59
+ questions = requests.get(f"{DEFAULT_API_URL}/questions").json()
 
 
 
60
 
61
  answers = []
62
  logs = []
63
 
64
  for q in questions:
65
+ try:
66
+ ans = llm_answer(q["question"])
67
+ except Exception as e:
68
+ ans = "I don't know"
69
+
70
  answers.append({
71
  "task_id": q["task_id"],
72
  "submitted_answer": ans
73
  })
74
+
75
  logs.append({
76
  "Task ID": q["task_id"],
77
  "Question": q["question"],
 
84
  "answers": answers
85
  }
86
 
87
+ res = requests.post(f"{DEFAULT_API_URL}/submit", json=payload).json()
 
 
 
 
 
 
 
 
 
88
 
89
  status = (
90
  f"✅ Submission Successful!\n"
91
+ f"User: {res.get('username')}\n"
92
+ f"Score: {res.get('score')}%\n"
93
+ f"Correct: {res.get('correct_count')}/{res.get('total_attempted')}\n"
94
+ f"Message: {res.get('message')}"
95
  )
96
 
97
  return status, pd.DataFrame(logs)
98
 
99
+ # =============================
100
+ # UI
101
+ # =============================
 
102
  with gr.Blocks() as demo:
103
+ gr.Markdown("# 🤖 GAIA Level-1 Agent (Unit-4)")
 
 
104
  gr.LoginButton()
 
105
 
106
+ run_btn = gr.Button("Run Evaluation & Submit")
107
+ status = gr.Textbox(label="Submission Result", lines=5)
108
+ table = gr.Dataframe(label="Questions & Answers")
 
 
 
 
109
 
110
+ run_btn.click(run_and_submit_all, outputs=[status, table])
111
 
112
+ demo.launch()