Spaces:
Sleeping
Sleeping
| 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) | |