Save local changes (GSC/Bing integrations) before merging PR #354

This commit is contained in:
ajaysi
2026-02-13 13:11:27 +05:30
parent 43e66835ac
commit 08a1f4a1d8
144 changed files with 8310 additions and 2748 deletions

View File

@@ -0,0 +1,122 @@
import asyncio
import time
import os
import sys
from typing import Dict, Any, List
from tabulate import tabulate
from loguru import logger
# Add project root to path
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from services.llm_providers.main_image_generation import generate_image_with_provider
from services.llm_providers.image_generation.wavespeed_provider import WaveSpeedImageProvider
async def benchmark_provider(provider_name: str, model: str, prompt: str) -> Dict[str, Any]:
"""Benchmark a single provider/model combination."""
logger.info(f"Benchmarking {provider_name} ({model})...")
start_time = time.time()
try:
# We use a mocked user_id for validation bypass if needed,
# or rely on the system to handle "benchmark_user"
result = await generate_image_with_provider(
prompt=prompt,
provider=provider_name,
model=model,
width=1024,
height=1024,
user_id="benchmark_user"
)
duration = time.time() - start_time
success = result.get("success", False)
return {
"provider": provider_name,
"model": model,
"duration": duration,
"success": success,
"error": result.get("error")
}
except Exception as e:
return {
"provider": provider_name,
"model": model,
"duration": time.time() - start_time,
"success": False,
"error": str(e)
}
async def run_benchmarks():
"""Run benchmarks across configured providers."""
# Check configured providers
wavespeed_key = os.getenv("WAVESPEED_API_KEY")
stability_key = os.getenv("STABILITY_API_KEY")
hf_token = os.getenv("HF_TOKEN")
logger.info("Checking configured providers...")
logger.info(f"WaveSpeed: {'✅ Configured' if wavespeed_key else '❌ Missing API Key'}")
logger.info(f"Stability: {'✅ Configured' if stability_key else '❌ Missing API Key'}")
logger.info(f"HuggingFace: {'✅ Configured' if hf_token else '❌ Missing API Key'}")
prompt = "A professional brand avatar of a creative designer, minimalist style, clean background, high resolution"
tasks = []
# WaveSpeed Models
if wavespeed_key:
tasks.append(benchmark_provider("wavespeed", "ideogram-v3-turbo", prompt))
tasks.append(benchmark_provider("wavespeed", "qwen-image", prompt))
tasks.append(benchmark_provider("wavespeed", "flux-kontext-pro", prompt))
# Stability Models
if stability_key:
tasks.append(benchmark_provider("stability", "core", prompt))
# HuggingFace Models
if hf_token:
tasks.append(benchmark_provider("huggingface", "black-forest-labs/FLUX.1-dev", prompt))
if not tasks:
logger.warning("No providers configured for benchmarking.")
return
logger.info(f"Starting benchmark for {len(tasks)} configurations...")
results = await asyncio.gather(*tasks)
# Display results
table_data = []
for r in results:
status = "✅ Success" if r["success"] else f"❌ Failed: {r['error'][:30]}..."
table_data.append([
r["provider"],
r["model"],
f"{r['duration']:.2f}s",
status
])
print("\n" + "="*60)
print("AVATAR GENERATION PERFORMANCE BENCHMARK")
print("="*60)
print(tabulate(table_data, headers=["Provider", "Model", "Time", "Status"], tablefmt="grid"))
print("\nRecommendation:")
# Simple recommendation logic
successful = [r for r in results if r["success"]]
if successful:
fastest = min(successful, key=lambda x: x["duration"])
print(f"Fastest provider: {fastest['provider']} ({fastest['model']}) at {fastest['duration']:.2f}s")
# Check WaveSpeed specifically
wavespeed_results = [r for r in successful if r["provider"] == "wavespeed"]
if wavespeed_results:
avg_wavespeed = sum(r["duration"] for r in wavespeed_results) / len(wavespeed_results)
print(f"WaveSpeed Average: {avg_wavespeed:.2f}s")
else:
print("No successful generations to analyze.")
if __name__ == "__main__":
asyncio.run(run_benchmarks())

View File

@@ -0,0 +1,88 @@
import sys
import os
import logging
# Add backend to path
sys.path.append(os.path.join(os.getcwd(), 'backend'))
from sqlalchemy import create_engine, text
from sqlalchemy.orm import sessionmaker
# Setup logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
from models.subscription_models import APIUsageLog, UserSubscription, APIProvider
from services.subscription import UsageTrackingService, PricingService
USER_ID = "user_33Gz1FPI86VDXhRY8QN4ragRFGN"
def get_db_path(user_id):
# Logic from database.py to resolve path
base_path = os.getcwd()
# Sanitize user_id
safe_user_id = "".join(c for c in user_id if c.isalnum() or c in ('-', '_'))
user_workspace = os.path.join(base_path, "workspace", f"workspace_{safe_user_id}")
# Try both naming conventions
db_path_v1 = os.path.join(user_workspace, "db", "alwrity.db")
db_path_v2 = os.path.join(user_workspace, "db", f"alwrity_{safe_user_id}.db")
if os.path.exists(db_path_v2):
return db_path_v2
return db_path_v1
def check_user_data():
db_path = get_db_path(USER_ID)
logger.info(f"Checking DB at: {db_path}")
if not os.path.exists(db_path):
logger.error(f"DB file not found at {db_path}")
# Check default DB as fallback
default_db = os.path.join(os.getcwd(), 'backend', 'data', 'alwrity.db')
if os.path.exists(default_db):
logger.info(f"Falling back to default DB: {default_db}")
db_url = f"sqlite:///{default_db}"
else:
return
else:
db_url = f"sqlite:///{db_path}"
engine = create_engine(db_url)
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
db = SessionLocal()
try:
# 1. Check API Usage Logs
logs = db.query(APIUsageLog).filter(APIUsageLog.user_id == USER_ID).all()
logger.info(f"Found {len(logs)} usage logs for user {USER_ID}")
if logs:
last_log = logs[-1]
logger.info(f"Last log: {last_log.timestamp} - {last_log.provider} - {last_log.cost_total}")
# Print provider breakdown
from collections import Counter
providers = Counter([l.provider for l in logs])
logger.info(f"Provider breakdown: {providers}")
# 2. Check Subscription
sub = db.query(UserSubscription).filter(UserSubscription.user_id == USER_ID).first()
if sub:
logger.info(f"Subscription found: {sub.plan_type} ({sub.status})")
else:
logger.warning("No subscription found")
# 3. Run Usage Service
logger.info("Running UsageTrackingService.get_user_usage_stats...")
service = UsageTrackingService(db)
stats = service.get_user_usage_stats(USER_ID)
logger.info(f"Service Stats: {stats}")
except Exception as e:
logger.error(f"Error: {e}")
finally:
db.close()
if __name__ == "__main__":
check_user_data()

View File

@@ -0,0 +1,48 @@
import os
import sys
from pathlib import Path
# Add the backend directory to the Python path
backend_dir = Path(__file__).parent.parent
sys.path.insert(0, str(backend_dir))
def diagnose():
print(f"Current working directory: {os.getcwd()}")
# Replicate database.py logic
file_path = os.path.abspath(__file__)
# backend/scripts/diagnose.py -> backend/scripts -> backend -> root
# Wait, in database.py it is services/database.py -> services -> backend -> root
# So 3 levels up.
# Here: scripts/diagnose.py -> scripts -> backend -> root.
# So also 3 levels up.
ROOT_DIR = os.path.dirname(os.path.dirname(os.path.dirname(file_path)))
print(f"Calculated ROOT_DIR: {ROOT_DIR}")
workspace_dir = os.path.join(ROOT_DIR, 'workspace')
print(f"Calculated WORKSPACE_DIR: {workspace_dir}")
if os.path.exists(workspace_dir):
print(f"Workspace directory exists.")
print("Contents:")
try:
for item in os.listdir(workspace_dir):
print(f" - {item}")
except Exception as e:
print(f"Error listing workspace: {e}")
else:
print(f"Workspace directory DOES NOT exist.")
# Check for alwrity.db in backend
backend_db = os.path.join(backend_dir, 'alwrity.db')
if os.path.exists(backend_db):
print(f"Found legacy DB in backend: {backend_db}")
# Check for alwrity.db in root
root_db = os.path.join(ROOT_DIR, 'alwrity.db')
if os.path.exists(root_db):
print(f"Found legacy DB in root: {root_db}")
if __name__ == "__main__":
diagnose()

View File

@@ -0,0 +1,57 @@
import os
import sqlite3
import pandas as pd
from pathlib import Path
def inspect_dbs():
root = Path(os.getcwd())
workspace_dir = root / 'workspace'
if not workspace_dir.exists():
print("No workspace directory found.")
return
print(f"Scanning {workspace_dir} for databases...")
for user_dir in workspace_dir.iterdir():
if user_dir.is_dir() and user_dir.name.startswith('workspace_'):
db_dir = user_dir / 'db'
if db_dir.exists():
for db_file in db_dir.glob('*.db'):
print(f"\n--- Checking DB: {db_file} ---")
try:
conn = sqlite3.connect(db_file)
# Check tables
cursor = conn.cursor()
cursor.execute("SELECT name FROM sqlite_master WHERE type='table';")
tables = [r[0] for r in cursor.fetchall()]
print(f"Tables: {len(tables)}")
if 'api_usage_logs' in tables:
count = cursor.execute("SELECT count(*) FROM api_usage_logs").fetchone()[0]
print(f"api_usage_logs count: {count}")
if count > 0:
# Show last 5 logs
print("Last 5 logs:")
df = pd.read_sql_query("SELECT * FROM api_usage_logs ORDER BY created_at DESC LIMIT 5", conn)
print(df[['id', 'provider', 'model_used', 'cost_total', 'created_at']].to_string())
else:
print("Table 'api_usage_logs' NOT found.")
if 'usage_summaries' in tables:
print("Usage Summaries:")
df = pd.read_sql_query("SELECT * FROM usage_summaries", conn)
if not df.empty:
print(df.to_string())
else:
print("Table 'usage_summaries' is empty.")
else:
print("Table 'usage_summaries' NOT found.")
conn.close()
except Exception as e:
print(f"Error reading DB: {e}")
if __name__ == "__main__":
inspect_dbs()

View File

@@ -0,0 +1,42 @@
import os
import sys
import sqlite3
user_id = "user_33Gz1FPI86VDXhRY8QN4ragRFGN"
base_path = os.getcwd()
safe_user_id = "".join(c for c in user_id if c.isalnum() or c in ('-', '_'))
user_workspace = os.path.join(base_path, "workspace", f"workspace_{safe_user_id}")
db_path = os.path.join(user_workspace, "db", f"alwrity_{safe_user_id}.db")
print(f"Reading from: {db_path}")
conn = sqlite3.connect(db_path)
conn.row_factory = sqlite3.Row
cursor = conn.cursor()
print("\n--- API Usage Logs ---")
cursor.execute("SELECT * FROM api_usage_logs")
rows = cursor.fetchall()
for row in rows:
print(dict(row))
print("\n--- Subscription Plans ---")
try:
cursor.execute("SELECT * FROM subscription_plans")
rows = cursor.fetchall()
for row in rows:
print(dict(row))
except Exception as e:
print(f"Error reading subscription_plans: {e}")
print("\n--- Usage Summaries ---")
try:
cursor.execute("SELECT * FROM usage_summaries")
rows = cursor.fetchall()
for row in rows:
print(dict(row))
except Exception as e:
print(f"Error reading usage_summaries: {e}")
conn.close()

View File

@@ -0,0 +1,89 @@
import asyncio
import logging
import sys
import os
# Add backend directory to path
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
from unittest.mock import MagicMock, AsyncMock
from services.intelligence.agents.specialized_agents import ContentGuardianAgent, StrategyArchitectAgent
# Configure logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
async def test_content_guardian():
print("\n=== Testing ContentGuardianAgent ===")
# Mock Intelligence Service
mock_intelligence = MagicMock()
mock_intelligence.is_initialized.return_value = True
# Mock search for cannibalization check
# Scenario 1: No cannibalization
mock_intelligence.search = AsyncMock(return_value=[])
agent = ContentGuardianAgent(mock_intelligence, user_id="test_user")
content = "This is a unique piece of content about AI agents." + " word" * 50 # Make it long enough
print(f"Testing assess_content_quality with content length: {len(content)}")
result = await agent.assess_content_quality(content)
print("Result:", result)
if result.get("quality_score", 0) > 0:
print("✅ assess_content_quality returned a valid score.")
else:
print("❌ assess_content_quality failed to return a valid score.")
# Scenario 2: Cannibalization detected
mock_intelligence.search = AsyncMock(return_value=[{'id': 'existing_doc', 'score': 0.9}])
print("\nTesting assess_content_quality with cannibalization...")
result_cannibal = await agent.assess_content_quality(content)
print("Result (Cannibalization):", result_cannibal)
if result_cannibal.get("cannibalization_risk", {}).get("warning"):
print("✅ Cannibalization correctly detected.")
else:
print("❌ Cannibalization NOT detected when it should be.")
async def test_strategy_architect():
print("\n=== Testing StrategyArchitectAgent ===")
mock_intelligence = MagicMock()
mock_intelligence.is_initialized.return_value = True
# Scenario 1: No clusters
mock_intelligence.cluster = AsyncMock(return_value=[])
agent = StrategyArchitectAgent(mock_intelligence, user_id="test_user")
print("Testing discover_pillars (No clusters)...")
pillars = await agent.discover_pillars()
print(f"Pillars found: {len(pillars)}")
if len(pillars) == 0:
print("✅ Correctly handled no clusters.")
else:
print("❌ Should have returned 0 pillars.")
# Scenario 2: Clusters found
mock_intelligence.cluster = AsyncMock(return_value=[[0, 1, 2], [3, 4]])
print("\nTesting discover_pillars (With clusters)...")
pillars = await agent.discover_pillars()
print(f"Pillars found: {len(pillars)}")
if len(pillars) == 2:
print("✅ Correctly identified pillars.")
print("Pillar 1 size:", pillars[0]['size'])
else:
print("❌ Failed to identify pillars.")
if __name__ == "__main__":
asyncio.run(test_content_guardian())
asyncio.run(test_strategy_architect())

View File

@@ -0,0 +1,43 @@
import os
import sys
import sqlite3
# Add backend to path
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from services.database import get_user_db_path
user_id = "user_33Gz1FPI86VDXhRY8QN4ragRFGN"
base_path = os.getcwd()
safe_user_id = "".join(c for c in user_id if c.isalnum() or c in ('-', '_'))
user_workspace = os.path.join(base_path, "workspace", f"workspace_{safe_user_id}")
path1 = os.path.join(user_workspace, "db", "alwrity.db")
path2 = os.path.join(user_workspace, "db", f"alwrity_{safe_user_id}.db")
print(f"Checking paths for user {user_id}:")
print(f"Legacy: {path1}")
print(f"Specific: {path2}")
def check_db(path):
if not os.path.exists(path):
print(f" [MISSING] {path}")
return
try:
conn = sqlite3.connect(path)
cursor = conn.cursor()
cursor.execute("SELECT count(*) FROM api_usage_logs")
count = cursor.fetchone()[0]
print(f" [EXISTS] {path} - Rows in api_usage_logs: {count}")
conn.close()
except Exception as e:
print(f" [EXISTS] {path} - Error reading: {e}")
check_db(path1)
check_db(path2)
print("-" * 30)
resolved = get_user_db_path(user_id)
print(f"Application resolves to: {resolved}")