“Transcendental-Programmer” commited on
Commit
1936a50
·
1 Parent(s): 74dd7d1

final commit

Browse files
Files changed (4) hide show
  1. app.py +17 -10
  2. routes.py +24 -5
  3. test_endpoints.py +1 -1
  4. test_results1/endpoint_test_results.json +185 -12
app.py CHANGED
@@ -1,9 +1,9 @@
1
  import os
 
2
  from flask import Flask, jsonify
3
- from routes import api_bp, initialize_models
4
  import logging
5
  from datetime import datetime
6
- from flask_cors import CORS
7
 
8
  # Configure logging
9
  logging.basicConfig(
@@ -14,15 +14,23 @@ logging.basicConfig(
14
  ]
15
  )
16
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
  def create_app():
18
  app = Flask(__name__)
19
 
20
- # Enable CORS for all routes
21
- CORS(app, resources={r"/api/*": {"origins": "*"}})
22
-
23
- # Initialize models on startup
24
- initialize_models()
25
-
26
  # Register blueprints
27
  app.register_blueprint(api_bp, url_prefix='/api')
28
 
@@ -41,6 +49,5 @@ def create_app():
41
  app = create_app()
42
 
43
  if __name__ == '__main__':
44
- # Use the port specified by Hugging Face Spaces
45
  port = int(os.environ.get('PORT', 7860))
46
- app.run(host='0.0.0.0', port=port)
 
1
  import os
2
+ from dotenv import load_dotenv
3
  from flask import Flask, jsonify
4
+ from routes import api_bp
5
  import logging
6
  from datetime import datetime
 
7
 
8
  # Configure logging
9
  logging.basicConfig(
 
14
  ]
15
  )
16
 
17
+ logger = logging.getLogger(__name__)
18
+
19
+ # Add debug logging for .env file
20
+ current_dir = os.path.dirname(os.path.abspath(__file__))
21
+ env_path = os.path.join(current_dir, '.env')
22
+ logger.info(f"Looking for .env file at: {env_path}")
23
+ logger.info(f"File exists: {os.path.exists(env_path)}")
24
+
25
+ # Load environment variables before creating the app
26
+ load_dotenv(env_path)
27
+
28
+ # Add debug logging for environment variable
29
+ logger.info(f"HUGGINGFACE_API_TOKEN after loading: {'HUGGINGFACE_API_TOKEN' in os.environ}")
30
+
31
  def create_app():
32
  app = Flask(__name__)
33
 
 
 
 
 
 
 
34
  # Register blueprints
35
  app.register_blueprint(api_bp, url_prefix='/api')
36
 
 
49
  app = create_app()
50
 
51
  if __name__ == '__main__':
 
52
  port = int(os.environ.get('PORT', 7860))
53
+ app.run(host='0.0.0.0', port=port, debug=True)
routes.py CHANGED
@@ -5,16 +5,30 @@ from models.intelligent_routing.model import IntelligentRoutingModel
5
  from models.job_recommendation.model import JobRecommendationModel
6
  import logging
7
  from datetime import datetime
 
8
 
9
  logger = logging.getLogger(__name__)
10
 
 
 
 
11
  api_bp = Blueprint('api', __name__)
12
 
13
  # Initialize models with proper paths
14
- translation_model = MultilingualTranslationModel()
15
- sentiment_model = SentimentAnalysisModel()
16
- routing_model = IntelligentRoutingModel()
17
- job_model = JobRecommendationModel()
 
 
 
 
 
 
 
 
 
 
18
 
19
  # Load pre-trained models
20
  try:
@@ -53,16 +67,21 @@ def translate():
53
  def analyze_sentiment():
54
  try:
55
  data = request.get_json()
 
56
 
57
  if not data or 'grievance_id' not in data or 'text' not in data:
 
58
  return jsonify({
59
  'error': 'Missing required fields',
60
  'required': ['grievance_id', 'text']
61
  }), 400
62
 
 
63
  result = sentiment_model.process_grievance(data)
 
64
 
65
  if result.get('error'):
 
66
  return jsonify({
67
  'error': result['error']
68
  }), 400
@@ -75,7 +94,7 @@ def analyze_sentiment():
75
  }), 200
76
 
77
  except Exception as e:
78
- logger.error(f"Sentiment analysis error: {str(e)}")
79
  return jsonify({
80
  'error': 'Internal server error',
81
  'details': str(e)
 
5
  from models.job_recommendation.model import JobRecommendationModel
6
  import logging
7
  from datetime import datetime
8
+ import os
9
 
10
  logger = logging.getLogger(__name__)
11
 
12
+ # Add debug logging for environment variables
13
+ logger.info(f"HUGGINGFACE_API_TOKEN present: {'HUGGINGFACE_API_TOKEN' in os.environ}")
14
+
15
  api_bp = Blueprint('api', __name__)
16
 
17
  # Initialize models with proper paths
18
+ try:
19
+ logger.info("Initializing translation model...")
20
+ translation_model = MultilingualTranslationModel()
21
+
22
+ logger.info("Initializing sentiment model...")
23
+ sentiment_model = SentimentAnalysisModel()
24
+
25
+ logger.info("Initializing routing model...")
26
+ routing_model = IntelligentRoutingModel()
27
+
28
+ logger.info("Initializing job model...")
29
+ job_model = JobRecommendationModel()
30
+ except Exception as e:
31
+ logger.error(f"Error initializing models: {str(e)}")
32
 
33
  # Load pre-trained models
34
  try:
 
67
  def analyze_sentiment():
68
  try:
69
  data = request.get_json()
70
+ logger.info(f"Received sentiment analysis request with data: {data}")
71
 
72
  if not data or 'grievance_id' not in data or 'text' not in data:
73
+ logger.warning("Missing required fields in request")
74
  return jsonify({
75
  'error': 'Missing required fields',
76
  'required': ['grievance_id', 'text']
77
  }), 400
78
 
79
+ logger.info("Processing grievance with sentiment model...")
80
  result = sentiment_model.process_grievance(data)
81
+ logger.info(f"Sentiment analysis result: {result}")
82
 
83
  if result.get('error'):
84
+ logger.error(f"Error in sentiment model: {result['error']}")
85
  return jsonify({
86
  'error': result['error']
87
  }), 400
 
94
  }), 200
95
 
96
  except Exception as e:
97
+ logger.error(f"Sentiment analysis error: {str(e)}", exc_info=True) # Added exc_info for full traceback
98
  return jsonify({
99
  'error': 'Internal server error',
100
  'details': str(e)
test_endpoints.py CHANGED
@@ -19,7 +19,7 @@ logging.basicConfig(
19
  logger = logging.getLogger(__name__)
20
 
21
  class EndpointTester:
22
- def __init__(self, base_url='https://huggingface.co/spaces/ArchCoder/Hostel-Management-and-Greivance-Redressal-System'):
23
  self.base_url = base_url
24
  self.results_dir = 'test_results1'
25
  # Create results directory if it doesn't exist
 
19
  logger = logging.getLogger(__name__)
20
 
21
  class EndpointTester:
22
+ def __init__(self, base_url='http://127.0.0.1:8000/api'):
23
  self.base_url = base_url
24
  self.results_dir = 'test_results1'
25
  # Create results directory if it doesn't exist
test_results1/endpoint_test_results.json CHANGED
@@ -1,5 +1,5 @@
1
  {
2
- "timestamp": "2024-10-28T18:28:04.957085",
3
  "health_check": {
4
  "status_code": 200,
5
  "response": {
@@ -10,7 +10,7 @@
10
  "translation": "up"
11
  },
12
  "status": "healthy",
13
- "timestamp": "2024-10-28T18:28:01.846113"
14
  }
15
  },
16
  "translation_tests": [
@@ -27,7 +27,7 @@
27
  "original_message": "toilet me paani nahi aa rha hain",
28
  "source_language": "hi",
29
  "target_language": "en",
30
- "timestamp": "2024-10-28T18:28:02.381673",
31
  "translated_message": "There is no water coming in the toilet"
32
  }
33
  },
@@ -44,7 +44,7 @@
44
  "original_message": "AC not working properly",
45
  "source_language": "af",
46
  "target_language": "hi",
47
- "timestamp": "2024-10-28T18:28:09.334685",
48
  "translated_message": "एसी ठीक से काम नहीं कर रहा है"
49
  }
50
  },
@@ -61,7 +61,7 @@
61
  "original_message": "ਪੱਖਾ ਕੰਮ ਨਹੀਂ ਕਰ ਰਿਹਾ",
62
  "source_language": "pa",
63
  "target_language": "en",
64
- "timestamp": "2024-10-28T18:28:10.832359",
65
  "translated_message": "The fan is not working"
66
  }
67
  }
@@ -77,7 +77,7 @@
77
  "confidence": 0.98044353723526,
78
  "emotional_label": "Frustration",
79
  "grievance_id": "G12347",
80
- "timestamp": "2024-10-28T18:28:12.376193"
81
  }
82
  },
83
  {
@@ -90,7 +90,7 @@
90
  "confidence": 0.938926100730896,
91
  "emotional_label": "Satisfaction",
92
  "grievance_id": "G12348",
93
- "timestamp": "2024-10-28T18:28:13.784609"
94
  }
95
  },
96
  {
@@ -103,11 +103,174 @@
103
  "confidence": 0.8782024383544922,
104
  "emotional_label": "Frustration",
105
  "grievance_id": "G12349",
106
- "timestamp": "2024-10-28T18:28:15.172750"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
107
  }
108
  }
109
  ],
110
- "routing_tests": [],
111
  "job_recommendation_tests": [
112
  {
113
  "test_case": {
@@ -147,10 +310,20 @@
147
  }
148
  ]
149
  },
150
- "status_code": 500,
151
  "response": {
152
- "details": "hour must be in 0..23",
153
- "error": "Internal server error"
 
 
 
 
 
 
 
 
 
 
154
  }
155
  }
156
  ]
 
1
  {
2
+ "timestamp": "2024-10-30T09:05:21.079985",
3
  "health_check": {
4
  "status_code": 200,
5
  "response": {
 
10
  "translation": "up"
11
  },
12
  "status": "healthy",
13
+ "timestamp": "2024-10-30T09:05:21.070991"
14
  }
15
  },
16
  "translation_tests": [
 
27
  "original_message": "toilet me paani nahi aa rha hain",
28
  "source_language": "hi",
29
  "target_language": "en",
30
+ "timestamp": "2024-10-30T09:05:21.465597",
31
  "translated_message": "There is no water coming in the toilet"
32
  }
33
  },
 
44
  "original_message": "AC not working properly",
45
  "source_language": "af",
46
  "target_language": "hi",
47
+ "timestamp": "2024-10-30T09:05:23.863590",
48
  "translated_message": "एसी ठीक से काम नहीं कर रहा है"
49
  }
50
  },
 
61
  "original_message": "ਪੱਖਾ ਕੰਮ ਨਹੀਂ ਕਰ ਰਿਹਾ",
62
  "source_language": "pa",
63
  "target_language": "en",
64
+ "timestamp": "2024-10-30T09:05:25.222452",
65
  "translated_message": "The fan is not working"
66
  }
67
  }
 
77
  "confidence": 0.98044353723526,
78
  "emotional_label": "Frustration",
79
  "grievance_id": "G12347",
80
+ "timestamp": "2024-10-30T09:05:27.138495"
81
  }
82
  },
83
  {
 
90
  "confidence": 0.938926100730896,
91
  "emotional_label": "Satisfaction",
92
  "grievance_id": "G12348",
93
+ "timestamp": "2024-10-30T09:05:29.544066"
94
  }
95
  },
96
  {
 
103
  "confidence": 0.8782024383544922,
104
  "emotional_label": "Frustration",
105
  "grievance_id": "G12349",
106
+ "timestamp": "2024-10-30T09:05:31.596253"
107
+ }
108
+ }
109
+ ],
110
+ "routing_tests": [
111
+ {
112
+ "test_case": {
113
+ "grievance_id": "G67890",
114
+ "category": "water_cooler",
115
+ "submission_timestamp": "2024-10-28T04:22:13Z",
116
+ "student_room_no": "148",
117
+ "hostel_name": "bh2",
118
+ "floor_number": 3,
119
+ "current_staff_status": [
120
+ {
121
+ "staff_id": "S84148",
122
+ "department": "water_cooler",
123
+ "current_workload": 0,
124
+ "availability_status": "Unavailable",
125
+ "past_resolution_rate": 0.98
126
+ },
127
+ {
128
+ "staff_id": "S57369",
129
+ "department": "water_cooler",
130
+ "current_workload": 0,
131
+ "availability_status": "Available",
132
+ "past_resolution_rate": 0.96
133
+ }
134
+ ],
135
+ "floor_metrics": {
136
+ "number_of_requests": 0,
137
+ "total_delays": 4
138
+ },
139
+ "availability_data": {
140
+ "staff_availability": [
141
+ {
142
+ "staff_id": "S84148",
143
+ "time_slot": "08:00-12:00",
144
+ "availability_status": "Available"
145
+ }
146
+ ],
147
+ "student_availability": [
148
+ {
149
+ "student_id": "STU200",
150
+ "time_slot": "16:00-20:00",
151
+ "availability_status": "Available"
152
+ }
153
+ ]
154
+ }
155
+ },
156
+ "status_code": 200,
157
+ "response": {
158
+ "assigned_staff_id": "S84148",
159
+ "assignment_timestamp": "2024-10-30T09:05:33Z",
160
+ "confidence_score": 0.0,
161
+ "estimated_resolution_time": null,
162
+ "grievance_id": "G67890"
163
+ }
164
+ },
165
+ {
166
+ "test_case": {
167
+ "grievance_id": "G67891",
168
+ "category": "carpenter",
169
+ "submission_timestamp": "2024-10-28T04:18:13Z",
170
+ "student_room_no": "213",
171
+ "hostel_name": "bh1",
172
+ "floor_number": 2,
173
+ "current_staff_status": [
174
+ {
175
+ "staff_id": "S52775",
176
+ "department": "carpenter",
177
+ "current_workload": 5,
178
+ "availability_status": "Unavailable",
179
+ "past_resolution_rate": 0.98
180
+ },
181
+ {
182
+ "staff_id": "S24943",
183
+ "department": "carpenter",
184
+ "current_workload": 3,
185
+ "availability_status": "Unavailable",
186
+ "past_resolution_rate": 0.85
187
+ }
188
+ ],
189
+ "floor_metrics": {
190
+ "number_of_requests": 29,
191
+ "total_delays": 4
192
+ },
193
+ "availability_data": {
194
+ "staff_availability": [
195
+ {
196
+ "staff_id": "S52775",
197
+ "time_slot": "12:00-16:00",
198
+ "availability_status": "Unavailable"
199
+ }
200
+ ],
201
+ "student_availability": [
202
+ {
203
+ "student_id": "STU201",
204
+ "time_slot": "08:00-12:00",
205
+ "availability_status": "Available"
206
+ }
207
+ ]
208
+ }
209
+ },
210
+ "status_code": 200,
211
+ "response": {
212
+ "assigned_staff_id": "S24943",
213
+ "assignment_timestamp": "2024-10-30T09:05:34Z",
214
+ "confidence_score": 0.0,
215
+ "estimated_resolution_time": null,
216
+ "grievance_id": "G67891"
217
+ }
218
+ },
219
+ {
220
+ "test_case": {
221
+ "grievance_id": "G67892",
222
+ "category": "electricity",
223
+ "submission_timestamp": "2024-10-28T04:00:13Z",
224
+ "student_room_no": "275",
225
+ "hostel_name": "bh3",
226
+ "floor_number": 2,
227
+ "current_staff_status": [
228
+ {
229
+ "staff_id": "S33368",
230
+ "department": "electricity",
231
+ "current_workload": 1,
232
+ "availability_status": "Available",
233
+ "past_resolution_rate": 0.95
234
+ },
235
+ {
236
+ "staff_id": "S20522",
237
+ "department": "electricity",
238
+ "current_workload": 5,
239
+ "availability_status": "Available",
240
+ "past_resolution_rate": 0.87
241
+ }
242
+ ],
243
+ "floor_metrics": {
244
+ "number_of_requests": 2,
245
+ "total_delays": 3
246
+ },
247
+ "availability_data": {
248
+ "staff_availability": [
249
+ {
250
+ "staff_id": "S33368",
251
+ "time_slot": "12:00-16:00",
252
+ "availability_status": "Unavailable"
253
+ }
254
+ ],
255
+ "student_availability": [
256
+ {
257
+ "student_id": "STU202",
258
+ "time_slot": "08:00-12:00",
259
+ "availability_status": "Unavailable"
260
+ }
261
+ ]
262
+ }
263
+ },
264
+ "status_code": 200,
265
+ "response": {
266
+ "assigned_staff_id": "S33368",
267
+ "assignment_timestamp": "2024-10-30T09:05:35Z",
268
+ "confidence_score": 0.0,
269
+ "estimated_resolution_time": null,
270
+ "grievance_id": "G67892"
271
  }
272
  }
273
  ],
 
274
  "job_recommendation_tests": [
275
  {
276
  "test_case": {
 
310
  }
311
  ]
312
  },
313
+ "status_code": 200,
314
  "response": {
315
+ "alternative_workers": [
316
+ {
317
+ "confidence_score": 0.92,
318
+ "current_workload": 1,
319
+ "worker_id": "W97054"
320
+ }
321
+ ],
322
+ "confidence_score": 0.95,
323
+ "job_id": "J60000",
324
+ "predicted_completion_time": "2024-10-30T21:48:36Z",
325
+ "recommendation_timestamp": "2024-10-30T09:05:36Z",
326
+ "recommended_worker_id": "W97053"
327
  }
328
  }
329
  ]