Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,28 +2,41 @@ import gradio as gr
|
|
| 2 |
import json
|
| 3 |
import os
|
| 4 |
from datetime import datetime, timedelta
|
|
|
|
| 5 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
SLOTS_FILE = "/tmp/slots.json"
|
| 7 |
BOOKINGS_FILE = "/tmp/bookings.json"
|
| 8 |
|
| 9 |
-
# -------------------------
|
| 10 |
-
# INIT
|
| 11 |
-
# -------------------------
|
| 12 |
def init_slots():
|
| 13 |
if os.path.exists(SLOTS_FILE):
|
| 14 |
with open(SLOTS_FILE) as f:
|
| 15 |
return json.load(f)
|
| 16 |
|
| 17 |
-
# generate next 5 days, 3 slots/day
|
| 18 |
slots = []
|
| 19 |
-
base = datetime.now().replace(hour=11, minute=0, second=0)
|
| 20 |
-
|
| 21 |
-
|
|
|
|
| 22 |
slot = (base + timedelta(days=d)).replace(hour=h)
|
| 23 |
slots.append(slot.strftime("%Y-%m-%d %H:%M"))
|
| 24 |
|
| 25 |
with open(SLOTS_FILE, "w") as f:
|
| 26 |
-
json.dump(slots, f)
|
| 27 |
|
| 28 |
return slots
|
| 29 |
|
|
@@ -37,39 +50,75 @@ def load_bookings():
|
|
| 37 |
|
| 38 |
AVAILABLE_SLOTS = init_slots()
|
| 39 |
|
| 40 |
-
# -------------------------
|
| 41 |
-
#
|
| 42 |
-
# -------------------------
|
| 43 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
if not slot or not name or not email:
|
| 45 |
return gr.update(), "β Please fill all fields"
|
| 46 |
|
| 47 |
if slot not in AVAILABLE_SLOTS:
|
| 48 |
return gr.update(), "β Slot already booked"
|
| 49 |
|
|
|
|
| 50 |
AVAILABLE_SLOTS.remove(slot)
|
| 51 |
|
| 52 |
with open(SLOTS_FILE, "w") as f:
|
| 53 |
-
json.dump(AVAILABLE_SLOTS, f)
|
| 54 |
|
| 55 |
bookings = load_bookings()
|
| 56 |
bookings.append({
|
| 57 |
"slot": slot,
|
| 58 |
"name": name,
|
| 59 |
"email": email,
|
|
|
|
| 60 |
})
|
| 61 |
|
| 62 |
with open(BOOKINGS_FILE, "w") as f:
|
| 63 |
-
json.dump(bookings, f)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
|
| 65 |
-
return
|
|
|
|
|
|
|
|
|
|
| 66 |
|
| 67 |
-
# -------------------------
|
| 68 |
# UI
|
| 69 |
-
# -------------------------
|
| 70 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
gr.Markdown("## π
Book a Meeting with OhamLab")
|
| 72 |
-
gr.Markdown("
|
| 73 |
|
| 74 |
slot_dd = gr.Dropdown(
|
| 75 |
choices=AVAILABLE_SLOTS,
|
|
@@ -83,9 +132,12 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 83 |
status = gr.Markdown()
|
| 84 |
|
| 85 |
book_btn.click(
|
| 86 |
-
|
| 87 |
inputs=[slot_dd, name, email],
|
| 88 |
outputs=[slot_dd, status]
|
| 89 |
)
|
| 90 |
|
| 91 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import json
|
| 3 |
import os
|
| 4 |
from datetime import datetime, timedelta
|
| 5 |
+
from huggingface_hub import HfApi, upload_file
|
| 6 |
|
| 7 |
+
# --------------------------------------------------
|
| 8 |
+
# HF CONFIG
|
| 9 |
+
# --------------------------------------------------
|
| 10 |
+
HF_REPO_ID = "rahul7star/ohamlab-pro"
|
| 11 |
+
HF_REPO_TYPE = "model"
|
| 12 |
+
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 13 |
+
|
| 14 |
+
api = HfApi(token=HF_TOKEN)
|
| 15 |
+
|
| 16 |
+
# --------------------------------------------------
|
| 17 |
+
# FILE PATHS
|
| 18 |
+
# --------------------------------------------------
|
| 19 |
SLOTS_FILE = "/tmp/slots.json"
|
| 20 |
BOOKINGS_FILE = "/tmp/bookings.json"
|
| 21 |
|
| 22 |
+
# --------------------------------------------------
|
| 23 |
+
# INIT SLOTS (RUN ONCE)
|
| 24 |
+
# --------------------------------------------------
|
| 25 |
def init_slots():
|
| 26 |
if os.path.exists(SLOTS_FILE):
|
| 27 |
with open(SLOTS_FILE) as f:
|
| 28 |
return json.load(f)
|
| 29 |
|
|
|
|
| 30 |
slots = []
|
| 31 |
+
base = datetime.now().replace(hour=11, minute=0, second=0, microsecond=0)
|
| 32 |
+
|
| 33 |
+
for d in range(5): # next 5 days
|
| 34 |
+
for h in [11, 15, 18]: # 3 slots per day
|
| 35 |
slot = (base + timedelta(days=d)).replace(hour=h)
|
| 36 |
slots.append(slot.strftime("%Y-%m-%d %H:%M"))
|
| 37 |
|
| 38 |
with open(SLOTS_FILE, "w") as f:
|
| 39 |
+
json.dump(slots, f, indent=2)
|
| 40 |
|
| 41 |
return slots
|
| 42 |
|
|
|
|
| 50 |
|
| 51 |
AVAILABLE_SLOTS = init_slots()
|
| 52 |
|
| 53 |
+
# --------------------------------------------------
|
| 54 |
+
# UPLOAD JSON TO HF HUB
|
| 55 |
+
# --------------------------------------------------
|
| 56 |
+
def upload_to_hf(local_path, repo_path):
|
| 57 |
+
upload_file(
|
| 58 |
+
path_or_fileobj=local_path,
|
| 59 |
+
path_in_repo=repo_path,
|
| 60 |
+
repo_id=HF_REPO_ID,
|
| 61 |
+
repo_type=HF_REPO_TYPE,
|
| 62 |
+
token=HF_TOKEN,
|
| 63 |
+
commit_message=f"Update {repo_path}"
|
| 64 |
+
)
|
| 65 |
+
|
| 66 |
+
# --------------------------------------------------
|
| 67 |
+
# BOOKING LOGIC
|
| 68 |
+
# --------------------------------------------------
|
| 69 |
+
def book_slot(slot, name, email):
|
| 70 |
if not slot or not name or not email:
|
| 71 |
return gr.update(), "β Please fill all fields"
|
| 72 |
|
| 73 |
if slot not in AVAILABLE_SLOTS:
|
| 74 |
return gr.update(), "β Slot already booked"
|
| 75 |
|
| 76 |
+
# remove slot
|
| 77 |
AVAILABLE_SLOTS.remove(slot)
|
| 78 |
|
| 79 |
with open(SLOTS_FILE, "w") as f:
|
| 80 |
+
json.dump(AVAILABLE_SLOTS, f, indent=2)
|
| 81 |
|
| 82 |
bookings = load_bookings()
|
| 83 |
bookings.append({
|
| 84 |
"slot": slot,
|
| 85 |
"name": name,
|
| 86 |
"email": email,
|
| 87 |
+
"booked_at": datetime.utcnow().isoformat()
|
| 88 |
})
|
| 89 |
|
| 90 |
with open(BOOKINGS_FILE, "w") as f:
|
| 91 |
+
json.dump(bookings, f, indent=2)
|
| 92 |
+
|
| 93 |
+
# upload both files
|
| 94 |
+
upload_to_hf(SLOTS_FILE, "slots.json")
|
| 95 |
+
upload_to_hf(BOOKINGS_FILE, "bookings.json")
|
| 96 |
|
| 97 |
+
return (
|
| 98 |
+
gr.update(choices=AVAILABLE_SLOTS, value=None),
|
| 99 |
+
"β
Booking confirmed! Weβll contact you shortly."
|
| 100 |
+
)
|
| 101 |
|
| 102 |
+
# --------------------------------------------------
|
| 103 |
# UI
|
| 104 |
+
# --------------------------------------------------
|
| 105 |
+
CSS = """
|
| 106 |
+
footer, .gradio-footer { display: none !important; }
|
| 107 |
+
#ohamlab-footer {
|
| 108 |
+
position: fixed;
|
| 109 |
+
bottom: 0;
|
| 110 |
+
width: 100%;
|
| 111 |
+
text-align: center;
|
| 112 |
+
padding: 8px;
|
| 113 |
+
font-size: 12px;
|
| 114 |
+
background: #f8f9fb;
|
| 115 |
+
border-top: 1px solid #e5e7eb;
|
| 116 |
+
}
|
| 117 |
+
"""
|
| 118 |
+
|
| 119 |
+
with gr.Blocks(css=CSS, theme=gr.themes.Soft()) as demo:
|
| 120 |
gr.Markdown("## π
Book a Meeting with OhamLab")
|
| 121 |
+
gr.Markdown("Select an available slot and confirm instantly.")
|
| 122 |
|
| 123 |
slot_dd = gr.Dropdown(
|
| 124 |
choices=AVAILABLE_SLOTS,
|
|
|
|
| 132 |
status = gr.Markdown()
|
| 133 |
|
| 134 |
book_btn.click(
|
| 135 |
+
book_slot,
|
| 136 |
inputs=[slot_dd, name, email],
|
| 137 |
outputs=[slot_dd, status]
|
| 138 |
)
|
| 139 |
|
| 140 |
+
gr.HTML("<div id='ohamlab-footer'>Β© OhamLab</div>")
|
| 141 |
+
|
| 142 |
+
demo.queue()
|
| 143 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|