Abu1998 commited on
Commit
ef9069d
·
verified ·
1 Parent(s): 941336c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -53
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 HfApi, HfFolder, Repository
6
 
7
- # Authenticate with Hugging Face using the secret token
8
- api = HfApi()
9
- HfFolder.save_token(HF_TOKEN)
 
10
 
11
- # Use the token to access or modify your repository
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
- repo = Repository(local_dir="temp_repo", clone_from=REPO_ID, use_auth_token=HF_TOKEN)
30
- repo.git_pull() # Ensure the latest version
31
- os.makedirs("temp_repo", exist_ok=True)
32
- df = pd.read_csv(STORAGE_PATH)
33
- df.to_csv(os.path.join("temp_repo", "appointments.csv"), index=False)
34
- repo.git_add("appointments.csv")
35
- repo.git_commit("Updated appointments data")
36
- repo.git_push()
37
- return "Data uploaded to Hugging Face successfully!"
 
 
 
38
 
39
- # Function to save data locally and upload to Hugging Face
40
  def save_and_upload_to_csv(appointment_timing, services, products, contact, customer_name, rating, location, key_points, price):
41
- # Load existing data
42
- df = pd.read_csv(STORAGE_PATH)
 
43
 
44
- # Auto-detect date and appointment ID
45
- date = datetime.now().strftime("%Y-%m-%d")
46
- appointment = len(df) + 1
47
 
48
- # Add a new row
49
- new_row = pd.DataFrame([{
50
- "Date": date,
51
- "Appointment": appointment,
52
- "Appointment Timing": appointment_timing,
53
- "Services": services,
54
- "Products": products,
55
- "Contact": contact,
56
- "Customer Name": customer_name,
57
- "Rating": rating,
58
- "Location": location,
59
- "Key-points": key_points,
60
- "Price": price
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
- # Upload to Hugging Face
68
- upload_to_huggingface()
 
69
 
70
- return f"Appointment {appointment} saved and uploaded successfully!"
 
 
 
 
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.Dropdown(
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")