Spaces:
Runtime error
Runtime error
| import requests | |
| import matplotlib.pyplot as plt | |
| # Define the URL for the prediction service | |
| url = "http://localhost:8960/predict" | |
| # Prepare the data for the prediction | |
| data = { | |
| "Age": 32, | |
| "DistanceFromHome": 2, | |
| "Education": 2, | |
| "NumCompaniesWorked": 0, | |
| "PercentSalaryHike": 2, | |
| "TotalWorkingYears": 8, | |
| "TrainingTimesLastYear": 2, | |
| "WorkLifeBalance": 2, | |
| "YearsInCurrentRole": 7, | |
| "YearsSinceLastPromotion": 5, | |
| "YearsWithCurrManager": 1, | |
| "BusinessTravel_Travel_Rarely": 0, | |
| "BusinessTravel_Travel_Frequently": 1, | |
| "Department_Research": 0, | |
| "Department_Sales": 1, | |
| "EducationField_Life_Sciences": 0, | |
| "EducationField_Medical": 0, | |
| "EducationField_Marketing": 0, | |
| "EducationField_Other": 0, | |
| "EducationField_Technical_Degree": 1, | |
| "Gender_Male": 1, | |
| "JobRole_Research_Scientist": 0, | |
| "JobRole_Sales_Executive": 0, | |
| "JobRole_Laboratory_Technician": 0, | |
| "JobRole_Manufacturing_Director": 0, | |
| "JobRole_Healthcare_Representative": 0, | |
| "JobRole_Manager": 0, | |
| "JobRole_Sales_Representative": 1, | |
| "JobRole_Research_Director": 0, | |
| "MaritalStatus_Married": 0, | |
| "MaritalStatus_Single": 1, | |
| "OverTime_Yes": 1 | |
| } | |
| # Make the POST request | |
| try: | |
| response = requests.post(url, json=data) | |
| response.raise_for_status() # Raise an error for bad responses | |
| prediction = response.json() # Parse the JSON response | |
| # Check if the prediction contains the expected key and is a list | |
| if isinstance(prediction, dict) and 'prediction' in prediction and isinstance(prediction['prediction'], list): | |
| survival_probabilities = prediction['prediction'] | |
| else: | |
| raise ValueError("Unexpected response format: {}".format(prediction)) | |
| # Create a list of years based on the number of predictions | |
| years = list(range(1, len(survival_probabilities) + 1)) | |
| # Plot the data | |
| plt.figure(figsize=(10, 6)) | |
| plt.plot(years, survival_probabilities, marker='o', linestyle='-', color='b') | |
| plt.xlabel('Years') | |
| plt.ylabel('Survival Probability') | |
| plt.title('Employee Survival Probability Over Time') | |
| plt.grid(True) | |
| plt.xticks(years) | |
| plt.ylim(0, 1) | |
| # Show the plot | |
| plt.show() | |
| except requests.exceptions.RequestException as e: | |
| print("An error occurred while making the request:", e) | |
| except ValueError as ve: | |
| print("Value error:", ve) | |
| except Exception as ex: | |
| print("An unexpected error occurred:", ex) |