Spaces:
Runtime error
Runtime error
Update app.py
Browse files
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
|
43 |
|
44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
|
|
|
46 |
api = HfApi()
|
47 |
|
48 |
-
# πΉ
|
49 |
try:
|
50 |
-
api.repo_info(repo_id, repo_type="dataset", token=HF_TOKEN)
|
51 |
-
print(f"β
Dataset '{repo_id}'
|
52 |
-
except
|
53 |
print(f"π΄ Dataset '{repo_id}' not found. Creating it now...")
|
54 |
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
|
|
|
|
64 |
|
65 |
db_path = "chatbot.db"
|
66 |
|
67 |
-
# πΉ
|
68 |
if not os.path.exists(db_path):
|
69 |
print("π΄ chatbot.db does not exist! Creating it now...")
|
70 |
-
|
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 |
-
|
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 |
-
|
92 |
|
93 |
-
|
94 |
-
|
|
|
|
|
95 |
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
102 |
|
103 |
-
|
|
|
104 |
|
105 |
-
if os.path.exists(DB_PATH):
|
106 |
-
print(f"β
Database downloaded at {DB_PATH}")
|
107 |
else:
|
108 |
-
|
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
|