Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -2,78 +2,88 @@ import gradio as gr
|
|
2 |
import pandas as pd
|
3 |
from datetime import datetime
|
4 |
import os
|
5 |
-
from huggingface_hub import Repository
|
6 |
|
7 |
-
#
|
8 |
-
HF_TOKEN =
|
9 |
-
STORAGE_PATH = "appointments.csv" # Local CSV file path
|
10 |
-
REPO_ID = "/huggingface.co/datasets/Abu1998/DataCollection" # Your Hugging Face dataset repo
|
11 |
|
12 |
-
#
|
13 |
-
|
14 |
-
df = pd.DataFrame(columns=[
|
15 |
-
"Date", "Appointment", "Appointment Timing", "Services", "Products",
|
16 |
-
"Contact", "Customer Name", "Rating", "Location", "Key-points", "Price"
|
17 |
-
])
|
18 |
-
df.to_csv(STORAGE_PATH, index=False)
|
19 |
|
20 |
-
#
|
|
|
|
|
|
|
|
|
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() #
|
|
|
|
|
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
|
36 |
def save_and_upload_to_csv(appointment_timing, services, products, contact, customer_name, rating, location, key_points, price):
|
37 |
-
|
38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
39 |
df = pd.read_csv(STORAGE_PATH)
|
40 |
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
|
64 |
-
|
65 |
-
|
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.
|
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")
|
|
|
2 |
import pandas as pd
|
3 |
from datetime import datetime
|
4 |
import os
|
5 |
+
from huggingface_hub import Repository, HfFolder
|
6 |
|
7 |
+
# Store your Hugging Face token securely (avoid hardcoding it in your app.py)
|
8 |
+
HF_TOKEN = "your_huggingface_token_here" # Make sure to replace this with your Hugging Face token
|
|
|
|
|
9 |
|
10 |
+
# Save Hugging Face token to the Hugging Face folder for authentication
|
11 |
+
HfFolder.save_token(HF_TOKEN)
|
|
|
|
|
|
|
|
|
|
|
12 |
|
13 |
+
# Define constants for the dataset path and local storage
|
14 |
+
STORAGE_PATH = "appointments.csv"
|
15 |
+
REPO_ID = "Abu1998/DataCollection" # This is your dataset repository on Hugging Face
|
16 |
+
|
17 |
+
# Function to upload CSV to Hugging Face
|
18 |
def upload_to_huggingface():
|
19 |
try:
|
20 |
+
# Clone the dataset repository from Hugging Face
|
21 |
repo = Repository(local_dir="temp_repo", clone_from=REPO_ID, use_auth_token=HF_TOKEN)
|
22 |
+
repo.git_pull() # Ensure the latest version
|
23 |
+
|
24 |
+
# Make sure the local repository folder exists
|
25 |
os.makedirs("temp_repo", exist_ok=True)
|
26 |
+
|
27 |
+
# Load the data and save it in the temp repository directory
|
28 |
df = pd.read_csv(STORAGE_PATH)
|
29 |
df.to_csv(os.path.join("temp_repo", "appointments.csv"), index=False)
|
30 |
+
|
31 |
+
# Commit and push the updated CSV file to Hugging Face
|
32 |
repo.git_add("appointments.csv")
|
33 |
repo.git_commit("Updated appointments data")
|
34 |
repo.git_push()
|
35 |
+
|
36 |
return "Data uploaded to Hugging Face successfully!"
|
37 |
except Exception as e:
|
38 |
return f"Error uploading data: {e}"
|
39 |
|
40 |
+
# Function to save appointment data locally and upload to Hugging Face
|
41 |
def save_and_upload_to_csv(appointment_timing, services, products, contact, customer_name, rating, location, key_points, price):
|
42 |
+
# Check if the CSV exists, if not, create a new DataFrame
|
43 |
+
if not os.path.exists(STORAGE_PATH):
|
44 |
+
df = pd.DataFrame(columns=[
|
45 |
+
"Date", "Appointment", "Appointment Timing", "Services", "Products",
|
46 |
+
"Contact", "Customer Name", "Rating", "Location", "Key-points", "Price"
|
47 |
+
])
|
48 |
+
df.to_csv(STORAGE_PATH, index=False)
|
49 |
+
else:
|
50 |
+
# Load the existing CSV data
|
51 |
df = pd.read_csv(STORAGE_PATH)
|
52 |
|
53 |
+
# Auto-detect date and appointment ID
|
54 |
+
date = datetime.now().strftime("%Y-%m-%d")
|
55 |
+
appointment = len(df) + 1
|
56 |
|
57 |
+
# Add a new row of appointment data
|
58 |
+
new_row = pd.DataFrame([{
|
59 |
+
"Date": date,
|
60 |
+
"Appointment": appointment,
|
61 |
+
"Appointment Timing": appointment_timing,
|
62 |
+
"Services": services,
|
63 |
+
"Products": products,
|
64 |
+
"Contact": contact,
|
65 |
+
"Customer Name": customer_name,
|
66 |
+
"Rating": rating,
|
67 |
+
"Location": location,
|
68 |
+
"Key-points": key_points,
|
69 |
+
"Price": price
|
70 |
+
}])
|
71 |
|
72 |
+
# Append new data to the existing DataFrame
|
73 |
+
df = pd.concat([df, new_row], ignore_index=True)
|
74 |
+
df.to_csv(STORAGE_PATH, index=False) # Save updated data locally
|
75 |
|
76 |
+
# Upload the updated data to Hugging Face
|
77 |
+
return upload_to_huggingface()
|
|
|
|
|
|
|
78 |
|
79 |
# Gradio Interface
|
80 |
with gr.Blocks() as app:
|
81 |
gr.Markdown("# Appointment Data Storage Application")
|
82 |
appointment_timing = gr.Textbox(label="Appointment Timing")
|
83 |
+
services = gr.Dropdown(
|
84 |
label="Services",
|
85 |
+
choices=["Full arm Rica", "Full leg", "Underarms", "Eyebrow", "Upper lips"],
|
86 |
+
multiselect=True
|
87 |
)
|
88 |
products = gr.Textbox(label="Products")
|
89 |
contact = gr.Textbox(label="Contact")
|