feat(company-explorer): Initial Web UI & Backend with Enrichment Flow
This commit introduces the foundational elements for the new "Company Explorer" web application, marking a significant step away from the legacy Google Sheets / CLI system. Key changes include: - Project Structure: A new directory with separate (FastAPI) and (React/Vite) components. - Data Persistence: Migration from Google Sheets to a local SQLite database () using SQLAlchemy. - Core Utilities: Extraction and cleanup of essential helper functions (LLM wrappers, text utilities) into . - Backend Services: , , for AI-powered analysis, and logic. - Frontend UI: Basic React application with company table, import wizard, and dynamic inspector sidebar. - Docker Integration: Updated and for multi-stage builds and sideloading. - Deployment & Access: Integrated into central Nginx proxy and dashboard, accessible via . Lessons Learned & Fixed during development: - Frontend Asset Loading: Addressed issues with Vite's path and FastAPI's . - TypeScript Configuration: Added and . - Database Schema Evolution: Solved errors by forcing a new database file and correcting override. - Logging: Implemented robust file-based logging (). This new foundation provides a powerful and maintainable platform for future B2B robotics lead generation.
This commit is contained in:
144
company-explorer/backend/lib/core_utils.py
Normal file
144
company-explorer/backend/lib/core_utils.py
Normal file
@@ -0,0 +1,144 @@
|
||||
import time
|
||||
import logging
|
||||
import random
|
||||
import os
|
||||
import re
|
||||
from functools import wraps
|
||||
from typing import Optional, Union, List
|
||||
|
||||
# Versuche neue Google GenAI Lib (v1.0+)
|
||||
try:
|
||||
from google import genai
|
||||
from google.genai import types
|
||||
HAS_NEW_GENAI = True
|
||||
except ImportError:
|
||||
HAS_NEW_GENAI = False
|
||||
|
||||
# Fallback auf alte Lib
|
||||
try:
|
||||
import google.generativeai as old_genai
|
||||
HAS_OLD_GENAI = True
|
||||
except ImportError:
|
||||
HAS_OLD_GENAI = False
|
||||
|
||||
from ..config import settings
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
# ==============================================================================
|
||||
# 1. DECORATORS
|
||||
# ==============================================================================
|
||||
|
||||
def retry_on_failure(max_retries: int = 3, delay: float = 2.0):
|
||||
"""
|
||||
Decorator for retrying functions with exponential backoff.
|
||||
"""
|
||||
def decorator(func):
|
||||
@wraps(func)
|
||||
def wrapper(*args, **kwargs):
|
||||
last_exception = None
|
||||
for attempt in range(max_retries):
|
||||
try:
|
||||
return func(*args, **kwargs)
|
||||
except Exception as e:
|
||||
last_exception = e
|
||||
# Don't retry on certain fatal errors (can be extended)
|
||||
if isinstance(e, ValueError) and "API Key" in str(e):
|
||||
raise e
|
||||
|
||||
wait_time = delay * (2 ** attempt) + random.uniform(0, 1)
|
||||
logger.warning(f"Retry {attempt + 1}/{max_retries} for '{func.__name__}' after error: {e}. Waiting {wait_time:.1f}s")
|
||||
time.sleep(wait_time)
|
||||
|
||||
logger.error(f"Function '{func.__name__}' failed after {max_retries} attempts.")
|
||||
raise last_exception
|
||||
return wrapper
|
||||
return decorator
|
||||
|
||||
# ==============================================================================
|
||||
# 2. TEXT TOOLS
|
||||
# ==============================================================================
|
||||
|
||||
def clean_text(text: str) -> str:
|
||||
"""Removes excess whitespace and control characters."""
|
||||
if not text:
|
||||
return ""
|
||||
text = str(text).strip()
|
||||
text = re.sub(r'\s+', ' ', text)
|
||||
return text
|
||||
|
||||
def normalize_string(s: str) -> str:
|
||||
"""Basic normalization (lowercase, stripped)."""
|
||||
return s.lower().strip() if s else ""
|
||||
|
||||
# ==============================================================================
|
||||
# 3. LLM WRAPPER (GEMINI)
|
||||
# ==============================================================================
|
||||
|
||||
@retry_on_failure(max_retries=3)
|
||||
def call_gemini(
|
||||
prompt: Union[str, List[str]],
|
||||
model_name: str = "gemini-2.0-flash",
|
||||
temperature: float = 0.3,
|
||||
json_mode: bool = False,
|
||||
system_instruction: Optional[str] = None
|
||||
) -> str:
|
||||
"""
|
||||
Unified caller for Gemini API. Prefers new `google.genai` library.
|
||||
"""
|
||||
api_key = settings.GEMINI_API_KEY
|
||||
if not api_key:
|
||||
raise ValueError("GEMINI_API_KEY is missing in configuration.")
|
||||
|
||||
# Option A: New Library (google-genai)
|
||||
if HAS_NEW_GENAI:
|
||||
try:
|
||||
client = genai.Client(api_key=api_key)
|
||||
config = {
|
||||
"temperature": temperature,
|
||||
"top_p": 0.95,
|
||||
"top_k": 40,
|
||||
"max_output_tokens": 8192,
|
||||
}
|
||||
if json_mode:
|
||||
config["response_mime_type"] = "application/json"
|
||||
|
||||
response = client.models.generate_content(
|
||||
model=model_name,
|
||||
contents=[prompt] if isinstance(prompt, str) else prompt,
|
||||
config=config,
|
||||
)
|
||||
if not response.text:
|
||||
raise ValueError("Empty response from Gemini")
|
||||
return response.text.strip()
|
||||
except Exception as e:
|
||||
logger.error(f"Error with google-genai lib: {e}")
|
||||
if not HAS_OLD_GENAI:
|
||||
raise e
|
||||
# Fallthrough to Option B
|
||||
|
||||
# Option B: Old Library (google-generativeai)
|
||||
if HAS_OLD_GENAI:
|
||||
try:
|
||||
old_genai.configure(api_key=api_key)
|
||||
generation_config = {
|
||||
"temperature": temperature,
|
||||
"top_p": 0.95,
|
||||
"top_k": 40,
|
||||
"max_output_tokens": 8192,
|
||||
}
|
||||
if json_mode:
|
||||
generation_config["response_mime_type"] = "application/json"
|
||||
|
||||
model = old_genai.GenerativeModel(
|
||||
model_name=model_name,
|
||||
generation_config=generation_config,
|
||||
system_instruction=system_instruction
|
||||
)
|
||||
response = model.generate_content(prompt)
|
||||
return response.text.strip()
|
||||
except Exception as e:
|
||||
logger.error(f"Error with google-generativeai lib: {e}")
|
||||
raise e
|
||||
|
||||
raise ImportError("No Google GenAI library installed (neither google-genai nor google-generativeai).")
|
||||
39
company-explorer/backend/lib/logging_setup.py
Normal file
39
company-explorer/backend/lib/logging_setup.py
Normal file
@@ -0,0 +1,39 @@
|
||||
import logging
|
||||
import sys
|
||||
import os
|
||||
from logging.handlers import RotatingFileHandler
|
||||
from ..config import settings
|
||||
|
||||
def setup_logging():
|
||||
log_file = os.path.join(settings.LOG_DIR, "company_explorer_debug.log")
|
||||
|
||||
# Create Formatter
|
||||
formatter = logging.Formatter(
|
||||
"%(asctime)s - %(name)s - %(levelname)s - %(message)s"
|
||||
)
|
||||
|
||||
# File Handler
|
||||
try:
|
||||
file_handler = RotatingFileHandler(log_file, maxBytes=10*1024*1024, backupCount=5)
|
||||
file_handler.setFormatter(formatter)
|
||||
file_handler.setLevel(logging.DEBUG)
|
||||
except Exception as e:
|
||||
print(f"FATAL: Could not create log file at {log_file}: {e}")
|
||||
return
|
||||
|
||||
# Console Handler
|
||||
console_handler = logging.StreamHandler(sys.stdout)
|
||||
console_handler.setFormatter(formatter)
|
||||
console_handler.setLevel(logging.INFO) # Keep console clean
|
||||
|
||||
# Root Logger Config
|
||||
root_logger = logging.getLogger()
|
||||
root_logger.setLevel(logging.DEBUG) # Catch ALL
|
||||
root_logger.addHandler(file_handler)
|
||||
root_logger.addHandler(console_handler)
|
||||
|
||||
# Silence noisy libs partially
|
||||
logging.getLogger("uvicorn.access").setLevel(logging.INFO)
|
||||
logging.getLogger("sqlalchemy.engine").setLevel(logging.INFO) # Set to DEBUG to see SQL queries!
|
||||
|
||||
logging.info(f"Logging initialized. Writing to {log_file}")
|
||||
Reference in New Issue
Block a user