File size: 3,040 Bytes
ef933ef ed3530e ef933ef ed3530e | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 | import requests
import json
import os
ROOT_API = "https://Samin7479-ABSAAPI.hf.space"
def call_greets_json():
headers = {"Content-Type": "application/json"}
try:
response = requests.get(ROOT_API + "/greet", json={}, headers=headers)
response.raise_for_status()
result = response.json()
return {
"status": "success",
"response": result,
"message": "Endpoint is working correctly"
}
except requests.exceptions.HTTPError as http_err:
return {
"status": "error",
"error": f"HTTP error occurred: {str(http_err)}",
"status_code": response.status_code
}
except requests.exceptions.RequestException as req_err:
return {
"status": "error",
"error": f"Request error occurred: {str(req_err)}"
}
except json.JSONDecodeError:
return {
"status": "error",
"error": "Invalid JSON response from API"
}
def call_predict_api(text: str, aspect: str) -> dict:
payload = {
"text": text,
"aspect": aspect
}
headers = {
"Content-Type": "application/json"
}
try:
response = requests.post(ROOT_API + "/predict", json=payload, headers=headers)
response.raise_for_status()
result = response.json()
return {
"status": "success",
"sentiment": result.get("sentiment"),
"probabilities": result.get("probabilities"),
"raw_response": result
}
except requests.exceptions.HTTPError as http_err:
return {
"status": "error",
"error": f"HTTP error occurred: {str(http_err)}",
"status_code": response.status_code
}
except requests.exceptions.RequestException as req_err:
return {
"status": "error",
"error": f"Request error occurred: {str(req_err)}"
}
except json.JSONDecodeError:
return {
"status": "error",
"error": "Invalid JSON response from API",
"raw_response": response.text
}
if __name__ == "__main__":
# response = call_greets_json()
# print(response)
test_cases = [
{"text": "The food was great but the service was slow", "aspect": "food"},
{"text": "The ambiance is nice but very crowded", "aspect": "ambiance"}
]
for case in test_cases:
result = call_predict_api(case["text"], case["aspect"])
print(f"\nInput: text='{case['text']}', aspect='{case['aspect']}'")
if result["status"] == "success":
print(f"Sentiment: {result['sentiment']}")
print(f"Probabilities: {result['probabilities']}")
else:
print(f"Error: {result['error']}")
if "status_code" in result:
print(f"Status Code: {result['status_code']}")
if "raw_response" in result:
print(f"Raw Response: {result['raw_response']}") |