Spaces:
Sleeping
Sleeping
Integrate DataEnrichmentManager into app.py with new UI tab
Browse files
app.py
CHANGED
|
@@ -5,6 +5,7 @@ import json
|
|
| 5 |
from typing import List, Dict, Any, Optional
|
| 6 |
from pathlib import Path
|
| 7 |
from linkedin_data import fetch_linkedin_profile, get_cache_stats
|
|
|
|
| 8 |
|
| 9 |
# --- Data Storage Configuration ---
|
| 10 |
DATA_DIR = Path("/data")
|
|
@@ -33,6 +34,10 @@ def save_user_data(username: str, data_type: str, data: Any):
|
|
| 33 |
with open(data_file, 'w') as f:
|
| 34 |
json.dump(data, f, indent=2)
|
| 35 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
# --- Consent/Event Audit Panel ---
|
| 37 |
def consent_audit_panel(consent_events: List[Dict[str, Any]], profile: Optional[gr.OAuthProfile]) -> str:
|
| 38 |
"""
|
|
@@ -219,6 +224,11 @@ with gr.Blocks(title="StrategyStack UI") as demo:
|
|
| 219 |
audit_output = gr.Markdown()
|
| 220 |
audit_refresh = gr.Button("Refresh Data")
|
| 221 |
user_profile_4 = gr.State()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 222 |
|
| 223 |
# Load initial data
|
| 224 |
def load_consent_data(profile: Optional[gr.OAuthProfile] = None):
|
|
@@ -232,17 +242,67 @@ with gr.Blocks(title="StrategyStack UI") as demo:
|
|
| 232 |
|
| 233 |
def load_audit_data(profile: Optional[gr.OAuthProfile] = None):
|
| 234 |
return audit_trail_widget(profile)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 235 |
|
| 236 |
# Set up event handlers
|
| 237 |
demo.load(load_consent_data, inputs=None, outputs=consent_output)
|
| 238 |
demo.load(load_branching_data, inputs=None, outputs=branching_output)
|
| 239 |
demo.load(load_diagnostics_data, inputs=None, outputs=diagnostics_output)
|
| 240 |
demo.load(load_audit_data, inputs=None, outputs=audit_output)
|
|
|
|
| 241 |
|
| 242 |
consent_refresh.click(load_consent_data, inputs=None, outputs=consent_output)
|
| 243 |
branching_refresh.click(load_branching_data, inputs=None, outputs=branching_output)
|
| 244 |
diagnostics_refresh.click(load_diagnostics_data, inputs=None, outputs=diagnostics_output)
|
| 245 |
audit_refresh.click(load_audit_data, inputs=None, outputs=audit_output)
|
|
|
|
| 246 |
|
| 247 |
if __name__ == "__main__":
|
| 248 |
demo.launch()
|
|
|
|
| 5 |
from typing import List, Dict, Any, Optional
|
| 6 |
from pathlib import Path
|
| 7 |
from linkedin_data import fetch_linkedin_profile, get_cache_stats
|
| 8 |
+
from data_enrichment import DataEnrichmentManager
|
| 9 |
|
| 10 |
# --- Data Storage Configuration ---
|
| 11 |
DATA_DIR = Path("/data")
|
|
|
|
| 34 |
with open(data_file, 'w') as f:
|
| 35 |
json.dump(data, f, indent=2)
|
| 36 |
|
| 37 |
+
def get_enrichment_manager(username: str) -> DataEnrichmentManager:
|
| 38 |
+
"""Get or create DataEnrichmentManager for a user"""
|
| 39 |
+
return DataEnrichmentManager(username)
|
| 40 |
+
|
| 41 |
# --- Consent/Event Audit Panel ---
|
| 42 |
def consent_audit_panel(consent_events: List[Dict[str, Any]], profile: Optional[gr.OAuthProfile]) -> str:
|
| 43 |
"""
|
|
|
|
| 224 |
audit_output = gr.Markdown()
|
| 225 |
audit_refresh = gr.Button("Refresh Data")
|
| 226 |
user_profile_4 = gr.State()
|
| 227 |
+
|
| 228 |
+
with gr.Tab("Data Enrichment"):
|
| 229 |
+
enrichment_output = gr.Markdown()
|
| 230 |
+
enrichment_refresh = gr.Button("Refresh Data")
|
| 231 |
+
user_profile_5 = gr.State()
|
| 232 |
|
| 233 |
# Load initial data
|
| 234 |
def load_consent_data(profile: Optional[gr.OAuthProfile] = None):
|
|
|
|
| 242 |
|
| 243 |
def load_audit_data(profile: Optional[gr.OAuthProfile] = None):
|
| 244 |
return audit_trail_widget(profile)
|
| 245 |
+
|
| 246 |
+
def load_enrichment_data(profile: Optional[gr.OAuthProfile] = None):
|
| 247 |
+
if profile is None:
|
| 248 |
+
return "# π Authentication Required\n\nPlease sign in with your Hugging Face account to access data enrichment features."
|
| 249 |
+
|
| 250 |
+
username = profile.username
|
| 251 |
+
manager = get_enrichment_manager(username)
|
| 252 |
+
stats = manager.get_enrichment_stats()
|
| 253 |
+
|
| 254 |
+
enrichment_md = f"# π Data Enrichment Dashboard\n\n**User:** {username}\n\n"
|
| 255 |
+
enrichment_md += "## Enrichment Statistics\n\n"
|
| 256 |
+
enrichment_md += f"- **Total API Calls:** {stats.get('total_api_calls', 0)}\n"
|
| 257 |
+
enrichment_md += f"- **Cache Hits:** {stats.get('cache_hits', 0)}\n"
|
| 258 |
+
enrichment_md += f"- **Cache Misses:** {stats.get('cache_misses', 0)}\n"
|
| 259 |
+
enrichment_md += f"- **Total Enrichments:** {stats.get('total_enrichments', 0)}\n"
|
| 260 |
+
enrichment_md += f"- **Successful Enrichments:** {stats.get('successful_enrichments', 0)}\n"
|
| 261 |
+
enrichment_md += f"- **Failed Enrichments:** {stats.get('failed_enrichments', 0)}\n\n"
|
| 262 |
+
|
| 263 |
+
# Calculate success rate
|
| 264 |
+
if stats.get('total_enrichments', 0) > 0:
|
| 265 |
+
success_rate = (stats.get('successful_enrichments', 0) / stats['total_enrichments']) * 100
|
| 266 |
+
enrichment_md += f"- **Success Rate:** {success_rate:.1f}%\n\n"
|
| 267 |
+
|
| 268 |
+
enrichment_md += "## Available APIs\n\n"
|
| 269 |
+
enrichment_md += "- β
LinkedIn Profile Enrichment (4 providers with fallback)\n"
|
| 270 |
+
enrichment_md += "- β
Company Profile Enrichment\n"
|
| 271 |
+
enrichment_md += "- β
Apollo.io Person Enrichment\n"
|
| 272 |
+
enrichment_md += "- β
Twitter User Data\n"
|
| 273 |
+
enrichment_md += "- β
Facebook Data\n"
|
| 274 |
+
enrichment_md += "- β
Google Search\n"
|
| 275 |
+
enrichment_md += "- β
Mediafy Enrichment\n"
|
| 276 |
+
enrichment_md += "- β
Local Business Data (LetScrape)\n"
|
| 277 |
+
enrichment_md += "- β
Website Contacts Scraper (LetScrape)\n\n"
|
| 278 |
+
|
| 279 |
+
enrichment_md += "## Features\n\n"
|
| 280 |
+
enrichment_md += "- π **Per-User Data Isolation**: Your enrichment data is stored separately\n"
|
| 281 |
+
enrichment_md += "- β‘ **Intelligent Caching**: 30-day TTL to minimize API costs\n"
|
| 282 |
+
enrichment_md += "- π **Automatic Fallback**: Multiple providers ensure high availability\n"
|
| 283 |
+
enrichment_md += "- π **Real-time Statistics**: Track API usage and success rates\n\n"
|
| 284 |
+
|
| 285 |
+
enrichment_md += "## Usage\n\n"
|
| 286 |
+
enrichment_md += "To use data enrichment in your workflows:\n"
|
| 287 |
+
enrichment_md += "1. Add contact data with fields like `linkedin_url`, `email`, `domain`\n"
|
| 288 |
+
enrichment_md += "2. The system automatically enriches contacts using all available APIs\n"
|
| 289 |
+
enrichment_md += "3. Enriched data is cached for 30 days to optimize performance\n"
|
| 290 |
+
enrichment_md += "4. View statistics and monitor API usage from this dashboard\n\n"
|
| 291 |
+
|
| 292 |
+
return gr.Markdown(enrichment_md)
|
| 293 |
|
| 294 |
# Set up event handlers
|
| 295 |
demo.load(load_consent_data, inputs=None, outputs=consent_output)
|
| 296 |
demo.load(load_branching_data, inputs=None, outputs=branching_output)
|
| 297 |
demo.load(load_diagnostics_data, inputs=None, outputs=diagnostics_output)
|
| 298 |
demo.load(load_audit_data, inputs=None, outputs=audit_output)
|
| 299 |
+
demo.load(load_enrichment_data, inputs=None, outputs=enrichment_output)
|
| 300 |
|
| 301 |
consent_refresh.click(load_consent_data, inputs=None, outputs=consent_output)
|
| 302 |
branching_refresh.click(load_branching_data, inputs=None, outputs=branching_output)
|
| 303 |
diagnostics_refresh.click(load_diagnostics_data, inputs=None, outputs=diagnostics_output)
|
| 304 |
audit_refresh.click(load_audit_data, inputs=None, outputs=audit_output)
|
| 305 |
+
enrichment_refresh.click(load_enrichment_data, inputs=None, outputs=enrichment_output)
|
| 306 |
|
| 307 |
if __name__ == "__main__":
|
| 308 |
demo.launch()
|