hasanain9 commited on
Commit
7b91124
Β·
verified Β·
1 Parent(s): b89a2ec
Files changed (1) hide show
  1. app.py +19 -19
app.py CHANGED
@@ -3,11 +3,11 @@ import gradio as gr
3
  import requests
4
  import openai
5
 
6
- # Setup OpenAI API Key dari Environment
7
  openai.api_key = os.getenv("OPENAI_API_KEY")
8
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
9
 
10
- # ----------- AGENT GPT -----------
11
  class GPTAgent:
12
  def __init__(self, model="gpt-4"):
13
  self.model = model
@@ -19,7 +19,7 @@ class GPTAgent:
19
  messages=[
20
  {
21
  "role": "system",
22
- "content": "You are a helpful assistant that answers only with one of the letters: A, B, C, or D.",
23
  },
24
  {"role": "user", "content": question},
25
  ],
@@ -30,30 +30,30 @@ class GPTAgent:
30
  except Exception as e:
31
  return f"ERROR: {str(e)}"
32
 
33
- # ----------- Main Logic -----------
34
- def run_and_submit_all(profile):
35
- username = getattr(profile, "username", "anonymous")
36
  space_id = os.getenv("SPACE_ID", "anonymous/Final_Assignment_Template")
37
 
38
  try:
39
  agent = GPTAgent()
40
  except Exception as e:
41
- return f"❌ Agent init error: {e}", ""
42
 
43
  try:
44
  questions = requests.get(f"{DEFAULT_API_URL}/questions", timeout=10).json()
45
  except Exception as e:
46
- return f"❌ Failed to fetch questions: {e}", ""
47
 
48
  answers = []
49
  logs = []
50
 
51
- for i, q in enumerate(questions, 1):
52
  task_id = q.get("task_id")
53
  question = q.get("question")
54
  answer = agent(question)
55
  answers.append({"task_id": task_id, "answer": answer})
56
- logs.append(f"### Q{i} - {task_id}\n{question}\n**Answer:** {answer}\n")
57
 
58
  try:
59
  result = requests.post(
@@ -75,16 +75,16 @@ def run_and_submit_all(profile):
75
 
76
  return summary, "\n\n".join(logs)
77
 
78
- # ------------- UI ---------------
79
  with gr.Blocks() as demo:
80
- gr.Markdown("# πŸ€– GAIA Evaluation Agent with GPT\nLogin with Hugging Face to evaluate your agent.")
81
 
82
- profile = gr.OAuthProfile()
83
- output_summary = gr.Textbox(label="πŸ“Š Result", lines=2)
84
- output_log = gr.Textbox(label="πŸ“‹ Answer Log", lines=20, show_copy_button=True)
 
 
85
 
86
- run_button = gr.Button("β–Ά Run All and Submit")
87
 
88
- run_button.click(fn=run_and_submit_all, inputs=[profile], outputs=[output_summary, output_log])
89
-
90
- demo.launch()
 
3
  import requests
4
  import openai
5
 
6
+ # Setup OpenAI API Key
7
  openai.api_key = os.getenv("OPENAI_API_KEY")
8
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
9
 
10
+ # Agent GPT
11
  class GPTAgent:
12
  def __init__(self, model="gpt-4"):
13
  self.model = model
 
19
  messages=[
20
  {
21
  "role": "system",
22
+ "content": "You are a helpful assistant that only replies with A, B, C, or D.",
23
  },
24
  {"role": "user", "content": question},
25
  ],
 
30
  except Exception as e:
31
  return f"ERROR: {str(e)}"
32
 
33
+ # Evaluation Function
34
+ def run_and_submit_all(user_profile):
35
+ username = getattr(user_profile, "username", "anonymous")
36
  space_id = os.getenv("SPACE_ID", "anonymous/Final_Assignment_Template")
37
 
38
  try:
39
  agent = GPTAgent()
40
  except Exception as e:
41
+ return f"❌ Agent error: {e}", ""
42
 
43
  try:
44
  questions = requests.get(f"{DEFAULT_API_URL}/questions", timeout=10).json()
45
  except Exception as e:
46
+ return f"❌ Could not fetch questions: {e}", ""
47
 
48
  answers = []
49
  logs = []
50
 
51
+ for idx, q in enumerate(questions, 1):
52
  task_id = q.get("task_id")
53
  question = q.get("question")
54
  answer = agent(question)
55
  answers.append({"task_id": task_id, "answer": answer})
56
+ logs.append(f"### Q{idx} - {task_id}\n{question}\n**Answer:** {answer}\n")
57
 
58
  try:
59
  result = requests.post(
 
75
 
76
  return summary, "\n\n".join(logs)
77
 
78
+ # Gradio UI
79
  with gr.Blocks() as demo:
80
+ gr.Markdown("# 🧠 GAIA Benchmark Agent\nLogin with your Hugging Face account to begin.")
81
 
82
+ login_btn = gr.LoginButton()
83
+ profile_output = gr.OAuthProfile()
84
+ run_button = gr.Button("β–Ά Run Evaluation")
85
+ summary = gr.Textbox(label="πŸ“Š Result", lines=2)
86
+ log_output = gr.Textbox(label="πŸ“‹ Log", lines=20, show_copy_button=True)
87
 
88
+ run_button.click(fn=run_and_submit_all, inputs=[profile_output], outputs=[summary, log_output])
89
 
90
+ demo.launch()