bhatanerohan commited on
Commit
6f3256c
·
verified ·
1 Parent(s): 81917a3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +55 -8
app.py CHANGED
@@ -10,14 +10,61 @@ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
10
 
11
  # --- Basic Agent Definition ---
12
  # ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
13
- class BasicAgent:
14
- def __init__(self):
15
- print("BasicAgent initialized.")
16
- def __call__(self, question: str) -> str:
17
- print(f"Agent received question (first 50 chars): {question[:50]}...")
18
- fixed_answer = "This is a default answer."
19
- print(f"Agent returning fixed answer: {fixed_answer}")
20
- return fixed_answer
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
 
22
  def run_and_submit_all( profile: gr.OAuthProfile | None):
23
  """
 
10
 
11
  # --- Basic Agent Definition ---
12
  # ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
13
+ import os
14
+ import time
15
+ from functools import lru_cache
16
+ from openai import OpenAI, RateLimitError, APIError
17
+
18
+ # --- GPT-4o-mini powered agent ------------------------------------------------
19
+ class GPT4oMiniAgent:
20
+ """
21
+ Very small wrapper around OpenAI GPT-4o-mini.
22
+ • Reads the API key from the OPENAI_API_KEY env variable (set as a HF secret)
23
+ • Uses temperature-0 for deterministic grading
24
+ • Retries on transient rate-limit / 5xx errors with exponential back-off
25
+ • Caches identical questions in memory so reruns are near-instant
26
+ """
27
+ def __init__(self, max_retries:int=3, backoff:float=2.0):
28
+ api_key = os.getenv("OPENAI_API_KEY")
29
+ if not api_key:
30
+ raise EnvironmentError(
31
+ "OPENAI_API_KEY not found – add it in the Space secrets!"
32
+ )
33
+ self.client = OpenAI(api_key=api_key)
34
+ self.max_retries = max_retries
35
+ self.backoff = backoff
36
+ self.system_prompt = (
37
+ "You are a concise, precise assistant. "
38
+ "Answer fact questions directly. "
39
+ "When asked for code, return valid Python in fenced ``` blocks."
40
+ )
41
+ print("✅ GPT4oMiniAgent initialised.")
42
+
43
+ # in-memory cache so if the grader calls the same Q twice we don’t spend tokens
44
+ @lru_cache(maxsize=256)
45
+ def __call__(self, question: str) -> str: # noqa: D401
46
+ print(f"🔹 Asking gpt-4o-mini: {question[:60]}…")
47
+ for attempt in range(1, self.max_retries + 1):
48
+ try:
49
+ response = self.client.chat.completions.create(
50
+ model="gpt-4o-mini", # ← official name :contentReference[oaicite:0]{index=0}
51
+ messages=[
52
+ {"role": "system", "content": self.system_prompt},
53
+ {"role": "user", "content": question},
54
+ ],
55
+ max_tokens=512,
56
+ temperature=0.0,
57
+ )
58
+ answer = response.choices[0].message.content.strip()
59
+ print(f"🔸 Answer (truncated): {answer[:60]}…")
60
+ return answer
61
+ except (RateLimitError, APIError) as e:
62
+ wait = self.backoff * attempt
63
+ print(f"⚠️ OpenAI error: {e}. Retry {attempt}/{self.max_retries} after {wait}s.")
64
+ time.sleep(wait)
65
+ # If all retries failed
66
+ return "I’m sorry, I could not generate an answer due to repeated API errors."
67
+
68
 
69
  def run_and_submit_all( profile: gr.OAuthProfile | None):
70
  """