Abu1998 commited on
Commit
5bf9ac2
·
verified ·
1 Parent(s): d3f0928

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -23
app.py CHANGED
@@ -2,11 +2,19 @@ import gradio as gr
2
  import pandas as pd
3
  from datetime import datetime
4
  import os
 
 
 
5
 
6
- # Path for local storage on Hugging Face Space
 
7
  STORAGE_PATH = "appointments.csv"
8
 
9
- # Check if the storage file exists; if not, create it
 
 
 
 
10
  if not os.path.exists(STORAGE_PATH):
11
  df = pd.DataFrame(columns=[
12
  "Date", "Appointment", "Appointment Timing", "Services", "Products",
@@ -14,16 +22,28 @@ if not os.path.exists(STORAGE_PATH):
14
  ])
15
  df.to_csv(STORAGE_PATH, index=False)
16
 
17
- # Function to save data to CSV
18
- def save_to_csv(appointment_timing, services, products, contact, customer_name, rating, location, key_points, price):
 
 
 
 
 
 
 
 
 
 
 
 
19
  # Load existing data
20
  df = pd.read_csv(STORAGE_PATH)
21
-
22
- # Auto-detect current date and appointment number
23
  date = datetime.now().strftime("%Y-%m-%d")
24
- appointment = len(df) + 1 # Generate appointment ID
25
-
26
- # Create a new row as a DataFrame
27
  new_row = pd.DataFrame([{
28
  "Date": date,
29
  "Appointment": appointment,
@@ -37,17 +57,17 @@ def save_to_csv(appointment_timing, services, products, contact, customer_name,
37
  "Key-points": key_points,
38
  "Price": price
39
  }])
40
-
41
- # Concatenate the new row with the existing DataFrame
42
  df = pd.concat([df, new_row], ignore_index=True)
43
- df.to_csv(STORAGE_PATH, index=False) # Save the updated DataFrame to the CSV file
44
- return f"Data saved successfully for Appointment {appointment}!"
45
 
46
- # Function to provide the download file
47
- def get_csv_file():
48
- return STORAGE_PATH
49
 
50
- # Define Gradio interface
 
 
51
  with gr.Blocks() as app:
52
  gr.Markdown("# Appointment Data Storage Application")
53
  appointment_timing = gr.Textbox(label="Appointment Timing")
@@ -65,18 +85,14 @@ with gr.Blocks() as app:
65
  price = gr.Dropdown(label="Price", choices=["999", "1499", "2499", "3499", "4499"])
66
  submit_button = gr.Button("Submit")
67
  output = gr.Textbox(label="Output")
68
- download_button = gr.File(label="Download Appointments Data", value=STORAGE_PATH)
69
 
70
  submit_button.click(
71
- save_to_csv,
72
  inputs=[
73
  appointment_timing, services, products, contact, customer_name, rating, location, key_points, price
74
  ],
75
  outputs=output
76
  )
77
 
78
- # Add a file download button
79
- download_button.render()
80
-
81
- # Launch the application
82
  app.launch(share=True)
 
2
  import pandas as pd
3
  from datetime import datetime
4
  import os
5
+ from huggingface_hub import HfApi, HfFolder, Repository
6
+ import os
7
+ HF_TOKEN = os.getenv("HF_TOKEN")
8
 
9
+ # Constants for Hugging Face
10
+ REPO_ID = "Abu1998/DataCollection" # Replace with your dataset repository name
11
  STORAGE_PATH = "appointments.csv"
12
 
13
+ # Authenticate with Hugging Face Hub
14
+ api = HfApi()
15
+ HfFolder.save_token(HF_TOKEN)
16
+
17
+ # Ensure the CSV file exists
18
  if not os.path.exists(STORAGE_PATH):
19
  df = pd.DataFrame(columns=[
20
  "Date", "Appointment", "Appointment Timing", "Services", "Products",
 
22
  ])
23
  df.to_csv(STORAGE_PATH, index=False)
24
 
25
+ # Function to upload CSV to Hugging Face Hub
26
+ def upload_to_huggingface():
27
+ repo = Repository(local_dir="temp_repo", clone_from=REPO_ID, use_auth_token=HF_TOKEN)
28
+ repo.git_pull() # Ensure the latest version
29
+ os.makedirs("temp_repo", exist_ok=True)
30
+ df = pd.read_csv(STORAGE_PATH)
31
+ df.to_csv(os.path.join("temp_repo", "appointments.csv"), index=False)
32
+ repo.git_add("appointments.csv")
33
+ repo.git_commit("Updated appointments data")
34
+ repo.git_push()
35
+ return "Data uploaded to Hugging Face successfully!"
36
+
37
+ # Function to save data locally and upload to Hugging Face
38
+ def save_and_upload_to_csv(appointment_timing, services, products, contact, customer_name, rating, location, key_points, price):
39
  # Load existing data
40
  df = pd.read_csv(STORAGE_PATH)
41
+
42
+ # Auto-detect date and appointment ID
43
  date = datetime.now().strftime("%Y-%m-%d")
44
+ appointment = len(df) + 1
45
+
46
+ # Add a new row
47
  new_row = pd.DataFrame([{
48
  "Date": date,
49
  "Appointment": appointment,
 
57
  "Key-points": key_points,
58
  "Price": price
59
  }])
60
+
61
+ # Append and save locally
62
  df = pd.concat([df, new_row], ignore_index=True)
63
+ df.to_csv(STORAGE_PATH, index=False)
 
64
 
65
+ # Upload to Hugging Face
66
+ upload_to_huggingface()
 
67
 
68
+ return f"Appointment {appointment} saved and uploaded successfully!"
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")
 
85
  price = gr.Dropdown(label="Price", choices=["999", "1499", "2499", "3499", "4499"])
86
  submit_button = gr.Button("Submit")
87
  output = gr.Textbox(label="Output")
 
88
 
89
  submit_button.click(
90
+ save_and_upload_to_csv,
91
  inputs=[
92
  appointment_timing, services, products, contact, customer_name, rating, location, key_points, price
93
  ],
94
  outputs=output
95
  )
96
 
97
+ # Launch the app
 
 
 
98
  app.launch(share=True)