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