Spaces:
Sleeping

File size: 2,953 Bytes
2fe683b
765c205
 
2fe683b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
765c205
2fe683b
765c205
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2fe683b
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
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", "")
}