PradeepBodhi commited on
Commit
28ced33
·
verified ·
1 Parent(s): ef9304f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +64 -59
app.py CHANGED
@@ -1,71 +1,71 @@
1
  import os
2
  import gradio as gr
3
  import requests
4
- import time
5
  import pandas as pd
6
- from dotenv import load_dotenv
7
-
8
- # Load environment variables
9
- load_dotenv()
10
 
11
  # --- Constants ---
12
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
13
- HF_API_URL = "https://api-inference.huggingface.co/models/mistralai/Mistral-7B-Instruct-v0.2"
14
- HUGGINGFACE_TOKEN = os.getenv("HUGGINGFACE_TOKEN")
15
 
16
- # --- Agent Definition ---
17
  class BasicAgent:
18
  def __init__(self):
19
- self.headers = {"Authorization": f"Bearer {HUGGINGFACE_TOKEN}"}
 
 
 
 
 
 
20
 
21
  def __call__(self, question: str) -> str:
22
- prompt = f"""Answer the question concisely and factually. Provide only the answer in the exact format required by the question, with no additional text, explanations, or formatting. Do not use quotation marks, bullet points, or any other formatting. Ensure correct casing and pluralization as specified.
23
-
24
- Question: {question}
25
- Answer: """
26
- payload = {
27
- "inputs": prompt,
28
- "parameters": {
29
- "max_new_tokens": 100,
30
- "return_full_text": False
31
- }
32
- }
33
- max_retries = 3
34
- for _ in range(max_retries):
35
- try:
36
- response = requests.post(HF_API_URL, headers=self.headers, json=payload, timeout=30)
37
- response.raise_for_status()
38
- answer = response.json()[0]['generated_text'].strip()
39
- # Clean up answer
40
- answer = answer.strip().rstrip('.').strip()
41
- return answer
42
- except requests.exceptions.HTTPError as e:
43
- if e.response.status_code == 503:
44
- time.sleep(10)
45
- else:
46
- print(f"HTTP Error: {e.response.text}")
47
- break
48
- except requests.exceptions.RequestException as e:
49
- print(f"Request failed: {e}, retrying...")
50
- time.sleep(5)
51
- except Exception as e:
52
- print(f"Error: {e}")
53
- break
54
- return "Error: Unable to generate answer."
55
 
56
- # --- Submission Logic ---
57
  def run_and_submit_all(profile: gr.OAuthProfile | None):
58
  if profile:
59
  username = f"{profile.username}"
 
60
  else:
 
61
  return "Please Login to Hugging Face with the button.", None
62
 
63
- agent = BasicAgent()
 
 
 
 
 
 
 
 
64
  space_id = os.getenv("SPACE_ID")
65
  agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main" if space_id else ""
66
 
67
  try:
68
- response = requests.get(f"{DEFAULT_API_URL}/questions", timeout=15)
69
  response.raise_for_status()
70
  questions_data = response.json()
71
  except Exception as e:
@@ -76,7 +76,7 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
76
  for item in questions_data:
77
  task_id = item.get("task_id")
78
  question_text = item.get("question")
79
- if not task_id or not question_text:
80
  continue
81
  try:
82
  submitted_answer = agent(question_text)
@@ -92,27 +92,32 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
92
  }
93
 
94
  try:
95
- response = requests.post(f"{DEFAULT_API_URL}/submit", json=submission_data, timeout=60)
96
  response.raise_for_status()
97
  result_data = response.json()
98
- final_status = f"Success! Score: {result_data.get('score', 'N/A')}%"
 
 
 
 
99
  return final_status, pd.DataFrame(results_log)
100
  except Exception as e:
101
  return f"Submission failed: {e}", pd.DataFrame(results_log)
102
 
103
- # --- Gradio Interface ---
104
  with gr.Blocks() as demo:
105
- gr.Markdown("# GAIA Evaluation Agent")
106
- gr.Markdown("""
107
- **Instructions:**
108
- 1. Log in to your Hugging Face account using the button below.
109
- 2. Click 'Run Evaluation & Submit Answers' to fetch questions, run your agent, and submit answers.
110
- """)
111
  gr.LoginButton()
112
- run_button = gr.Button("Run Evaluation & Submit Answers")
113
- status_output = gr.Textbox(label="Status")
114
- results_table = gr.DataFrame(label="Results")
115
- run_button.click(run_and_submit_all, outputs=[status_output, results_table])
 
 
 
 
116
 
117
  if __name__ == "__main__":
118
  demo.launch()
 
1
  import os
2
  import gradio as gr
3
  import requests
 
4
  import pandas as pd
5
+ from huggingface_hub import InferenceClient
 
 
 
6
 
7
  # --- Constants ---
8
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
9
+ MODEL_NAME = "mistralai/Mixtral-8x7B-Instruct-v0.1" # Free inference endpoint
 
10
 
11
+ # --- Enhanced BasicAgent ---
12
  class BasicAgent:
13
  def __init__(self):
14
+ self.client = InferenceClient(model=MODEL_NAME)
15
+ self.system_prompt = """You are a GAIA question answering agent. Follow these rules:
16
+ 1. Answer EXACTLY as required - no extra text
17
+ 2. Use correct pluralization and ordering
18
+ 3. Never explain your answer
19
+ 4. Format lists as comma-separated values
20
+ 5. Use only facts from verified sources"""
21
 
22
  def __call__(self, question: str) -> str:
23
+ try:
24
+ response = self.client.chat(
25
+ model=MODEL_NAME,
26
+ messages=[{
27
+ "role": "system",
28
+ "content": self.system_prompt
29
+ },{
30
+ "role": "user",
31
+ "content": question
32
+ }],
33
+ max_tokens=100,
34
+ stop_sequences=["\n"]
35
+ )
36
+
37
+ # Clean and format response
38
+ answer = response.content.split("Answer:")[-1].strip()
39
+ answer = answer.replace('"', '').replace('.', '').strip()
40
+ return answer
41
+
42
+ except Exception as e:
43
+ print(f"Error in agent: {str(e)}")
44
+ return "Error generating answer"
 
 
 
 
 
 
 
 
 
 
 
45
 
46
+ # --- Keep Original Submission Logic Intact ---
47
  def run_and_submit_all(profile: gr.OAuthProfile | None):
48
  if profile:
49
  username = f"{profile.username}"
50
+ print(f"User logged in: {username}")
51
  else:
52
+ print("User not logged in.")
53
  return "Please Login to Hugging Face with the button.", None
54
 
55
+ api_url = DEFAULT_API_URL
56
+ questions_url = f"{api_url}/questions"
57
+ submit_url = f"{api_url}/submit"
58
+
59
+ try:
60
+ agent = BasicAgent()
61
+ except Exception as e:
62
+ return f"Error initializing agent: {e}", None
63
+
64
  space_id = os.getenv("SPACE_ID")
65
  agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main" if space_id else ""
66
 
67
  try:
68
+ response = requests.get(questions_url, timeout=15)
69
  response.raise_for_status()
70
  questions_data = response.json()
71
  except Exception as e:
 
76
  for item in questions_data:
77
  task_id = item.get("task_id")
78
  question_text = item.get("question")
79
+ if not task_id or question_text is None:
80
  continue
81
  try:
82
  submitted_answer = agent(question_text)
 
92
  }
93
 
94
  try:
95
+ response = requests.post(submit_url, json=submission_data, timeout=60)
96
  response.raise_for_status()
97
  result_data = response.json()
98
+ final_status = (
99
+ f"Submission Successful!\n"
100
+ f"Score: {result_data.get('score', 'N/A')}% "
101
+ f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)"
102
+ )
103
  return final_status, pd.DataFrame(results_log)
104
  except Exception as e:
105
  return f"Submission failed: {e}", pd.DataFrame(results_log)
106
 
107
+ # --- Keep Original Gradio Interface ---
108
  with gr.Blocks() as demo:
109
+ gr.Markdown("# Basic Agent Evaluation Runner")
110
+ gr.Markdown("""... (original markdown content) ...""")
111
+
 
 
 
112
  gr.LoginButton()
113
+ run_button = gr.Button("Run Evaluation & Submit All Answers")
114
+ status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
115
+ results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
116
+
117
+ run_button.click(
118
+ fn=run_and_submit_all,
119
+ outputs=[status_output, results_table]
120
+ )
121
 
122
  if __name__ == "__main__":
123
  demo.launch()