Basementup commited on
Commit
81af1b7
·
verified ·
1 Parent(s): ac3881c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -14
app.py CHANGED
@@ -12,6 +12,10 @@ try:
12
  SPACES_ENABLED = True
13
  except ImportError:
14
  SPACES_ENABLED = False
 
 
 
 
15
 
16
  # All dependencies are installed via requirements.txt / apt.txt
17
  from pypdf import PdfReader
@@ -35,22 +39,25 @@ _MODELS = [
35
  def call_llama(prompt: str, max_tokens: int = 2000) -> str:
36
  token = os.environ.get("HF_TOKEN", "")
37
  headers = {"Authorization": f"Bearer {token}"} if token else {}
 
 
 
 
38
  for model in _MODELS:
39
- for attempt in range(4):
40
  try:
41
- fmt = prompt
42
  resp = _req.post(
43
  _FREE_API.format(model=model),
44
  headers=headers,
45
  json={
46
- "inputs": fmt,
47
  "parameters": {
48
  "max_new_tokens": max_tokens,
49
  "temperature": 0.7,
50
  "return_full_text": False,
51
  "do_sample": True,
52
  },
53
- "options": {"wait_for_model": True}
54
  },
55
  timeout=180
56
  )
@@ -61,19 +68,30 @@ def call_llama(prompt: str, max_tokens: int = 2000) -> str:
61
  text = result[0].get("generated_text", "").strip()
62
  if text:
63
  return text
64
- elif resp.status_code == 503:
65
- time.sleep(30 * (attempt + 1))
 
 
 
 
 
 
 
66
  continue
67
- elif resp.status_code in [429, 402]:
 
 
68
  break
69
- else:
70
- time.sleep(10)
71
- continue
72
- except Exception:
73
- time.sleep(10)
 
 
74
  continue
75
 
76
- return "Could not reach AI. Models may be loading — please try again in 30 seconds."
77
 
78
  # =============================================================================
79
  # FILE EXTRACTION
@@ -490,4 +508,3 @@ if __name__ == "__main__":
490
  server_port=7860,
491
  show_error=True,
492
  share=False # Spaces handles sharing automatically
493
- )
 
12
  SPACES_ENABLED = True
13
  except ImportError:
14
  SPACES_ENABLED = False
15
+ class spaces:
16
+ @staticmethod
17
+ def GPU(f):
18
+ return f
19
 
20
  # All dependencies are installed via requirements.txt / apt.txt
21
  from pypdf import PdfReader
 
39
  def call_llama(prompt: str, max_tokens: int = 2000) -> str:
40
  token = os.environ.get("HF_TOKEN", "")
41
  headers = {"Authorization": f"Bearer {token}"} if token else {}
42
+
43
+ # Add default header to prevent gating issues
44
+ headers["User-Agent"] = "Phoenix-Framework/1.0"
45
+
46
  for model in _MODELS:
47
+ for attempt in range(5):
48
  try:
 
49
  resp = _req.post(
50
  _FREE_API.format(model=model),
51
  headers=headers,
52
  json={
53
+ "inputs": prompt,
54
  "parameters": {
55
  "max_new_tokens": max_tokens,
56
  "temperature": 0.7,
57
  "return_full_text": False,
58
  "do_sample": True,
59
  },
60
+ "options": {"wait_for_model": True, "use_cache": False}
61
  },
62
  timeout=180
63
  )
 
68
  text = result[0].get("generated_text", "").strip()
69
  if text:
70
  return text
71
+ elif isinstance(result, dict) and "generated_text" in result:
72
+ text = result.get("generated_text", "").strip()
73
+ if text:
74
+ return text
75
+
76
+ # 503 = model loading, retry with backoff
77
+ if resp.status_code == 503:
78
+ wait_time = min(60 * (attempt + 1), 300) # cap at 5 min
79
+ time.sleep(wait_time)
80
  continue
81
+
82
+ # 429/402 = rate limit or payment issue, skip model
83
+ if resp.status_code in [429, 402]:
84
  break
85
+
86
+ # Other errors, retry after delay
87
+ time.sleep(5 * (attempt + 1))
88
+ continue
89
+
90
+ except Exception as e:
91
+ time.sleep(5 * (attempt + 1))
92
  continue
93
 
94
+ return "Could not reach AI models. Please check: 1) HF_TOKEN env var is set, 2) Network connection, 3) Hugging Face API status (try again in 60 seconds)."
95
 
96
  # =============================================================================
97
  # FILE EXTRACTION
 
508
  server_port=7860,
509
  show_error=True,
510
  share=False # Spaces handles sharing automatically