Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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("
|
| 16 |
-
self.
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
def __call__(self, question: str) -> str:
|
| 19 |
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
| 20 |
|
| 21 |
-
if not self.
|
| 22 |
-
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
try:
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 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 |
-
|
|
|
|
| 41 |
|
| 42 |
except Exception as e:
|
| 43 |
-
|
|
|
|
| 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):
|