File size: 7,905 Bytes
9dc895b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
═══════════════════════════════════════════════════════════════════════
   SIMPLE FIX - UPDATE YOUR SPACE (Takes 2 Minutes)
═══════════════════════════════════════════════════════════════════════

PROBLEM: Still getting timeout errors even with token set
CAUSE: Code is still using local models (setdefault doesn't force override)
SOLUTION: Replace with direct assignment to force HF API mode

───────────────────────────────────────────────────────────────────────
STEP 1: Open Your Space
───────────────────────────────────────────────────────────────────────

1. Go to your HuggingFace Space
2. Click "Files" tab
3. Click "app.py" to edit

───────────────────────────────────────────────────────────────────────
STEP 2: Find and Replace Lines 140-170
───────────────────────────────────────────────────────────────────────

FIND THIS (around line 140-170):

    # Set defaults for HuggingFace Spaces (can be overridden with Spaces Variables)
    os.environ.setdefault("USE_HF_API", "False")
    os.environ.setdefault("USE_LMSTUDIO", "False")
    os.environ.setdefault("DEBUG_MODE", os.getenv("DEBUG_MODE", "False"))
    os.environ.setdefault("LLM_BACKEND", "local")
    os.environ.setdefault("LLM_TIMEOUT", "120")
    os.environ.setdefault("MAX_TOKENS_PER_REQUEST", "1500")
    os.environ.setdefault("LLM_TEMPERATURE", "0.7")

    print("βœ… Configuration loaded for HuggingFace Spaces")

    # Auto-detect HuggingFace Spaces and force HF API (...)
    # ... (about 20 more lines of auto-detection code)

REPLACE WITH THIS (copy exactly):

    # FORCE HF API for HuggingFace Spaces deployment
    # Local models timeout on free tier - always use HF API when deployed
    print("πŸš€ Forcing HF API mode for HuggingFace Spaces deployment...")
    os.environ["USE_HF_API"] = "True"
    os.environ["USE_LMSTUDIO"] = "False"
    os.environ["LLM_BACKEND"] = "hf_api"
    os.environ["DEBUG_MODE"] = os.getenv("DEBUG_MODE", "False")
    os.environ["LLM_TIMEOUT"] = "180"  # 3 minutes
    os.environ["MAX_TOKENS_PER_REQUEST"] = "1500"
    os.environ["LLM_TEMPERATURE"] = "0.7"

    # Check if HF token is set (required for HF API)
    hf_token = os.getenv("HUGGINGFACE_TOKEN", "")
    if not hf_token:
        print("="*70)
        print("⚠️  ERROR: HUGGINGFACE_TOKEN not set!")
        print("   This is REQUIRED for HF API mode to work.")
        print("   Add it in Space Settings β†’ Repository Secrets")
        print("   Get token from: https://huggingface.co/settings/tokens")
        print("="*70)
    else:
        print("βœ… HuggingFace token detected")

    print("βœ… Configuration loaded for HuggingFace Spaces")

───────────────────────────────────────────────────────────────────────
STEP 3: Save and Commit
───────────────────────────────────────────────────────────────────────

1. Click "Commit changes to main"
2. Wait ~2 minutes for Space to restart

───────────────────────────────────────────────────────────────────────
STEP 4: Verify It Worked
───────────────────────────────────────────────────────────────────────

After restart, check the Logs tab. You should see:

    πŸš€ Forcing HF API mode for HuggingFace Spaces deployment...
    βœ… HuggingFace token detected
    βœ… Configuration loaded for HuggingFace Spaces
    πŸš€ TranscriptorAI Enterprise - LLM Backend: hf_api
    πŸ”§ USE_HF_API: True

When processing a file, you should see:

    INFO: Calling HF API: microsoft/Phi-3-mini-4k-instruct
    (NOT "Generating with local model")

───────────────────────────────────────────────────────────────────────
KEY DIFFERENCE: setdefault vs direct assignment
───────────────────────────────────────────────────────────────────────

OLD (doesn't work):
    os.environ.setdefault("USE_HF_API", "False")  ← Won't override existing value

NEW (works):
    os.environ["USE_HF_API"] = "True"  ← Always sets it to True

───────────────────────────────────────────────────────────────────────
IF STILL NOT WORKING
───────────────────────────────────────────────────────────────────────

Add this as the FIRST LINE in the analyze() function (around line 178):

    def analyze(files, file_type, user_comments, role_hint, debug_mode, interviewee_type,
                enable_pii_redaction, redaction_level, progress=gr.Progress()):
        """..."""

        # FORCE HF API MODE - add this as first line
        os.environ["USE_HF_API"] = "True"
        os.environ["LLM_BACKEND"] = "hf_api"
        print("πŸš€πŸš€πŸš€ FORCED HF API IN ANALYZE FUNCTION")

        # ... rest of function ...

This ensures it's set RIGHT before processing starts.

───────────────────────────────────────────────────────────────────────
CHECKLIST
───────────────────────────────────────────────────────────────────────

β–‘ Token added to Space Settings β†’ Repository Secrets
β–‘ Replaced lines 140-170 in app.py with new code
β–‘ Committed changes
β–‘ Space restarted
β–‘ Logs show "USE_HF_API: True"
β–‘ Logs show "Calling HF API" (not "local model")
β–‘ Processing completes without timeout
β–‘ Quality Score > 0.00

═══════════════════════════════════════════════════════════════════════
   IF ALL CHECKED: YOUR SPACE IS FIXED! πŸŽ‰
═══════════════════════════════════════════════════════════════════════