Hidayatmahar commited on
Commit
1bea3bd
·
verified ·
1 Parent(s): 1ecf220

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -43
app.py CHANGED
@@ -1,55 +1,56 @@
1
  import gradio as gr
2
- import os
3
  import pandas as pd
4
- from datetime import datetime
5
- import uuid
6
-
7
- # File to store allotment data
8
- CSV_FILE = "hostel_allotments.csv"
9
-
10
- # Create CSV if not exists
11
- if not os.path.exists(CSV_FILE):
12
- df = pd.DataFrame(columns=["Allotment_ID", "Name", "Roll_No", "Department", "Hostel", "Room_Type", "Bed_No", "Date_Time"])
13
- df.to_csv(CSV_FILE, index=False)
14
-
15
- # Function for hostel allotment
16
- def allot_hostel(name, roll_no, department, hostel, room_type, bed_no):
17
- allotment_id = str(uuid.uuid4())[:8] # unique short ID
18
- date_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
19
-
20
- # Save to CSV
21
- df = pd.read_csv(CSV_FILE)
22
- new_entry = {
23
- "Allotment_ID": allotment_id,
24
- "Name": name,
25
- "Roll_No": roll_no,
26
- "Department": department,
27
- "Hostel": hostel,
28
- "Room_Type": room_type,
29
- "Bed_No": bed_no,
30
- "Date_Time": date_time
31
- }
32
- df = pd.concat([df, pd.DataFrame([new_entry])], ignore_index=True)
33
- df.to_csv(CSV_FILE, index=False)
34
-
35
- return f"✅ Hostel Allotted Successfully!\n\nAllotment ID: {allotment_id}\nName: {name}\nRoll No: {roll_no}\nDepartment: {department}\nHostel: {hostel}\nRoom Type: {room_type}\nBed No: {bed_no}\nDate & Time: {date_time}"
36
-
37
- # UI
 
 
38
  with gr.Blocks() as demo:
39
- gr.Markdown("## 🏨 Hostel Allotment System")
40
 
41
  with gr.Row():
42
  name = gr.Textbox(label="Student Name")
43
  roll_no = gr.Textbox(label="Roll Number")
44
- department = gr.Textbox(label="Department")
45
 
46
- hostel = gr.Dropdown(["Hostel A", "Hostel B", "Hostel C"], label="Select Hostel")
47
- room_type = gr.Dropdown(["Single", "Double", "Triple"], label="Select Room Type")
48
- bed_no = gr.Dropdown([f"Bed {i}" for i in range(1, 11)], label="Select Bed No")
49
 
50
- submit_btn = gr.Button("Allot Hostel")
51
- output = gr.Textbox(label="Allotment Details", lines=10)
52
 
53
- submit_btn.click(fn=allot_hostel, inputs=[name, roll_no, department, hostel, room_type, bed_no], outputs=output)
 
54
 
55
  demo.launch()
 
1
  import gradio as gr
 
2
  import pandas as pd
3
+ import random
4
+
5
+ # Hostels and rooms
6
+ hostels = ["Hostel A", "Hostel B", "Hostel C"]
7
+ rooms = list(range(1, 21)) # 20 rooms per hostel
8
+
9
+ # DataFrame to store allotments
10
+ allotments = pd.DataFrame(columns=["Name", "Roll No", "Father's Name", "Hostel", "Room No"])
11
+
12
+ # Function to allot hostel and room
13
+ def allot_hostel(name, roll_no, father_name):
14
+ global allotments
15
+
16
+ # Check if already allotted
17
+ existing = allotments[allotments["Roll No"] == roll_no]
18
+ if not existing.empty:
19
+ return f"Already allotted: {existing.iloc[0]['Hostel']} - Room {existing.iloc[0]['Room No']}"
20
+
21
+ # Random hostel & room
22
+ hostel = random.choice(hostels)
23
+ room = random.choice(rooms)
24
+
25
+ # Save in DataFrame
26
+ new_row = pd.DataFrame([[name, roll_no, father_name, hostel, room]],
27
+ columns=allotments.columns)
28
+ allotments = pd.concat([allotments, new_row], ignore_index=True)
29
+
30
+ return f"Allotted: {hostel}, Room {room}"
31
+
32
+ # Function to show all allotments
33
+ def show_allotments():
34
+ if allotments.empty:
35
+ return "No allotments yet."
36
+ return allotments
37
+
38
+ # Gradio UI
39
  with gr.Blocks() as demo:
40
+ gr.Markdown("## 🏠 Hostel Allotment System")
41
 
42
  with gr.Row():
43
  name = gr.Textbox(label="Student Name")
44
  roll_no = gr.Textbox(label="Roll Number")
45
+ father_name = gr.Textbox(label="Father's Name")
46
 
47
+ allot_btn = gr.Button("Allot Hostel")
48
+ output = gr.Textbox(label="Allotment Result")
 
49
 
50
+ show_btn = gr.Button("Show Allotted Data")
51
+ table = gr.Dataframe(label="Allotment Records")
52
 
53
+ allot_btn.click(allot_hostel, inputs=[name, roll_no, father_name], outputs=output)
54
+ show_btn.click(show_allotments, outputs=table)
55
 
56
  demo.launch()