Nav772 commited on
Commit
38d5bbb
·
verified ·
1 Parent(s): 6e0d313

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -22
app.py CHANGED
@@ -10,37 +10,41 @@ 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("WolfarmAlpha Agent Initialized")
16
- self.app_id = os.getenv("WOLFARM_APP_ID")
17
-
 
 
 
 
 
18
  def __call__(self, question: str) -> str:
19
  print(f"Agent received question (first 50 chars): {question[:50]}...")
20
 
21
- if not self.app_id:
22
- print(" WolframAlpha App ID not found.")
23
- return "Error: WolframAlpha App ID not set."
 
 
 
 
 
24
 
25
  try:
26
- url = "http://api.wolframalpha.com/v1/result"
27
- params = {
28
- "i": question,
29
- "appid": self.app_id
30
- }
31
- response = requests.get(url, params=params, timeout=10)
32
-
33
- print(f"🔁 Request URL: {response.url}")
34
-
35
- if response.status_code == 200:
36
- return response.text
37
- elif response.status_code == 501:
38
- return "Error: WolframAlpha API endpoint doesn't support this query type (501)."
39
  else:
40
- return f"Couldn't retrieve a response from WolframAlpha. Status {response.status_code}"
 
41
 
42
  except Exception as e:
43
- return f"Exception occurred: {str(e)}"
 
44
 
45
 
46
  def run_and_submit_all( profile: gr.OAuthProfile | None):
 
10
 
11
  # --- Basic Agent Definition ---
12
  # ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
 
13
  def __init__(self):
14
+ print("FLAN-T5 API Agent initialized.")
15
+ self.api_token = os.getenv("HF_API_TOKEN")
16
+ self.api_url = "https://api-inference.huggingface.co/models/google/flan-t5-small"
17
+ self.headers = {
18
+ "Authorization": f"Bearer {self.api_token}",
19
+ "Content-Type": "application/json"
20
+ }
21
+
22
  def __call__(self, question: str) -> str:
23
  print(f"Agent received question (first 50 chars): {question[:50]}...")
24
 
25
+ if not self.api_token:
26
+ return "Error: Missing Hugging Face API token."
27
+
28
+ prompt = f"Answer this question clearly and briefly:\n{question.strip()}"
29
+
30
+ payload = {
31
+ "inputs": prompt
32
+ }
33
 
34
  try:
35
+ response = requests.post(self.api_url, headers=self.headers, json=payload, timeout=15)
36
+ response.raise_for_status()
37
+ output = response.json()
38
+
39
+ if isinstance(output, list) and "generated_text" in output[0]:
40
+ return output[0]["generated_text"]
 
 
 
 
 
 
 
41
  else:
42
+ print(f"Unexpected API response: {output}")
43
+ return "Model did not return a valid response."
44
 
45
  except Exception as e:
46
+ print(f"Exception during inference: {e}")
47
+ return f"API Error: {str(e)}"
48
 
49
 
50
  def run_and_submit_all( profile: gr.OAuthProfile | None):