Spaces:
Sleeping
Sleeping
| import streamlit as st | |
| import cv2 | |
| from PIL import Image | |
| import numpy as np # <-- Add this line | |
| import io | |
| import tempfile | |
| import os | |
| import pickle | |
| import requests | |
| from datetime import datetime | |
| from googleapiclient.discovery import build | |
| from googleapiclient.http import MediaFileUpload | |
| from google.auth.transport.requests import Request | |
| from simple_salesforce import Salesforce | |
| import pytz | |
| # === SALESFORCE SETUP === | |
| sf = Salesforce( | |
| username='suriraja822@agentforce.com', | |
| password='Sati@1010', | |
| security_token='B9nS0HEyBE7YmWllqXCyiOJpY', | |
| domain='login' | |
| ) | |
| # === CONSTANTS === | |
| UPLOAD_FOLDER_ID = '1qkcR7nQTEtiMH9OFUv2bGxVn08E3dKjF' | |
| def get_vacant_rooms(): | |
| rooms = sf.query("SELECT Id, Name FROM Hotel_Room__c WHERE Room_Status__c = 'Vacant Clean'") | |
| return {room['Name']: room['Id'] for room in rooms['records']} | |
| def upload_image_to_drive(image_np, filename): | |
| creds = None | |
| if os.path.exists("token.pickle"): | |
| with open("token.pickle", "rb") as token: | |
| creds = pickle.load(token) | |
| if not creds or not creds.valid: | |
| if creds and creds.expired and creds.refresh_token: | |
| creds.refresh(Request()) | |
| service = build('drive', 'v3', credentials=creds) | |
| image = Image.fromarray(cv2.cvtColor(image_np, cv2.COLOR_BGR2RGB)) | |
| buf = io.BytesIO() | |
| image.save(buf, format="JPEG") | |
| buf.seek(0) | |
| with tempfile.NamedTemporaryFile(delete=False, suffix=".jpg") as tmp: | |
| tmp.write(buf.read()) | |
| tmp.flush() | |
| file_metadata = {'name': filename, 'parents': [UPLOAD_FOLDER_ID]} | |
| media = MediaFileUpload(tmp.name, mimetype='image/jpeg') | |
| uploaded_file = service.files().create(body=file_metadata, media_body=media, fields='id').execute() | |
| file_id = uploaded_file.get('id') | |
| os.remove(tmp.name) | |
| return f"https://drive.google.com/uc?id={file_id}" | |
| def save_guest_to_sf(data): | |
| try: | |
| sf.Hotel_Guest__c.create(data) | |
| st.success("β Guest registered successfully.") | |
| except Exception as e: | |
| st.error(f"β Salesforce Guest Error: {e}") | |
| def save_staff_to_sf(data): | |
| try: | |
| sf.Hotel_Staff__c.create(data) | |
| st.success("β Staff registered successfully.") | |
| except Exception as e: | |
| st.error(f"β Salesforce Staff Error: {e}") | |
| st.title("π¨ Hotel Guest and Staff Registration") | |
| tab1, tab2 = st.tabs(["π€ Guest", "π¨βπΌ Staff"]) | |
| with tab1: | |
| st.header("Register a Guest") | |
| fname = st.text_input("First Name") | |
| lname = st.text_input("Last Name") | |
| email = st.text_input("Email") | |
| phone = st.text_input("Phone") | |
| id_number = st.text_input("ID Number") | |
| special_requests = st.text_area("Special Requests") | |
| emergency_contact = st.text_input("Emergency Contact") | |
| vacant_rooms = get_vacant_rooms() | |
| room_name = st.selectbox("Assign Room", list(vacant_rooms.keys())) | |
| capture = st.camera_input("Capture Guest Photo") | |
| if st.button("π Register Guest"): | |
| if capture: | |
| timestamp = datetime.now(pytz.timezone('Asia/Kolkata')).strftime("%Y%m%d%H%M%S") | |
| image = Image.open(capture) | |
| image_np = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR) | |
| file_name = f"G_{timestamp}.jpg" | |
| photo_url = upload_image_to_drive(image_np, file_name) | |
| guest_data = { | |
| 'First_Name__c': fname, | |
| 'Last_Name__c': lname, | |
| 'Email__c': email, | |
| 'Phone__c': phone, | |
| 'ID_Number__c': id_number, | |
| 'Special_Requests__c': special_requests, | |
| 'Emergency_Contact__c': emergency_contact, | |
| 'Room_Number__c': vacant_rooms[room_name], | |
| 'Check_In_Date__c': datetime.utcnow().isoformat(), | |
| 'Photo_Uploaded__c': True, | |
| "Photo_URL__c":photo_url, | |
| 'HF_Registered__c': True, | |
| 'Guest_Status__c': 'Checked In' | |
| } | |
| save_guest_to_sf(guest_data) | |
| else: | |
| st.warning("πΈ Please capture a photo before registering.") | |
| with tab2: | |
| st.header("Register a Staff Member") | |
| fname = st.text_input("Staff First Name") | |
| lname = st.text_input("Staff Last Name") | |
| email = st.text_input("Staff Email") | |
| phone = st.text_input("Staff Phone") | |
| emp_number = st.text_input("Employee Number") | |
| department = st.text_input("Department") | |
| position = st.text_input("Position") | |
| shift = st.selectbox("Shift", ["Morning", "Evening", "Night"]) | |
| status = st.selectbox("Status", ["Active", "Inactive"]) | |
| access = st.selectbox("Access Level", ["Low", "Medium", "High"]) | |
| badge = st.text_input("Badge Number") | |
| supervisor = st.text_input("Supervisor") | |
| hire_date = st.date_input("Hire Date") | |
| capture = st.camera_input("Capture Staff Photo") | |
| if st.button("π Register Staff"): | |
| if capture: | |
| timestamp = datetime.now(pytz.timezone('Asia/Kolkata')).strftime("%Y%m%d%H%M%S") | |
| image = Image.open(capture) | |
| image_np = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR) | |
| file_name = f"S_{timestamp}.jpg" | |
| photo_url = upload_image_to_drive(image_np, file_name) | |
| staff_data = { | |
| 'First_Name__c': fname, | |
| 'Last_Name__c': lname, | |
| 'Email__c': email, | |
| 'Phone__c': phone, | |
| 'Employee_Number__c': emp_number, | |
| 'Department__c': department, | |
| 'Position__c': position, | |
| 'Shift__c': shift, | |
| 'Employment_Status__c': status, | |
| 'Access_Level__c': 'Basic', | |
| 'Badge_Number__c': badge, | |
| 'Supervisor__c': supervisor, | |
| 'Hire_Date__c': hire_date.isoformat(), | |
| 'Photo_Uploaded__c': True, | |
| 'photo_url__c':photo_url, | |
| 'HF_Registered__c': True | |
| } | |
| save_staff_to_sf(staff_data) | |
| else: | |
| st.warning("πΈ Please capture a photo before registering.") | |