joe4ai commited on
Commit
b2a9a89
Β·
verified Β·
1 Parent(s): 2bfc944

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +66 -40
app.py CHANGED
@@ -39,35 +39,44 @@ os.environ['AUTH_TOKEN_KEY'] = AUTH_TOKEN_KEY
39
  HF_TOKEN = os.environ.get("HF_TOKEN")
40
 
41
  if not HF_TOKEN:
42
- raise ValueError("🚨 HF_TOKEN is not set! Ensure you have the right permissions.")
43
 
44
- from huggingface_hub import HfApi
 
 
 
 
 
 
45
 
 
46
  api = HfApi()
47
 
48
- # πŸ”Ή Check if the dataset exists in the organization
49
  try:
50
- api.repo_info(repo_id, repo_type="dataset", token=HF_TOKEN)
51
- print(f"βœ… Dataset '{repo_id}' already exists in the organization.")
52
- except Exception:
53
  print(f"πŸ”΄ Dataset '{repo_id}' not found. Creating it now...")
54
 
55
- # Create repo inside the organization
56
- api.create_repo(
57
- repo_id=repo_id,
58
- repo_type="dataset",
59
- private=True, # Ensure it's private
60
- token=HF_TOKEN,
61
- organization=HF_ORG_NAME # Specify organization
62
- )
63
- print(f"βœ… Dataset '{repo_id}' created successfully in the organization.")
 
 
64
 
65
  db_path = "chatbot.db"
66
 
67
- # πŸ”Ή Ensure `chatbot.db` exists before uploading
68
  if not os.path.exists(db_path):
69
  print("πŸ”΄ chatbot.db does not exist! Creating it now...")
70
- import sqlite3
71
  conn = sqlite3.connect(db_path)
72
  cursor = conn.cursor()
73
  cursor.execute('''CREATE TABLE IF NOT EXISTS users (
@@ -77,35 +86,52 @@ if not os.path.exists(db_path):
77
  )''')
78
  conn.commit()
79
  conn.close()
80
- print("βœ… chatbot.db created successfully!")
81
-
82
- # πŸ”Ή Upload chatbot.db as a private dataset
83
- api.upload_file(
84
- path_or_fileobj=db_path,
85
- path_in_repo="chatbot.db",
86
- repo_id=repo_id,
87
- repo_type="dataset",
88
- token=HF_TOKEN
89
- )
90
 
91
- print("βœ… chatbot.db successfully uploaded to the organization's private dataset.")
92
 
93
- from huggingface_hub import snapshot_download
94
- import os
 
 
95
 
96
- # πŸ”Ή Download chatbot.db securely
97
- db_folder = snapshot_download(
98
- repo_id=repo_id,
99
- allow_patterns=["chatbot.db"], # Only download chatbot.db
100
- use_auth_token=HF_TOKEN
101
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
102
 
103
- DB_PATH = os.path.join(db_folder, "chatbot.db")
 
104
 
105
- if os.path.exists(DB_PATH):
106
- print(f"βœ… Database downloaded at {DB_PATH}")
107
  else:
108
- raise FileNotFoundError("🚨 Failed to download chatbot.db from the organization's dataset.")
109
 
110
  # ---- Database part ----- #
111
  # Database Connection
 
39
  HF_TOKEN = os.environ.get("HF_TOKEN")
40
 
41
  if not HF_TOKEN:
42
+ raise ValueError("🚨 HF_TOKEN is not set! Ensure you have the correct permissions.")
43
 
44
+ # πŸ”Ή Verify authentication works
45
+ try:
46
+ user_info = api.whoami(token=HF_TOKEN)
47
+ print(f"βœ… Authenticated as: {user_info['name']}")
48
+ except Exception as e:
49
+ raise ValueError(f"🚨 Hugging Face authentication failed: {e}")
50
+ from huggingface_hub import RepositoryNotFoundError
51
 
52
+ repo_id = f"{HF_ORG_NAME}/{DATASET_NAME}" # Use your organization dataset
53
  api = HfApi()
54
 
55
+ # πŸ”Ή Verify if dataset exists
56
  try:
57
+ repo_info = api.repo_info(repo_id, repo_type="dataset", token=HF_TOKEN)
58
+ print(f"βœ… Dataset '{repo_id}' exists.")
59
+ except RepositoryNotFoundError:
60
  print(f"πŸ”΄ Dataset '{repo_id}' not found. Creating it now...")
61
 
62
+ try:
63
+ api.create_repo(
64
+ repo_id=repo_id,
65
+ repo_type="dataset",
66
+ private=True, # βœ… Keep it private
67
+ token=HF_TOKEN,
68
+ organization=HF_ORG_NAME
69
+ )
70
+ print(f"βœ… Dataset '{repo_id}' created successfully.")
71
+ except Exception as e:
72
+ raise ValueError(f"🚨 Failed to create dataset '{repo_id}': {e}")
73
 
74
  db_path = "chatbot.db"
75
 
76
+ # πŸ”Ή Check if chatbot.db exists
77
  if not os.path.exists(db_path):
78
  print("πŸ”΄ chatbot.db does not exist! Creating it now...")
79
+
80
  conn = sqlite3.connect(db_path)
81
  cursor = conn.cursor()
82
  cursor.execute('''CREATE TABLE IF NOT EXISTS users (
 
86
  )''')
87
  conn.commit()
88
  conn.close()
89
+
90
+ print("βœ… chatbot.db created successfully.")
 
 
 
 
 
 
 
 
91
 
92
+ repo_files = api.list_repo_files(repo_id, repo_type="dataset", token=HF_TOKEN)
93
 
94
+ if "chatbot.db" in repo_files:
95
+ print("🟒 `chatbot.db` already exists in the dataset. Skipping upload.")
96
+ else:
97
+ print("πŸ“€ Uploading `chatbot.db` to Hugging Face...")
98
 
99
+ api.upload_file(
100
+ path_or_fileobj=db_path,
101
+ path_in_repo="chatbot.db",
102
+ repo_id=repo_id,
103
+ repo_type="dataset",
104
+ token=HF_TOKEN
105
+ )
106
+
107
+ print("βœ… `chatbot.db` successfully uploaded to the organization's private dataset.")
108
+
109
+ DB_LOCAL_PATH = "/tmp/chatbot.db" # Path where we save the database
110
+
111
+ # πŸ”Ή Only download if chatbot.db does not exist locally
112
+ if not os.path.exists(DB_LOCAL_PATH):
113
+ print("πŸ“₯ Downloading `chatbot.db` from Hugging Face...")
114
+
115
+ try:
116
+ db_folder = snapshot_download(
117
+ repo_id=repo_id,
118
+ repo_type="dataset", # βœ… Ensure it's a dataset repo
119
+ allow_patterns=["chatbot.db"],
120
+ use_auth_token=HF_TOKEN
121
+ )
122
+
123
+ DB_PATH = os.path.join(db_folder, "chatbot.db")
124
+
125
+ if os.path.exists(DB_PATH):
126
+ print(f"βœ… Database downloaded at {DB_PATH}")
127
+ else:
128
+ raise FileNotFoundError("🚨 Failed to download chatbot.db from the organization's dataset.")
129
 
130
+ except Exception as e:
131
+ raise FileNotFoundError(f"🚨 Error downloading chatbot.db: {e}")
132
 
 
 
133
  else:
134
+ print(f"🟒 `chatbot.db` already exists at {DB_LOCAL_PATH}, skipping download.")
135
 
136
  # ---- Database part ----- #
137
  # Database Connection