Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,63 +1,135 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
with tab1:
|
| 2 |
st.subheader("Guest Registration")
|
| 3 |
-
guest_name = st.text_input("Full Name")
|
| 4 |
-
guest_mobile = st.text_input("Mobile Number")
|
| 5 |
-
guest_email = st.text_input("Email")
|
| 6 |
-
guest_id_photo = st.file_uploader("Upload ID Proof", type=["jpg", "jpeg", "png"])
|
| 7 |
-
room_number = st.text_input("Assign Room Number")
|
| 8 |
-
guest_cam = st.camera_input("Capture Guest Photo")
|
| 9 |
-
|
| 10 |
-
if st.button("๐ฅ Register Guest"):
|
| 11 |
-
if guest_cam and guest_id_photo and guest_name and room_number:
|
| 12 |
-
st.write("๐งช Starting image conversion...")
|
| 13 |
-
image_np = np.array(Image.open(guest_cam))
|
| 14 |
-
try:
|
| 15 |
-
img_url = upload_image_to_drive(image_np, f"GUEST")
|
| 16 |
-
st.write(f"โ
Guest photo uploaded: {img_url}")
|
| 17 |
-
except Exception as e:
|
| 18 |
-
st.error(f"โ Upload Guest Image Failed: {e}")
|
| 19 |
-
st.stop()
|
| 20 |
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
| 29 |
|
|
|
|
| 30 |
try:
|
| 31 |
-
|
| 32 |
-
st.
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
| 42 |
except Exception as e:
|
| 43 |
-
st.error(f"
|
| 44 |
-
|
|
|
|
| 45 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
try:
|
| 47 |
-
|
| 48 |
-
st.
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
}
|
| 57 |
-
|
| 58 |
-
st.
|
| 59 |
-
st.
|
| 60 |
except Exception as e:
|
| 61 |
-
st.error(f"
|
| 62 |
else:
|
| 63 |
-
st.warning("
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import uuid
|
| 3 |
+
import cv2
|
| 4 |
+
import numpy as np
|
| 5 |
+
from PIL import Image
|
| 6 |
+
import tempfile
|
| 7 |
+
import pickle
|
| 8 |
+
import os
|
| 9 |
+
from datetime import datetime
|
| 10 |
+
from googleapiclient.discovery import build
|
| 11 |
+
from googleapiclient.http import MediaFileUpload
|
| 12 |
+
from google.auth.transport.requests import Request
|
| 13 |
+
from simple_salesforce import Salesforce
|
| 14 |
+
|
| 15 |
+
# === Google Drive Setup ===
|
| 16 |
+
REG_FOLDER_ID = '1qkcR7nQTEtiMH9OFUv2bGxVn08E3dKjF' # Registered images folder
|
| 17 |
+
INTRUDER_FOLDER_ID = '1PPAUWU-wMx7fek73p-hqPqYQypYtG8Ob' # Intruder folder
|
| 18 |
+
|
| 19 |
+
# === Salesforce Setup ===
|
| 20 |
+
sf = Salesforce(
|
| 21 |
+
username='suriraja822@agentforce.com',
|
| 22 |
+
password='Sati@1010',
|
| 23 |
+
security_token='B9nS0HEyBE7YmWllqXCyiOJpY',
|
| 24 |
+
domain='login'
|
| 25 |
+
)
|
| 26 |
+
|
| 27 |
+
def upload_to_drive(image, filename, folder_id):
|
| 28 |
+
creds = None
|
| 29 |
+
if os.path.exists("token.pickle"):
|
| 30 |
+
with open("token.pickle", "rb") as token:
|
| 31 |
+
creds = pickle.load(token)
|
| 32 |
+
if creds and creds.expired and creds.refresh_token:
|
| 33 |
+
creds.refresh(Request())
|
| 34 |
+
service = build('drive', 'v3', credentials=creds)
|
| 35 |
+
|
| 36 |
+
image_pil = Image.fromarray(image)
|
| 37 |
+
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".jpg")
|
| 38 |
+
image_pil.save(temp_file.name)
|
| 39 |
+
|
| 40 |
+
file_metadata = {'name': filename, 'parents': [folder_id]}
|
| 41 |
+
media = MediaFileUpload(temp_file.name, mimetype='image/jpeg')
|
| 42 |
+
file = service.files().create(body=file_metadata, media_body=media, fields='id').execute()
|
| 43 |
+
|
| 44 |
+
file_id = file.get('id')
|
| 45 |
+
public_link = f"https://drive.google.com/uc?id={file_id}"
|
| 46 |
+
os.remove(temp_file.name)
|
| 47 |
+
|
| 48 |
+
# Make file public
|
| 49 |
+
try:
|
| 50 |
+
service.permissions().create(
|
| 51 |
+
fileId=file_id,
|
| 52 |
+
body={'role': 'reader', 'type': 'anyone'}
|
| 53 |
+
).execute()
|
| 54 |
+
except Exception as e:
|
| 55 |
+
st.warning(f"Failed to set permission: {e}")
|
| 56 |
+
|
| 57 |
+
return public_link
|
| 58 |
+
|
| 59 |
+
# === Streamlit UI ===
|
| 60 |
+
st.set_page_config(page_title="Hotel Registration", layout="wide")
|
| 61 |
+
st.title("๐ธ Hotel Guest & Staff Registration")
|
| 62 |
+
|
| 63 |
+
tab1, tab2 = st.tabs(["๐๏ธ Guest Registration", "๐ฎ Staff Registration"])
|
| 64 |
+
|
| 65 |
+
# === Guest Tab ===
|
| 66 |
with tab1:
|
| 67 |
st.subheader("Guest Registration")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
+
guest_name = st.text_input("Guest Name")
|
| 70 |
+
guest_aadhar = st.text_input("Aadhar Number")
|
| 71 |
+
guest_phone = st.text_input("Phone Number")
|
| 72 |
+
guest_room = st.text_input("Room Number")
|
| 73 |
+
|
| 74 |
+
id_photo = st.file_uploader("Upload ID Card Photo", type=["jpg", "jpeg", "png"])
|
| 75 |
+
guest_image = st.camera_input("Capture Guest Photo")
|
| 76 |
+
|
| 77 |
+
if st.button("โ
Register Guest"):
|
| 78 |
+
if guest_image and id_photo and all([guest_name, guest_aadhar, guest_phone, guest_room]):
|
| 79 |
+
guest_np = np.array(Image.open(guest_image).convert('RGB'))
|
| 80 |
|
| 81 |
+
filename = f"G_{datetime.now().strftime('%Y%m%d_%H%M%S')}.jpg"
|
| 82 |
try:
|
| 83 |
+
image_link = upload_to_drive(guest_np, filename, REG_FOLDER_ID)
|
| 84 |
+
st.image(guest_np, caption="Guest Registered")
|
| 85 |
+
|
| 86 |
+
result = sf.Guest__c.create({
|
| 87 |
+
"Name": guest_name,
|
| 88 |
+
"Aadhar_Number__c": guest_aadhar,
|
| 89 |
+
"Phone_Number__c": guest_phone,
|
| 90 |
+
"Room_Number__c": guest_room,
|
| 91 |
+
"Check_In__c": datetime.utcnow().isoformat(),
|
| 92 |
+
"Image_Link__c": image_link
|
| 93 |
+
})
|
| 94 |
+
|
| 95 |
+
st.success("๐ Guest Registered in Salesforce!")
|
| 96 |
+
st.json(result)
|
| 97 |
except Exception as e:
|
| 98 |
+
st.error(f"Salesforce Error: {e}")
|
| 99 |
+
else:
|
| 100 |
+
st.warning("๐ Please fill all fields and upload both photos.")
|
| 101 |
|
| 102 |
+
# === Staff Tab ===
|
| 103 |
+
with tab2:
|
| 104 |
+
st.subheader("Staff Registration")
|
| 105 |
+
|
| 106 |
+
staff_name = st.text_input("Staff Name")
|
| 107 |
+
staff_id = st.text_input("Staff ID")
|
| 108 |
+
staff_dept = st.selectbox("Department", ["Security", "Reception", "Housekeeping", "Maintenance", "Kitchen"])
|
| 109 |
+
|
| 110 |
+
id_card = st.file_uploader("Upload ID Card (Staff)", type=["jpg", "jpeg", "png"], key="staff")
|
| 111 |
+
staff_image = st.camera_input("Capture Staff Photo")
|
| 112 |
+
|
| 113 |
+
if st.button("โ
Register Staff"):
|
| 114 |
+
if staff_image and id_card and all([staff_name, staff_id, staff_dept]):
|
| 115 |
+
staff_np = np.array(Image.open(staff_image).convert('RGB'))
|
| 116 |
+
|
| 117 |
+
filename = f"S_{datetime.now().strftime('%Y%m%d_%H%M%S')}.jpg"
|
| 118 |
try:
|
| 119 |
+
image_link = upload_to_drive(staff_np, filename, REG_FOLDER_ID)
|
| 120 |
+
st.image(staff_np, caption="Staff Registered")
|
| 121 |
+
|
| 122 |
+
result = sf.Staff__c.create({
|
| 123 |
+
"Name": staff_name,
|
| 124 |
+
"Staff_ID__c": staff_id,
|
| 125 |
+
"Department__c": staff_dept,
|
| 126 |
+
"Registered_On__c": datetime.utcnow().isoformat(),
|
| 127 |
+
"Image_Link__c": image_link
|
| 128 |
+
})
|
| 129 |
+
|
| 130 |
+
st.success("โ
Staff Registered in Salesforce!")
|
| 131 |
+
st.json(result)
|
| 132 |
except Exception as e:
|
| 133 |
+
st.error(f"Salesforce Error: {e}")
|
| 134 |
else:
|
| 135 |
+
st.warning("๐ Please fill all fields and upload both photos.")
|