SuriRaja commited on
Commit
6aa11d0
ยท
verified ยท
1 Parent(s): 75db724

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +125 -53
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
- try:
22
- id_bytes = guest_id_photo.read()
23
- id_np = np.array(Image.open(io.BytesIO(id_bytes)))
24
- id_url = upload_image_to_drive(id_np, f"GUEST_ID")
25
- st.write(f"โœ… ID photo uploaded: {id_url}")
26
- except Exception as e:
27
- st.error(f"โŒ Upload ID Photo Failed: {e}")
28
- st.stop()
 
 
 
29
 
 
30
  try:
31
- # 1. Update or Create Room
32
- st.write(f"๐Ÿ” Searching for room: {room_number}")
33
- rooms = sf.query_all(f"SELECT Id FROM Room__c WHERE Name = '{room_number}'")
34
- if rooms['totalSize'] == 0:
35
- st.write("๐Ÿ—๏ธ Creating new Room record...")
36
- room_rec = sf.Room__c.create({'Name': room_number})
37
- room_id = room_rec['id']
38
- st.write(f"โœ… Room created with ID: {room_id}")
39
- else:
40
- room_id = rooms['records'][0]['Id']
41
- st.write(f"๐Ÿจ Found existing Room ID: {room_id}")
 
 
 
42
  except Exception as e:
43
- st.error(f"โŒ Room handling failed: {e}")
44
- st.stop()
 
45
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
46
  try:
47
- # 2. Create Guest record
48
- st.write("๐Ÿ“ค Attempting to create Guest__c record...")
49
- guest_data = {
50
- 'Name': guest_name,
51
- 'Mobile__c': guest_mobile,
52
- 'Email__c': guest_email,
53
- 'Room__c': room_id,
54
- 'Image_Link__c': img_url,
55
- 'ID_Proof_Link__c': id_url
56
- }
57
- result = sf.Guest__c.create(guest_data)
58
- st.write(f"โœ… Guest creation result: {result}")
59
- st.success("๐ŸŽ‰ Guest registered successfully!")
60
  except Exception as e:
61
- st.error(f"โŒ Guest creation failed: {e}")
62
  else:
63
- st.warning("โš ๏ธ Please complete all fields and capture/upload images.")
 
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.")