Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,14 +5,28 @@ import json
|
|
| 5 |
import pandas as pd # Import pandas
|
| 6 |
|
| 7 |
# Initialize sentiment analysis pipeline
|
| 8 |
-
sentiment_analyzer = pipeline(
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
# Function to calculate scores via API call
|
| 11 |
-
def calculate_scores_from_logs(
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
"""
|
| 14 |
Calculates performance scores by calling the Salesforce API.
|
| 15 |
-
|
| 16 |
Args:
|
| 17 |
log_type_1, log_type_2 (str): Type of log (Quality, Delay, Incident, Communication).
|
| 18 |
quality_score_1, quality_score_2 (float): Quality score.
|
|
@@ -21,39 +35,67 @@ def calculate_scores_from_logs(log_type_1, quality_score_1, delay_percentage_1,
|
|
| 21 |
feedback_1, feedback_2 (str): Feedback text.
|
| 22 |
vendor_id (str): Vendor ID.
|
| 23 |
month (str): The month for which to calculate scores (YYYY-MM-DD).
|
| 24 |
-
|
| 25 |
Returns:
|
| 26 |
dict: A dictionary containing the calculated scores and alert flag, or an error message.
|
| 27 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
logs = [
|
| 29 |
{
|
| 30 |
"log_type": log_type_1,
|
| 31 |
-
"quality_score": float(quality_score_1)
|
| 32 |
-
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
"feedback": feedback_1 if log_type_1 == "Communication" else "",
|
| 35 |
},
|
| 36 |
{
|
| 37 |
"log_type": log_type_2,
|
| 38 |
-
"quality_score": float(quality_score_2)
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
"feedback": feedback_2 if log_type_2 == "Communication" else "",
|
| 42 |
},
|
| 43 |
]
|
| 44 |
payload = json.dumps(logs)
|
| 45 |
-
headers = {
|
| 46 |
# Replace with your Salesforce API endpoint
|
| 47 |
-
salesforce_api_url =
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
try:
|
| 50 |
response = requests.post(salesforce_api_url, headers=headers, data=payload)
|
| 51 |
response.raise_for_status() # Raise HTTPError for bad responses (4xx or 5xx)
|
|
|
|
| 52 |
return response.json() # Return the JSON response from Salesforce
|
| 53 |
except requests.exceptions.RequestException as e:
|
| 54 |
error_message = f"Error calling Salesforce API: {e}"
|
| 55 |
print(error_message)
|
| 56 |
return {"error": error_message} # Return a user-friendly error message
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
|
| 58 |
# Gradio Interface
|
| 59 |
iface = gr.Interface(
|
|
@@ -70,13 +112,15 @@ iface = gr.Interface(
|
|
| 70 |
gr.Radio(["True", "False"], label="Safety Compliance 2 (for Incident)"),
|
| 71 |
gr.Textbox(label="Feedback 2 (for Communication)"),
|
| 72 |
gr.Textbox(label="Vendor ID", placeholder="Enter Vendor ID"), # Added Vendor ID
|
| 73 |
-
gr.Textbox(
|
|
|
|
|
|
|
| 74 |
],
|
| 75 |
outputs=gr.Label(label="Calculated Scores"),
|
| 76 |
title="Vendor Performance Score Calculator",
|
| 77 |
-
description="Calculate vendor performance scores based on log data and Vendor ID/Month."
|
| 78 |
)
|
| 79 |
-
|
| 80 |
# Run the Gradio interface
|
| 81 |
if __name__ == "__main__":
|
| 82 |
-
iface.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|
|
|
| 5 |
import pandas as pd # Import pandas
|
| 6 |
|
| 7 |
# Initialize sentiment analysis pipeline
|
| 8 |
+
sentiment_analyzer = pipeline(
|
| 9 |
+
"sentiment-analysis", model="distilbert-base-uncased-finetuned-sst-2-english"
|
| 10 |
+
)
|
| 11 |
+
|
| 12 |
|
| 13 |
# Function to calculate scores via API call
|
| 14 |
+
def calculate_scores_from_logs(
|
| 15 |
+
log_type_1,
|
| 16 |
+
quality_score_1,
|
| 17 |
+
delay_percentage_1,
|
| 18 |
+
safety_compliance_1,
|
| 19 |
+
feedback_1,
|
| 20 |
+
log_type_2,
|
| 21 |
+
quality_score_2,
|
| 22 |
+
delay_percentage_2,
|
| 23 |
+
safety_compliance_2,
|
| 24 |
+
feedback_2,
|
| 25 |
+
vendor_id,
|
| 26 |
+
month,
|
| 27 |
+
):
|
| 28 |
"""
|
| 29 |
Calculates performance scores by calling the Salesforce API.
|
|
|
|
| 30 |
Args:
|
| 31 |
log_type_1, log_type_2 (str): Type of log (Quality, Delay, Incident, Communication).
|
| 32 |
quality_score_1, quality_score_2 (float): Quality score.
|
|
|
|
| 35 |
feedback_1, feedback_2 (str): Feedback text.
|
| 36 |
vendor_id (str): Vendor ID.
|
| 37 |
month (str): The month for which to calculate scores (YYYY-MM-DD).
|
|
|
|
| 38 |
Returns:
|
| 39 |
dict: A dictionary containing the calculated scores and alert flag, or an error message.
|
| 40 |
"""
|
| 41 |
+
print("Entered calculate_scores_from_logs") # Added logging
|
| 42 |
+
print(f"Vendor ID: {vendor_id}, Month: {month}")
|
| 43 |
+
print(
|
| 44 |
+
f"Log 1: {log_type_1}, Quality: {quality_score_1}, Delay: {delay_percentage_1}, Safety: {safety_compliance_1}, Feedback: {feedback_1}"
|
| 45 |
+
)
|
| 46 |
+
print(
|
| 47 |
+
f"Log 2: {log_type_2}, Quality: {quality_score_2}, Delay: {delay_percentage_2}, Safety: {safety_compliance_2}, Feedback: {feedback_2}"
|
| 48 |
+
)
|
| 49 |
+
|
| 50 |
logs = [
|
| 51 |
{
|
| 52 |
"log_type": log_type_1,
|
| 53 |
+
"quality_score": float(quality_score_1)
|
| 54 |
+
if log_type_1 == "Quality" and quality_score_1
|
| 55 |
+
else None,
|
| 56 |
+
"delay_percentage": float(delay_percentage_1)
|
| 57 |
+
if log_type_1 == "Delay" and delay_percentage_1
|
| 58 |
+
else None,
|
| 59 |
+
"safety_compliance": safety_compliance_1 == "True"
|
| 60 |
+
if log_type_1 == "Incident"
|
| 61 |
+
else None,
|
| 62 |
"feedback": feedback_1 if log_type_1 == "Communication" else "",
|
| 63 |
},
|
| 64 |
{
|
| 65 |
"log_type": log_type_2,
|
| 66 |
+
"quality_score": float(quality_score_2)
|
| 67 |
+
if log_type_2 == "Quality" and quality_score_2
|
| 68 |
+
else None,
|
| 69 |
+
"delay_percentage": float(delay_percentage_2)
|
| 70 |
+
if log_type_2 == "Delay" and delay_percentage_2
|
| 71 |
+
else None,
|
| 72 |
+
"safety_compliance": safety_compliance_2 == "True"
|
| 73 |
+
if log_type_2 == "Incident"
|
| 74 |
+
else None,
|
| 75 |
"feedback": feedback_2 if log_type_2 == "Communication" else "",
|
| 76 |
},
|
| 77 |
]
|
| 78 |
payload = json.dumps(logs)
|
| 79 |
+
headers = {"Content-Type": "application/json"}
|
| 80 |
# Replace with your Salesforce API endpoint
|
| 81 |
+
salesforce_api_url = (
|
| 82 |
+
f"https://your-salesforce-domain.com/services/apexrest/VendorScoreCalculator?vendorId={vendor_id}&month={month}" # Replace
|
| 83 |
+
)
|
| 84 |
+
print(f"Salesforce API URL: {salesforce_api_url}") #Added Log
|
| 85 |
|
| 86 |
try:
|
| 87 |
response = requests.post(salesforce_api_url, headers=headers, data=payload)
|
| 88 |
response.raise_for_status() # Raise HTTPError for bad responses (4xx or 5xx)
|
| 89 |
+
print(f"Salesforce API Response: {response.text}") # Added logging
|
| 90 |
return response.json() # Return the JSON response from Salesforce
|
| 91 |
except requests.exceptions.RequestException as e:
|
| 92 |
error_message = f"Error calling Salesforce API: {e}"
|
| 93 |
print(error_message)
|
| 94 |
return {"error": error_message} # Return a user-friendly error message
|
| 95 |
+
except json.JSONDecodeError as e:
|
| 96 |
+
error_message = f"Error decoding JSON response: {e}, Response Text: {response.text}"
|
| 97 |
+
print(error_message)
|
| 98 |
+
return {"error": error_message}
|
| 99 |
|
| 100 |
# Gradio Interface
|
| 101 |
iface = gr.Interface(
|
|
|
|
| 112 |
gr.Radio(["True", "False"], label="Safety Compliance 2 (for Incident)"),
|
| 113 |
gr.Textbox(label="Feedback 2 (for Communication)"),
|
| 114 |
gr.Textbox(label="Vendor ID", placeholder="Enter Vendor ID"), # Added Vendor ID
|
| 115 |
+
gr.Textbox(
|
| 116 |
+
label="Month (YYYY-MM-DD)", placeholder="Enter Month (YYYY-MM-DD)"
|
| 117 |
+
), # Added Month
|
| 118 |
],
|
| 119 |
outputs=gr.Label(label="Calculated Scores"),
|
| 120 |
title="Vendor Performance Score Calculator",
|
| 121 |
+
description="Calculate vendor performance scores based on log data and Vendor ID/Month.",
|
| 122 |
)
|
|
|
|
| 123 |
# Run the Gradio interface
|
| 124 |
if __name__ == "__main__":
|
| 125 |
+
iface.launch(server_name="0.0.0.0", server_port=7860)
|
| 126 |
+
|