Spaces:
Sleeping

backendAPI / config.py
changweikoy's picture
Fix sentiment model VersionMismatch and clean_text reference issue
765c205
Raw History Blame Contribute Delete
2.95 kB
import os
import json
import tempfile
from dotenv import load_dotenv
# Load environment variables from .env file (if exists)
load_dotenv()
# Configuration file for backend server
# Server IP address - 0.0.0.0 to listen on all network interfaces
# This allows the Android emulator to connect via 10.0.2.2
SERVER_IP = os.getenv("SERVER_IP", "0.0.0.0")
# Server port - 7860 for Hugging Face Spaces, 5000 for local development
SERVER_PORT = int(os.getenv("PORT", os.getenv("SERVER_PORT", "7860")))
# Gemini API Key - REQUIRED for AI chat functionality
# For Hugging Face Spaces: Add as a Secret named GEMINI_API_KEY
GEMINI_API_KEY = os.getenv("GEMINI_API_KEY", "")
# Get full server URL
def get_server_url():
return f"http://{SERVER_IP}:{SERVER_PORT}"
# ============================================================================
# Google Cloud Speech-to-Text configuration
# ============================================================================
# For Hugging Face Spaces: Add the entire JSON content as a Secret named GCP_CREDENTIALS_JSON
# For local development: Use gcp_key.json file
def get_gcp_credentials_file():
"""
Get GCP credentials file path.
Priority:
1. GCP_CREDENTIALS_JSON env var (for Hugging Face - contains full JSON)
2. GCP_SERVICE_ACCOUNT_FILE env var (path to file)
3. Default file path 'gcp_key.json'
"""
# Check if credentials are provided as JSON string (Hugging Face Secrets)
gcp_json = os.getenv("GCP_CREDENTIALS_JSON")
if gcp_json:
try:
# Parse to validate it's valid JSON
creds_dict = json.loads(gcp_json)
# Write to a temp file that can be used by the SDK
temp_file = tempfile.NamedTemporaryFile(
mode='w',
suffix='.json',
delete=False
)
json.dump(creds_dict, temp_file)
temp_file.close()
print(f">> GCP credentials loaded from environment variable")
return temp_file.name
except json.JSONDecodeError:
print(">> WARNING: GCP_CREDENTIALS_JSON is not valid JSON")
# Fall back to file path
file_path = os.getenv("GCP_SERVICE_ACCOUNT_FILE", "gcp_key.json")
if os.path.exists(file_path):
print(f">> GCP credentials loaded from file: {file_path}")
return file_path
print(f">> WARNING: GCP credentials not found")
return None
# Get the credentials file path
GCP_SERVICE_ACCOUNT_FILE = get_gcp_credentials_file()
SPEECH_LANGUAGE_CODE = os.getenv("SPEECH_LANGUAGE_CODE", "en-US")
# Database configuration (Optional - handled by PHP backend in production)
ENABLE_FLASK_DB = os.getenv("ENABLE_FLASK_DB", "false").lower() == "true"
DB_CONFIG = {
'host': os.getenv("DB_HOST", "localhost"),
'user': os.getenv("DB_USER", ""),
'password': os.getenv("DB_PASSWORD", ""),
'database': os.getenv("DB_NAME", "")
}