Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import PyPDF2 | |
| import numpy as np | |
| from simple_salesforce import Salesforce | |
| import matplotlib.pyplot as plt | |
| import io | |
| import os | |
| # Salesforce credentials (replace with your own) | |
| SF_USERNAME = "'prudhvilathat@sandbox.com'" | |
| SF_PASSWORD = "varnika@143" | |
| SF_SECURITY_TOKEN = "TAVrr05thdo1vWYwMgyGR36OO" | |
| def forecast_labour_shortage(pdf_file, project_id): | |
| # Validate inputs | |
| if not pdf_file or not project_id: | |
| return "Error: Please upload a PDF and enter a Project ID.", None | |
| # Validate Project ID format (mock validation) | |
| if not project_id.startswith('A0'): | |
| return "Error: Invalid Project ID format. It should start with 'A0'.", None | |
| # Save the uploaded PDF temporarily | |
| pdf_path = 'temp.pdf' | |
| with open(pdf_path, 'wb') as f: | |
| f.write(pdf_file.read()) | |
| # Extract text from PDF | |
| try: | |
| with open(pdf_path, 'rb') as file: | |
| pdf_reader = PyPDF2.PdfReader(file) | |
| text = '' | |
| for page in pdf_reader.pages: | |
| text += page.extract_text() or '' | |
| except Exception as e: | |
| os.remove(pdf_path) | |
| return f"Error: Failed to parse PDF: {str(e)}", None | |
| # Clean up | |
| os.remove(pdf_path) | |
| # Mock forecasting logic (replace with actual logic) | |
| attendance_rate = np.random.uniform(0.7, 0.95) # Mock attendance rate | |
| shortage = (1 - attendance_rate) * 100 # Shortage percentage | |
| # Salesforce integration | |
| try: | |
| sf = Salesforce( | |
| username=SF_USERNAME, | |
| password=SF_PASSWORD, | |
| security_token=SF_SECURITY_TOKEN, | |
| domain='login' # Use 'test' for sandbox, 'login' for production | |
| ) | |
| sf_record = { | |
| 'Project_ID__c': project_id, | |
| 'Forecasted_Shortage__c': shortage | |
| } | |
| # Replace 'Custom_Object__c' with your Salesforce object | |
| # sf.Custom_Object__c.create(sf_record) | |
| except Exception as e: | |
| return f"Error: Failed to authenticate with Salesforce: {str(e)}", None | |
| # Generate chart using Matplotlib | |
| fig, ax = plt.subplots() | |
| categories = ['Attendance', 'Shortage'] | |
| values = [attendance_rate * 100, shortage] | |
| ax.bar(categories, values, color=['#36a2eb', '#ff6384']) | |
| ax.set_ylim(0, 100) | |
| ax.set_ylabel('Percentage') | |
| ax.set_title('Labour Attendance Forecast') | |
| plt.tight_layout() | |
| # Save the plot to a buffer | |
| buf = io.BytesIO() | |
| plt.savefig(buf, format='png') | |
| buf.seek(0) | |
| plt.close() | |
| # Prepare the result text | |
| result_text = f"Attendance Rate: {round(attendance_rate * 100, 2)}% | Forecasted Shortage: {round(shortage, 2)}%" | |
| return result_text, buf | |
| # Create Gradio interface | |
| interface = gr.Interface( | |
| fn=forecast_labour_shortage, | |
| inputs=[ | |
| gr.File(label="Upload PDF", type="binary"), | |
| gr.Textbox(label="Project ID", placeholder="Enter Salesforce Project ID (e.g., A0xxxxxxxxxxxxx)") | |
| ], | |
| outputs=[ | |
| gr.Textbox(label="Forecast Result"), | |
| gr.Image(label="Forecast Chart") | |
| ], | |
| title="Labour Attendance Forecast", | |
| description="This app forecasts labour shortages based on attendance logs, trends, and other factors extracted from uploaded PDFs. It also creates a record in Salesforce and displays a chart of historical and forecasted headcount." | |
| ) | |
| # Launch the interface | |
| if __name__ == "__main__": | |
| interface.launch() |