Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -2,21 +2,14 @@ import gradio as gr
|
|
2 |
import pandas as pd
|
3 |
from datetime import datetime
|
4 |
import os
|
5 |
-
from huggingface_hub import
|
6 |
|
7 |
-
#
|
8 |
-
|
9 |
-
|
|
|
10 |
|
11 |
-
#
|
12 |
-
repo = Repository(local_dir="temp_repo", clone_from="Abu1998/DataCollection", use_auth_token=HF_TOKEN)
|
13 |
-
|
14 |
-
|
15 |
-
# Authenticate with Hugging Face Hub
|
16 |
-
api = HfApi()
|
17 |
-
HfFolder.save_token(HF_TOKEN)
|
18 |
-
|
19 |
-
# Ensure the CSV file exists
|
20 |
if not os.path.exists(STORAGE_PATH):
|
21 |
df = pd.DataFrame(columns=[
|
22 |
"Date", "Appointment", "Appointment Timing", "Services", "Products",
|
@@ -24,59 +17,63 @@ if not os.path.exists(STORAGE_PATH):
|
|
24 |
])
|
25 |
df.to_csv(STORAGE_PATH, index=False)
|
26 |
|
27 |
-
# Function to upload CSV to Hugging Face Hub
|
28 |
def upload_to_huggingface():
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
|
|
|
|
|
|
38 |
|
39 |
-
# Function to save data locally and upload
|
40 |
def save_and_upload_to_csv(appointment_timing, services, products, contact, customer_name, rating, location, key_points, price):
|
41 |
-
|
42 |
-
|
|
|
43 |
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
# Append and save locally
|
64 |
-
df = pd.concat([df, new_row], ignore_index=True)
|
65 |
-
df.to_csv(STORAGE_PATH, index=False)
|
66 |
|
67 |
-
|
68 |
-
|
|
|
69 |
|
70 |
-
|
|
|
|
|
|
|
|
|
71 |
|
72 |
# Gradio Interface
|
73 |
with gr.Blocks() as app:
|
74 |
gr.Markdown("# Appointment Data Storage Application")
|
75 |
appointment_timing = gr.Textbox(label="Appointment Timing")
|
76 |
-
services = gr.
|
77 |
label="Services",
|
78 |
-
choices=["Full arm Rica", "Full leg", "Underarms", "Eyebrow", "Upper lips"]
|
79 |
-
multiselect=True
|
80 |
)
|
81 |
products = gr.Textbox(label="Products")
|
82 |
contact = gr.Textbox(label="Contact")
|
|
|
2 |
import pandas as pd
|
3 |
from datetime import datetime
|
4 |
import os
|
5 |
+
from huggingface_hub import Repository
|
6 |
|
7 |
+
# Constants
|
8 |
+
HF_TOKEN = os.getenv("HF_TOKEN") # Retrieve token from environment variables
|
9 |
+
STORAGE_PATH = "appointments.csv" # Local CSV file path
|
10 |
+
REPO_ID = "Abu1998/DataCollection" # Your Hugging Face dataset repo
|
11 |
|
12 |
+
# Ensure the CSV file exists locally
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
if not os.path.exists(STORAGE_PATH):
|
14 |
df = pd.DataFrame(columns=[
|
15 |
"Date", "Appointment", "Appointment Timing", "Services", "Products",
|
|
|
17 |
])
|
18 |
df.to_csv(STORAGE_PATH, index=False)
|
19 |
|
20 |
+
# Function to upload the CSV to Hugging Face Hub
|
21 |
def upload_to_huggingface():
|
22 |
+
try:
|
23 |
+
repo = Repository(local_dir="temp_repo", clone_from=REPO_ID, use_auth_token=HF_TOKEN)
|
24 |
+
repo.git_pull() # Pull the latest changes
|
25 |
+
os.makedirs("temp_repo", exist_ok=True)
|
26 |
+
df = pd.read_csv(STORAGE_PATH)
|
27 |
+
df.to_csv(os.path.join("temp_repo", "appointments.csv"), index=False)
|
28 |
+
repo.git_add("appointments.csv")
|
29 |
+
repo.git_commit("Updated appointments data")
|
30 |
+
repo.git_push()
|
31 |
+
return "Data uploaded to Hugging Face successfully!"
|
32 |
+
except Exception as e:
|
33 |
+
return f"Error uploading data: {e}"
|
34 |
|
35 |
+
# Function to save data locally and upload it
|
36 |
def save_and_upload_to_csv(appointment_timing, services, products, contact, customer_name, rating, location, key_points, price):
|
37 |
+
try:
|
38 |
+
# Load existing data
|
39 |
+
df = pd.read_csv(STORAGE_PATH)
|
40 |
|
41 |
+
# Auto-detect date and appointment ID
|
42 |
+
date = datetime.now().strftime("%Y-%m-%d")
|
43 |
+
appointment = len(df) + 1
|
44 |
|
45 |
+
# Add a new row
|
46 |
+
new_row = pd.DataFrame([{
|
47 |
+
"Date": date,
|
48 |
+
"Appointment": appointment,
|
49 |
+
"Appointment Timing": appointment_timing,
|
50 |
+
"Services": ', '.join(services) if isinstance(services, list) else services,
|
51 |
+
"Products": products,
|
52 |
+
"Contact": contact,
|
53 |
+
"Customer Name": customer_name,
|
54 |
+
"Rating": rating,
|
55 |
+
"Location": location,
|
56 |
+
"Key-points": key_points,
|
57 |
+
"Price": price
|
58 |
+
}])
|
|
|
|
|
|
|
|
|
59 |
|
60 |
+
# Append and save locally
|
61 |
+
df = pd.concat([df, new_row], ignore_index=True)
|
62 |
+
df.to_csv(STORAGE_PATH, index=False)
|
63 |
|
64 |
+
# Upload to Hugging Face
|
65 |
+
upload_message = upload_to_huggingface()
|
66 |
+
return f"Appointment {appointment} saved locally and uploaded! {upload_message}"
|
67 |
+
except Exception as e:
|
68 |
+
return f"Error saving data: {e}"
|
69 |
|
70 |
# Gradio Interface
|
71 |
with gr.Blocks() as app:
|
72 |
gr.Markdown("# Appointment Data Storage Application")
|
73 |
appointment_timing = gr.Textbox(label="Appointment Timing")
|
74 |
+
services = gr.CheckboxGroup(
|
75 |
label="Services",
|
76 |
+
choices=["Full arm Rica", "Full leg", "Underarms", "Eyebrow", "Upper lips"]
|
|
|
77 |
)
|
78 |
products = gr.Textbox(label="Products")
|
79 |
contact = gr.Textbox(label="Contact")
|