File size: 5,236 Bytes
d1b6abf
89fb907
d1b6abf
 
 
89fb907
 
c0e8342
 
 
 
89fb907
d1b6abf
c0e8342
 
 
 
 
 
 
 
 
 
 
 
 
 
d32bef1
d1b6abf
d32bef1
d1b6abf
 
 
 
 
 
 
d32bef1
d1b6abf
d32bef1
c0e8342
 
 
 
 
 
 
 
 
d1b6abf
 
 
c0e8342
 
 
 
 
 
 
 
 
d1b6abf
 
 
 
c0e8342
 
 
 
 
 
 
 
 
d1b6abf
 
 
 
c0e8342
d1b6abf
c0e8342
 
 
 
d32bef1
b174f19
d1b6abf
 
c0e8342
d1b6abf
 
 
 
 
c0e8342
 
 
 
d1b6abf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c0e8342
 
 
d1b6abf
 
 
c0e8342
d1b6abf
 
896c289
c0e8342
 
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import gradio as gr
from transformers import pipeline
import requests
import json
import pandas as pd  # Import pandas

# Initialize sentiment analysis pipeline
sentiment_analyzer = pipeline(
    "sentiment-analysis", model="distilbert-base-uncased-finetuned-sst-2-english"
)


# Function to calculate scores via API call
def calculate_scores_from_logs(
    log_type_1,
    quality_score_1,
    delay_percentage_1,
    safety_compliance_1,
    feedback_1,
    log_type_2,
    quality_score_2,
    delay_percentage_2,
    safety_compliance_2,
    feedback_2,
    vendor_id,
    month,
):
    """
    Calculates performance scores by calling the Salesforce API.
    Args:
        log_type_1, log_type_2 (str): Type of log (Quality, Delay, Incident, Communication).
        quality_score_1, quality_score_2 (float): Quality score.
        delay_percentage_1, delay_percentage_2 (float): Delay percentage.
        safety_compliance_1, safety_compliance_2 (str): Safety compliance (True/False).
        feedback_1, feedback_2 (str): Feedback text.
        vendor_id (str):  Vendor ID.
        month (str): The month for which to calculate scores (YYYY-MM-DD).
    Returns:
        dict: A dictionary containing the calculated scores and alert flag, or an error message.
    """
    print("Entered calculate_scores_from_logs")  # Added logging
    print(f"Vendor ID: {vendor_id}, Month: {month}")
    print(
        f"Log 1: {log_type_1}, Quality: {quality_score_1}, Delay: {delay_percentage_1}, Safety: {safety_compliance_1}, Feedback: {feedback_1}"
    )
    print(
        f"Log 2: {log_type_2}, Quality: {quality_score_2}, Delay: {delay_percentage_2}, Safety: {safety_compliance_2}, Feedback: {feedback_2}"
    )

    logs = [
        {
            "log_type": log_type_1,
            "quality_score": float(quality_score_1)
            if log_type_1 == "Quality" and quality_score_1
            else None,
            "delay_percentage": float(delay_percentage_1)
            if log_type_1 == "Delay" and delay_percentage_1
            else None,
            "safety_compliance": safety_compliance_1 == "True"
            if log_type_1 == "Incident"
            else None,
            "feedback": feedback_1 if log_type_1 == "Communication" else "",
        },
        {
            "log_type": log_type_2,
            "quality_score": float(quality_score_2)
            if log_type_2 == "Quality" and quality_score_2
            else None,
            "delay_percentage": float(delay_percentage_2)
            if log_type_2 == "Delay" and delay_percentage_2
            else None,
            "safety_compliance": safety_compliance_2 == "True"
            if log_type_2 == "Incident"
            else None,
            "feedback": feedback_2 if log_type_2 == "Communication" else "",
        },
    ]
    payload = json.dumps(logs)
    headers = {"Content-Type": "application/json"}
    #  Replace with your Salesforce API endpoint
    salesforce_api_url = (
        f"https://your-salesforce-domain.com/services/apexrest/VendorScoreCalculator?vendorId={vendor_id}&month={month}"  # Replace
    )
    print(f"Salesforce API URL: {salesforce_api_url}") #Added Log

    try:
        response = requests.post(salesforce_api_url, headers=headers, data=payload)
        response.raise_for_status()  # Raise HTTPError for bad responses (4xx or 5xx)
        print(f"Salesforce API Response: {response.text}")  # Added logging
        return response.json()  # Return the JSON response from Salesforce
    except requests.exceptions.RequestException as e:
        error_message = f"Error calling Salesforce API: {e}"
        print(error_message)
        return {"error": error_message}  # Return a user-friendly error message
    except json.JSONDecodeError as e:
        error_message = f"Error decoding JSON response: {e}, Response Text: {response.text}"
        print(error_message)
        return {"error": error_message}

# Gradio Interface
iface = gr.Interface(
    fn=calculate_scores_from_logs,
    inputs=[
        gr.Dropdown(["Quality", "Delay", "Incident", "Communication"], label="Log Type 1"),
        gr.Number(label="Quality Score 1 (for Quality)"),
        gr.Number(label="Delay Percentage 1 (for Delay)"),
        gr.Radio(["True", "False"], label="Safety Compliance 1 (for Incident)"),
        gr.Textbox(label="Feedback 1 (for Communication)"),
        gr.Dropdown(["Quality", "Delay", "Incident", "Communication"], label="Log Type 2"),
        gr.Number(label="Quality Score 2 (for Quality)"),
        gr.Number(label="Delay Percentage 2 (for Delay)"),
        gr.Radio(["True", "False"], label="Safety Compliance 2 (for Incident)"),
        gr.Textbox(label="Feedback 2 (for Communication)"),
        gr.Textbox(label="Vendor ID", placeholder="Enter Vendor ID"),  # Added Vendor ID
        gr.Textbox(
            label="Month (YYYY-MM-DD)", placeholder="Enter Month (YYYY-MM-DD)"
        ),  # Added Month
    ],
    outputs=gr.Label(label="Calculated Scores"),
    title="Vendor Performance Score Calculator",
    description="Calculate vendor performance scores based on log data and Vendor ID/Month.",
)
# Run the Gradio interface
if __name__ == "__main__":
    iface.launch(server_name="0.0.0.0", server_port=7860)