import gradio as gr import nltk from nltk.sentiment.vader import SentimentIntensityAnalyzer import re # Download required NLTK resources try: nltk.data.find('sentiment/vader_lexicon.zip') except LookupError: nltk.download('vader_lexicon') try: nltk.data.find('tokenizers/punkt/english.pickle') except LookupError: nltk.download('punkt') def calculate_scores(work_completion, delay_reports, incident_logs): """ Calculates vendor performance scores based on log entries. This is a simplified, rule-based demo version. """ quality_score = 100.0 # Initialize as float timeliness_score = 100.0 safety_score = 100.0 communication_score = 100.0 # --- Quality Score --- quality_keywords_positive = ["good", "excellent", "high quality", "efficient", "precise"] quality_keywords_negative = ["poor", "bad", "low quality", "defect", "error"] for keyword in quality_keywords_positive: quality_score += work_completion.lower().count(keyword) * 5 for keyword in quality_keywords_negative: quality_score -= work_completion.lower().count(keyword) * 10 quality_score = max(0.0, min(quality_score, 100.0)) # --- Timeliness Score --- timeliness_keywords_positive = ["on time", "punctual", "early", "ahead of schedule"] timeliness_keywords_negative = ["late", "delayed", "behind schedule"] for keyword in timeliness_keywords_positive: timeliness_score += work_completion.lower().count(keyword) * 5 + delay_reports.lower().count(keyword) * 5 for keyword in timeliness_keywords_negative: timeliness_score -= delay_reports.lower().count(keyword) * 10 timeliness_score = max(0.0, min(timeliness_score, 100.0)) # --- Safety Score --- safety_keywords_positive = ["safe", "safety protocol", "no accidents", "precaution"] safety_keywords_negative = ["unsafe", "accident", "injury", "hazard"] for keyword in safety_keywords_positive: safety_score += work_completion.lower().count(keyword) * 5 + incident_logs.lower().count(keyword) * 5 for keyword in safety_keywords_negative: safety_score -= incident_logs.lower().count(keyword) * 15 safety_score = max(0.0, min(safety_score, 100.0)) # --- Communication Score --- communication_keywords_positive = ["clear communication", "responsive", "proactive", "helpful"] communication_keywords_negative = ["unresponsive", "late reply", "miscommunication", "unclear"] for keyword in communication_keywords_positive: communication_score += work_completion.lower().count(keyword) * 5 + delay_reports.lower().count(keyword) * 5 + incident_logs.lower().count(keyword) * 5 for keyword in communication_keywords_negative: communication_score -= delay_reports.lower().count(keyword) * 10 communication_score = max(0.0, min(communication_score, 100.0)) # Basic Sentiment Analysis (Optional - Requires NLTK Download) analyzer = SentimentIntensityAnalyzer() vs = analyzer.polarity_scores(work_completion + " " + delay_reports + " " + incident_logs) sentiment_score = vs['compound'] communication_score += sentiment_score * 10 # Adjust weight as needed communication_score = max(0.0, min(communication_score, 100.0)) # Calculate Final Score (as per Salesforce formula) final_score = (0.4 * quality_score + 0.3 * timeliness_score + 0.15 * safety_score + 0.15 * communication_score) final_score = max(0.0, min(final_score, 100.0)) return { "Quality Score": round(quality_score, 2), "Timeliness Score": round(timeliness_score, 2), "Safety Score": round(safety_score, 2), "Communication Score": round(communication_score, 2), "Final Score": round(final_score, 2) } if __name__ == "__main__": iface = gr.Interface( fn=calculate_scores, inputs=[ gr.Textbox(lines=5, label="Work Completion Details"), gr.Textbox(lines=5, label="Delay Reports"), gr.Textbox(lines=5, label="Incident Logs") ], outputs=gr.Label(label="Performance Scores"), title="Vendor Performance Scoring", description="Enter vendor logs to calculate performance scores." ) iface.launch()