Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -14,6 +14,16 @@ from sklearn.pipeline import Pipeline
|
|
| 14 |
from datetime import datetime
|
| 15 |
import json
|
| 16 |
import re # For email validation
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
# --- 0. Configuration & Tracking ---
|
| 19 |
|
|
@@ -24,7 +34,8 @@ USER_IP_USAGE = {} # In-memory database to track usage: {ip_address: c
|
|
| 24 |
|
| 25 |
# User Activation Data (Placeholder for Hugging Face)
|
| 26 |
USER_DATA_FILE = "user_activations_SalesPilot.csv"
|
| 27 |
-
|
|
|
|
| 28 |
|
| 29 |
# ML Model/File Configuration
|
| 30 |
MODEL_FILE = "ensemble_model.pkl"
|
|
@@ -200,7 +211,7 @@ def predict_leads(file_path):
|
|
| 200 |
summary_data = df_original['AI_Category'].value_counts().reset_index()
|
| 201 |
summary_data.columns = ['AI_Category', 'Count']
|
| 202 |
|
| 203 |
-
#
|
| 204 |
total_leads = df_original.shape[0]
|
| 205 |
summary_data['Percentage'] = (summary_data['Count'] / total_leads * 100).round(1).astype(str) + '%'
|
| 206 |
|
|
@@ -237,7 +248,7 @@ if not os.path.exists(MODEL_FILE):
|
|
| 237 |
# ----------------------------
|
| 238 |
|
| 239 |
def save_user_data(name, email):
|
| 240 |
-
"""Saves user data locally
|
| 241 |
try:
|
| 242 |
data = {
|
| 243 |
'timestamp': datetime.now().isoformat(),
|
|
@@ -246,27 +257,33 @@ def save_user_data(name, email):
|
|
| 246 |
}
|
| 247 |
|
| 248 |
# 1. Save locally to the specified CSV file
|
| 249 |
-
|
|
|
|
| 250 |
df_new = pd.DataFrame([data])
|
| 251 |
-
df_new.to_csv(USER_DATA_FILE, mode=mode, header=
|
|
|
|
|
|
|
|
|
|
| 252 |
|
| 253 |
-
#
|
| 254 |
-
#
|
| 255 |
-
#
|
| 256 |
-
#
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
|
|
|
|
|
|
|
|
|
|
| 268 |
|
| 269 |
-
return f"Activation successful! User data recorded *locally* in '{USER_DATA_FILE}'.\n\nTo see this data in your remote Hugging Face dataset ({HF_DATASET_REPO}), you must implement and configure the 'huggingface_hub' upload logic in the code."
|
| 270 |
except Exception as e:
|
| 271 |
return f"Activation failed due to data saving error: {e}"
|
| 272 |
|
|
@@ -361,7 +378,7 @@ def wrapper_retrain(file_path, request: gr.Request):
|
|
| 361 |
INSTRUCTION_STEPS = f"""
|
| 362 |
### How to Use the Ultimate Confidence App
|
| 363 |
|
| 364 |
-
1. **Activate App:** Enter your Name and Email and click 'Activate App'. This records your usage data **
|
| 365 |
2. **Download Sample (Optional):** Click 'Download Sample CSV' to see the required format for your leads data.
|
| 366 |
3. **Upload Data:** Upload your CSV file containing the leads you want to score (must include Name, Industry, Job\_Title, Company\_Size, Lead\_Source).
|
| 367 |
4. **Predict:** Click '🔮 Predict Leads'.
|
|
|
|
| 14 |
from datetime import datetime
|
| 15 |
import json
|
| 16 |
import re # For email validation
|
| 17 |
+
# NEW: Import HfApi for pushing data to Hugging Face
|
| 18 |
+
try:
|
| 19 |
+
from huggingface_hub import HfApi
|
| 20 |
+
except ImportError:
|
| 21 |
+
# This allows the app to run locally even if the HF libraries are not installed
|
| 22 |
+
class HfApi:
|
| 23 |
+
def upload_file(self, *args, **kwargs):
|
| 24 |
+
print("WARNING: huggingface_hub not imported. HF upload skipped.")
|
| 25 |
+
pass
|
| 26 |
+
|
| 27 |
|
| 28 |
# --- 0. Configuration & Tracking ---
|
| 29 |
|
|
|
|
| 34 |
|
| 35 |
# User Activation Data (Placeholder for Hugging Face)
|
| 36 |
USER_DATA_FILE = "user_activations_SalesPilot.csv"
|
| 37 |
+
# Ensure you own/have write access to this dataset repo
|
| 38 |
+
HF_DATASET_REPO = "abhidgp1978/user_activations_SalesPilot"
|
| 39 |
|
| 40 |
# ML Model/File Configuration
|
| 41 |
MODEL_FILE = "ensemble_model.pkl"
|
|
|
|
| 211 |
summary_data = df_original['AI_Category'].value_counts().reset_index()
|
| 212 |
summary_data.columns = ['AI_Category', 'Count']
|
| 213 |
|
| 214 |
+
# Use the total number of rows (leads) in the original dataframe
|
| 215 |
total_leads = df_original.shape[0]
|
| 216 |
summary_data['Percentage'] = (summary_data['Count'] / total_leads * 100).round(1).astype(str) + '%'
|
| 217 |
|
|
|
|
| 248 |
# ----------------------------
|
| 249 |
|
| 250 |
def save_user_data(name, email):
|
| 251 |
+
"""Saves user data locally and attempts to push it to the Hugging Face Hub."""
|
| 252 |
try:
|
| 253 |
data = {
|
| 254 |
'timestamp': datetime.now().isoformat(),
|
|
|
|
| 257 |
}
|
| 258 |
|
| 259 |
# 1. Save locally to the specified CSV file
|
| 260 |
+
is_new_file = not os.path.exists(USER_DATA_FILE)
|
| 261 |
+
mode = 'a' if not is_new_file else 'w'
|
| 262 |
df_new = pd.DataFrame([data])
|
| 263 |
+
df_new.to_csv(USER_DATA_FILE, mode=mode, header=is_new_file, index=False)
|
| 264 |
+
|
| 265 |
+
# 2. Hugging Face Upload Logic
|
| 266 |
+
api = HfApi()
|
| 267 |
|
| 268 |
+
# NOTE: For this to work in a deployed environment, you need to ensure
|
| 269 |
+
# that the environment is authenticated to push to HF (e.g., using a
|
| 270 |
+
# set HF_TOKEN environment variable). If you run this locally, you must
|
| 271 |
+
# run `huggingface-cli login` first or set a token in the script.
|
| 272 |
+
try:
|
| 273 |
+
api.upload_file(
|
| 274 |
+
path_or_fileobj=USER_DATA_FILE,
|
| 275 |
+
path_in_repo=USER_DATA_FILE,
|
| 276 |
+
repo_id=HF_DATASET_REPO,
|
| 277 |
+
repo_type="dataset",
|
| 278 |
+
commit_message=f"New user activation by {name} at {datetime.now().isoformat()}"
|
| 279 |
+
)
|
| 280 |
+
hf_status = f"Data successfully uploaded to Hugging Face dataset: `{HF_DATASET_REPO}`."
|
| 281 |
+
except Exception as hf_e:
|
| 282 |
+
hf_status = f"WARNING: Failed to upload data to Hugging Face. Check your token/permissions. Error: {hf_e.__class__.__name__}"
|
| 283 |
+
|
| 284 |
+
|
| 285 |
+
return f"Activation successful! User data recorded locally and in memory.\n\n{hf_status}"
|
| 286 |
|
|
|
|
| 287 |
except Exception as e:
|
| 288 |
return f"Activation failed due to data saving error: {e}"
|
| 289 |
|
|
|
|
| 378 |
INSTRUCTION_STEPS = f"""
|
| 379 |
### How to Use the Ultimate Confidence App
|
| 380 |
|
| 381 |
+
1. **Activate App:** Enter your Name and Email and click 'Activate App'. This records your usage data and **uploads it to the Hugging Face dataset** at **`{HF_DATASET_REPO}`**.
|
| 382 |
2. **Download Sample (Optional):** Click 'Download Sample CSV' to see the required format for your leads data.
|
| 383 |
3. **Upload Data:** Upload your CSV file containing the leads you want to score (must include Name, Industry, Job\_Title, Company\_Size, Lead\_Source).
|
| 384 |
4. **Predict:** Click '🔮 Predict Leads'.
|