Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -12,9 +12,9 @@ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
| 12 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
| 13 |
class BasicAgent:
|
| 14 |
def __init__(self):
|
| 15 |
-
print("
|
| 16 |
self.api_token = os.getenv("HF_API_TOKEN")
|
| 17 |
-
self.api_url = "https://api-inference.huggingface.co/models/
|
| 18 |
self.headers = {
|
| 19 |
"Authorization": f"Bearer {self.api_token}",
|
| 20 |
"Content-Type": "application/json"
|
|
@@ -26,26 +26,35 @@ class BasicAgent:
|
|
| 26 |
if not self.api_token:
|
| 27 |
return "Error: Missing Hugging Face API token."
|
| 28 |
|
| 29 |
-
prompt = f"Answer this question
|
| 30 |
|
| 31 |
payload = {
|
| 32 |
-
"inputs": prompt
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
}
|
| 34 |
|
| 35 |
try:
|
| 36 |
-
response = requests.post(self.api_url, headers=self.headers, json=payload, timeout=
|
| 37 |
response.raise_for_status()
|
| 38 |
output = response.json()
|
| 39 |
|
|
|
|
| 40 |
if isinstance(output, list) and "generated_text" in output[0]:
|
| 41 |
return output[0]["generated_text"]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
else:
|
| 43 |
-
print(f"Unexpected
|
| 44 |
-
return "Model
|
| 45 |
|
| 46 |
except Exception as e:
|
| 47 |
-
print(f"Exception during inference: {e}")
|
| 48 |
-
return f"API Error: {str(e)}"
|
| 49 |
|
| 50 |
|
| 51 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
|
|
| 12 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
| 13 |
class BasicAgent:
|
| 14 |
def __init__(self):
|
| 15 |
+
print("FLAN-T5 Agent initialized.")
|
| 16 |
self.api_token = os.getenv("HF_API_TOKEN")
|
| 17 |
+
self.api_url = "https://api-inference.huggingface.co/models/google/flan-t5-base"
|
| 18 |
self.headers = {
|
| 19 |
"Authorization": f"Bearer {self.api_token}",
|
| 20 |
"Content-Type": "application/json"
|
|
|
|
| 26 |
if not self.api_token:
|
| 27 |
return "Error: Missing Hugging Face API token."
|
| 28 |
|
| 29 |
+
prompt = f"Answer this question:\n{question.strip()}"
|
| 30 |
|
| 31 |
payload = {
|
| 32 |
+
"inputs": prompt,
|
| 33 |
+
"parameters": {
|
| 34 |
+
"max_new_tokens": 256,
|
| 35 |
+
"temperature": 0.5
|
| 36 |
+
}
|
| 37 |
}
|
| 38 |
|
| 39 |
try:
|
| 40 |
+
response = requests.post(self.api_url, headers=self.headers, json=payload, timeout=30)
|
| 41 |
response.raise_for_status()
|
| 42 |
output = response.json()
|
| 43 |
|
| 44 |
+
# Extract output
|
| 45 |
if isinstance(output, list) and "generated_text" in output[0]:
|
| 46 |
return output[0]["generated_text"]
|
| 47 |
+
elif isinstance(output, dict) and "generated_text" in output:
|
| 48 |
+
return output["generated_text"]
|
| 49 |
+
elif isinstance(output, list) and "output" in output[0]:
|
| 50 |
+
return output[0]["output"]
|
| 51 |
else:
|
| 52 |
+
print(f"Unexpected response: {output}")
|
| 53 |
+
return "Model returned an unexpected format."
|
| 54 |
|
| 55 |
except Exception as e:
|
| 56 |
+
print(f"❌ Exception during inference: {e}")
|
| 57 |
+
return f"❌ API Error: {str(e)}"
|
| 58 |
|
| 59 |
|
| 60 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|