MrArray22 commited on
Commit
713c2f1
·
verified ·
1 Parent(s): a21b613

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -84
app.py CHANGED
@@ -3,9 +3,6 @@ import gradio as gr
3
  import requests
4
  import inspect
5
  import pandas as pd
6
- import json
7
- import re
8
- from typing import Dict, Any
9
  from dotenv import load_dotenv
10
  from openai import OpenAI
11
  from tenacity import retry, stop_after_attempt, wait_exponential
@@ -16,9 +13,7 @@ load_dotenv()
16
  # (Keep Constants as is)
17
  # --- Constants ---
18
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
19
- OPENAI_MODEL = (
20
- "gpt-4-turbo-preview" # Using OpenAI's latest model for better performance
21
- )
22
 
23
 
24
  # --- Basic Agent Definition ---
@@ -27,39 +22,37 @@ class BasicAgent:
27
  def __init__(self):
28
  """Initialize the agent with OpenAI client and setup."""
29
  print("BasicAgent initializing...")
30
- self.client = OpenAI(api_key="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",base_url="https://models.github.ai/inference")
31
- self.question_history: Dict[str, Any] = {} # Store question context
 
 
 
 
32
  print("BasicAgent initialized successfully.")
33
 
34
- def _format_answer(self, raw_answer: str) -> str:
35
- """Format the answer to improve exact matching success."""
36
- # Remove any explanations or reasoning
37
- if "Answer:" in raw_answer:
38
- answer = raw_answer.split("Answer:")[-1].strip()
39
- elif "Final answer:" in raw_answer:
40
- answer = raw_answer.split("Final answer:")[-1].strip()
41
- else:
42
- answer = raw_answer.strip()
43
-
44
- # Clean up formatting
45
- answer = re.sub(
46
- r"\s+", " ", answer
47
- ) # Replace multiple spaces with single space
48
- answer = answer.strip("\"'") # Remove quotes
49
- answer = answer.strip(".") # Remove trailing periods
50
-
51
- return answer.strip()
52
-
53
  @retry(
54
  stop=stop_after_attempt(3), wait=wait_exponential(multiplier=1, min=4, max=10)
55
  )
56
- def _get_completion(self, messages: list) -> str:
57
  """Get completion from OpenAI with retry logic."""
58
  try:
59
  response = self.client.chat.completions.create(
60
  model=OPENAI_MODEL,
61
- messages=messages,
62
- temperature=0.1, # Lower temperature for more consistent outputs
 
 
 
 
 
 
 
 
 
 
 
 
 
63
  max_tokens=1000,
64
  )
65
  return response.choices[0].message.content.strip()
@@ -67,70 +60,37 @@ class BasicAgent:
67
  print(f"Error in OpenAI API call: {e}")
68
  raise
69
 
70
- def _analyze_question(self, question: str) -> dict:
71
- """Analyze the question to determine its type and required approach."""
72
- system_msg = """You are an expert at analyzing questions. For the given question:
73
- 1. Identify the question type (e.g., factual, calculation, reasoning)
74
- 2. Identify key entities and concepts
75
- 3. Determine if external knowledge is needed
76
- 4. Suggest the best approach to answer it
77
- Provide your analysis in JSON format."""
78
-
79
- messages = [
80
- {"role": "system", "content": system_msg},
81
- {"role": "user", "content": f"Analyze this question: {question}"},
82
- ]
83
 
84
- try:
85
- analysis = self._get_completion(messages)
86
- return json.loads(analysis)
87
- except:
88
- return {"type": "unknown", "approach": "direct"}
89
-
90
- def _get_answer(self, question: str, analysis: dict) -> str:
91
- """Get the answer based on question analysis."""
92
- system_prompt = f"""You are an AI assistant specialized in answering GAIA benchmark questions.
93
- Your task is to provide EXACT, PRECISE answers that can be matched against a ground truth.
94
-
95
- Guidelines:
96
- 1. Provide ONLY the final answer, no explanations
97
- 2. Be extremely precise and consistent in formatting
98
- 3. For numerical answers, use digits (e.g., "42" not "forty-two")
99
- 4. For lists, use comma-separated values without spaces after commas
100
- 5. For yes/no questions, answer only with "Yes" or "No"
101
- 6. Remove any punctuation from the end of your answer
102
- 7. Keep your answer as concise as possible while being complete
103
-
104
- Question type: {analysis.get('type', 'unknown')}
105
- Approach: {analysis.get('approach', 'direct')}
106
-
107
- Remember: Your answer will be compared EXACTLY with the ground truth. Format matters!"""
108
-
109
- messages = [
110
- {"role": "system", "content": system_prompt},
111
- {"role": "user", "content": question},
112
- ]
113
-
114
- raw_answer = self._get_completion(messages)
115
- return self._format_answer(raw_answer)
116
 
117
  def __call__(self, question: str) -> str:
118
  """Process the question and return an answer."""
119
  print(f"Agent received question (first 50 chars): {question[:50]}...")
120
 
121
  try:
122
- # Analyze the question
123
- analysis = self._analyze_question(question)
124
- print(f"Question analysis: {json.dumps(analysis, indent=2)}")
 
 
125
 
126
- # Get and format the answer
127
- answer = self._get_answer(question, analysis)
128
- print(f"Generated answer: {answer}")
 
 
129
 
130
- # Store question context
131
- self.question_history[question] = {"analysis": analysis, "answer": answer}
132
 
133
- return answer
134
 
135
  except Exception as e:
136
  print(f"Error processing question: {e}")
 
3
  import requests
4
  import inspect
5
  import pandas as pd
 
 
 
6
  from dotenv import load_dotenv
7
  from openai import OpenAI
8
  from tenacity import retry, stop_after_attempt, wait_exponential
 
13
  # (Keep Constants as is)
14
  # --- Constants ---
15
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
16
+ OPENAI_MODEL = "openai/gpt-4.1" # or "gpt-3.5-turbo" based on your preference
 
 
17
 
18
 
19
  # --- Basic Agent Definition ---
 
22
  def __init__(self):
23
  """Initialize the agent with OpenAI client and setup."""
24
  print("BasicAgent initializing...")
25
+ self.client = OpenAI(
26
+ api_key="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",
27
+ base_url="https://models.github.ai/inference",
28
+ )
29
+ if not os.getenv("OPENAI_API_KEY"):
30
+ raise ValueError("OPENAI_API_KEY environment variable is not set")
31
  print("BasicAgent initialized successfully.")
32
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33
  @retry(
34
  stop=stop_after_attempt(3), wait=wait_exponential(multiplier=1, min=4, max=10)
35
  )
36
+ def _get_completion(self, prompt: str) -> str:
37
  """Get completion from OpenAI with retry logic."""
38
  try:
39
  response = self.client.chat.completions.create(
40
  model=OPENAI_MODEL,
41
+ messages=[
42
+ {
43
+ "role": "system",
44
+ "content": """You are a Expert helpful AI assistant designed to answer questions from the GAIA benchmark.
45
+ Follow these guidelines:
46
+ 1. Provide clear, concise, and accurate answers
47
+ 2. If a question requires specific steps or calculations, show them clearly
48
+ 3. Format your response in a clean, readable way
49
+ 4. Be precise and avoid ambiguity
50
+ 5. If you're not completely sure about an answer, state your confidence level
51
+ Remember: Your answers will be evaluated through exact matching.""",
52
+ },
53
+ {"role": "user", "content": prompt},
54
+ ],
55
+ temperature=0.5, # Lower temperature for more consistent outputs
56
  max_tokens=1000,
57
  )
58
  return response.choices[0].message.content.strip()
 
60
  print(f"Error in OpenAI API call: {e}")
61
  raise
62
 
63
+ def _preprocess_question(self, question: str) -> str:
64
+ """Preprocess the question to enhance clarity and context."""
65
+ enhanced_prompt = f"""Please analyze and answer the following question from the GAIA benchmark.
66
+ Question: {question}
 
 
 
 
 
 
 
 
 
67
 
68
+ Provide a clear, specific answer that can be evaluated through exact matching.
69
+ If the question requires multiple steps, please show your reasoning but ensure the final answer is clearly stated.
70
+ """
71
+ return enhanced_prompt
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
72
 
73
  def __call__(self, question: str) -> str:
74
  """Process the question and return an answer."""
75
  print(f"Agent received question (first 50 chars): {question[:50]}...")
76
 
77
  try:
78
+ # Preprocess the question
79
+ enhanced_prompt = self._preprocess_question(question)
80
+
81
+ # Get completion from OpenAI
82
+ response = self._get_completion(enhanced_prompt)
83
 
84
+ # Extract the final answer
85
+ # If the response contains multiple lines or explanations,
86
+ # we'll try to extract just the final answer
87
+ answer_lines = response.strip().split("\n")
88
+ final_answer = answer_lines[-1].strip()
89
 
90
+ # Log the response for debugging
91
+ print(f"Agent generated answer: {final_answer[:100]}...")
92
 
93
+ return final_answer
94
 
95
  except Exception as e:
96
  print(f"Error processing question: {e}")