varshakolanu commited on
Commit
b174f19
·
verified ·
1 Parent(s): 3336ece

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -52
app.py CHANGED
@@ -7,63 +7,63 @@ import json
7
  # Initialize sentiment analysis pipeline
8
  sentiment_analyzer = pipeline("sentiment-analysis", model="distilbert-base-uncased-finetuned-sst-2-english")
9
 
10
- # FastAPI app for API endpoint
11
  app = FastAPI()
12
 
13
  @app.post("/calculate_scores")
14
  async def calculate_scores(request: Request):
15
- logs = await request.json()
16
- quality_score = 0
17
- timeliness_score = 0
18
- safety_score = 0
19
- communication_score = 0
20
- quality_count = 0
21
- delay_count = 0
22
- incident_count = 0
23
- feedback_count = 0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24
 
25
- for log in logs:
26
- log_type = log.get("log_type")
27
- # Safely handle quality_score
28
- quality_value = log.get("quality_score")
29
- if log_type == "Quality" and quality_value != None:
30
- try:
31
- quality_score += float(quality_value)
32
- quality_count += 1
33
- except (ValueError, TypeError):
34
- pass
35
- # Safely handle delay_percentage
36
- delay_value = log.get("delay_percentage")
37
- if log_type == "Delay" and delay_value != None:
38
- try:
39
- timeliness_score += (100 - float(delay_value))
40
- delay_count += 1
41
- except (ValueError, TypeError):
42
- pass
43
- if log_type == "Incident":
44
- safety_score += (100 if log.get("safety_compliance", False) else 0)
45
- incident_count += 1
46
- if log.get("feedback"):
47
- feedback = log["feedback"]
48
- sentiment = sentiment_analyzer(feedback)[0]
49
- sentiment_score = sentiment["score"] * 100 if sentiment["label"] == "POSITIVE" else 50
50
- base_score = 90 if len(feedback) > 50 else 70
51
- communication_score += base_score * (sentiment_score / 100)
52
- feedback_count += 1
53
 
54
- # Calculate averages
55
- quality_score = quality_score / quality_count if quality_count > 0 else 0
56
- timeliness_score = timeliness_score / delay_count if delay_count > 0 else 0
57
- safety_score = safety_score / incident_count if incident_count > 0 else 0
58
- communication_score = communication_score / feedback_count if feedback_count > 0 else 0
59
 
60
- return {
61
- "quality_score": round(quality_score, 2),
62
- "timeliness_score": round(timeliness_score, 2),
63
- "safety_score": round(safety_score, 2),
64
- "communication_score": round(communication_score, 2),
65
- "alert_flag": quality_score < 50 or timeliness_score < 50
66
- }
 
 
67
 
68
  # Gradio interface for testing
69
  def test_api(logs_json):
@@ -74,7 +74,6 @@ def test_api(logs_json):
74
  except Exception as e:
75
  return f"Error: {str(e)}"
76
 
77
- # Launch Gradio UI
78
  gr.Interface(
79
  fn=test_api,
80
  inputs=gr.Textbox(label="Enter Logs JSON", placeholder='[{"log_type": "Quality", "quality_score": 80, "feedback": "Great work!"}]'),
@@ -82,6 +81,6 @@ gr.Interface(
82
  title="Subcontractor Scoring API Tester"
83
  ).launch()
84
 
85
- # Run FastAPI with Uvicorn
86
  if __name__ == "__main__":
87
  uvicorn.run(app, host="0.0.0.0", port=7860)
 
 
7
  # Initialize sentiment analysis pipeline
8
  sentiment_analyzer = pipeline("sentiment-analysis", model="distilbert-base-uncased-finetuned-sst-2-english")
9
 
10
+ # FastAPI app
11
  app = FastAPI()
12
 
13
  @app.post("/calculate_scores")
14
  async def calculate_scores(request: Request):
15
+ try:
16
+ logs = await request.json()
17
+ quality_score = 0
18
+ timeliness_score = 0
19
+ safety_score = 0
20
+ communication_score = 0
21
+ quality_count = 0
22
+ delay_count = 0
23
+ incident_count = 0
24
+ feedback_count = 0
25
+
26
+ for log in logs:
27
+ log_type = log.get("log_type")
28
+ if log_type == "Quality" and "quality_score" in log and log["quality_score"] is not None:
29
+ try:
30
+ quality_score += float(log["quality_score"])
31
+ quality_count += 1
32
+ except (ValueError, TypeError):
33
+ pass
34
+ if log_type == "Delay" and "delay_percentage" in log and log["delay_percentage"] is not None:
35
+ try:
36
+ timeliness_score += (100 - float(log["delay_percentage"]))
37
+ delay_count += 1
38
+ except (ValueError, TypeError):
39
+ pass
40
+ if log_type == "Incident":
41
+ safety_score += (100 if log.get("safety_compliance", False) else 0)
42
+ incident_count += 1
43
+ if "feedback" in log and log["feedback"]:
44
+ feedback = log["feedback"]
45
+ sentiment = sentiment_analyzer(feedback)[0]
46
+ sentiment_score = sentiment["score"] * 100 if sentiment["label"] == "POSITIVE" else 50
47
+ base_score = 90 if len(feedback) > 50 else 70
48
+ communication_score += base_score * (sentiment_score / 100)
49
+ feedback_count += 1
50
 
51
+ quality_score = quality_score / quality_count if quality_count > 0 else 0
52
+ timeliness_score = timeliness_score / delay_count if delay_count > 0 else 0
53
+ safety_score = safety_score / incident_count if incident_count > 0 else 0
54
+ communication_score = communication_score / feedback_count if feedback_count > 0 else 0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55
 
56
+ alert_flag = quality_score < 50 or timeliness_score < 50
 
 
 
 
57
 
58
+ return {
59
+ "quality_score": round(quality_score, 2),
60
+ "timeliness_score": round(timeliness_score, 2),
61
+ "safety_score": round(safety_score, 2),
62
+ "communication_score": round(communication_score, 2),
63
+ "alert_flag": alert_flag
64
+ }
65
+ except Exception as e:
66
+ return {"error": str(e)}, 400
67
 
68
  # Gradio interface for testing
69
  def test_api(logs_json):
 
74
  except Exception as e:
75
  return f"Error: {str(e)}"
76
 
 
77
  gr.Interface(
78
  fn=test_api,
79
  inputs=gr.Textbox(label="Enter Logs JSON", placeholder='[{"log_type": "Quality", "quality_score": 80, "feedback": "Great work!"}]'),
 
81
  title="Subcontractor Scoring API Tester"
82
  ).launch()
83
 
 
84
  if __name__ == "__main__":
85
  uvicorn.run(app, host="0.0.0.0", port=7860)
86
+