Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -16,12 +16,15 @@ import bm25s
|
|
| 16 |
import aiohttp
|
| 17 |
from bs4 import BeautifulSoup
|
| 18 |
|
| 19 |
-
# 🔥 CRITICAL HOTFIX
|
|
|
|
| 20 |
import transformers.utils
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
if not hasattr(
|
| 24 |
-
|
|
|
|
|
|
|
| 25 |
|
| 26 |
from transformers import AutoTokenizer, pipeline
|
| 27 |
from optimum.onnxruntime import ORTModelForCausalLM
|
|
@@ -31,13 +34,13 @@ import onnxruntime as ort
|
|
| 31 |
TOTAL_CORES = multiprocessing.cpu_count()
|
| 32 |
AI_CORES = max(1, int(TOTAL_CORES * 0.4))
|
| 33 |
|
| 34 |
-
#
|
| 35 |
try:
|
| 36 |
p = psutil.Process()
|
| 37 |
p.cpu_affinity(list(range(AI_CORES)))
|
| 38 |
print(f"🖥️ Total Cores: {TOTAL_CORES} | 🔒 Process strictly bound to {AI_CORES} cores.")
|
| 39 |
-
except
|
| 40 |
-
print(f"🖥️ Total Cores: {TOTAL_CORES} | Allocating {AI_CORES} cores to AI.")
|
| 41 |
|
| 42 |
raw_data = []
|
| 43 |
suggestion_list = []
|
|
@@ -386,5 +389,6 @@ async def ai_mode_chat_get(q: str = Query(...)):
|
|
| 386 |
async def ai_mode_chat_post(q: str = Form(...)):
|
| 387 |
return await process_rag_chat(q)
|
| 388 |
|
|
|
|
| 389 |
if __name__ == "__main__":
|
| 390 |
-
uvicorn.run(
|
|
|
|
| 16 |
import aiohttp
|
| 17 |
from bs4 import BeautifulSoup
|
| 18 |
|
| 19 |
+
# 🔥 CRITICAL HOTFIX FOR HUGGING FACE SPACES
|
| 20 |
+
# Bypasses the Optimum crash with the latest Transformers version natively on Python 3.13
|
| 21 |
import transformers.utils
|
| 22 |
+
import transformers.utils.import_utils
|
| 23 |
+
for mod in [transformers.utils, transformers.utils.import_utils]:
|
| 24 |
+
if not hasattr(mod, 'is_tf_available'):
|
| 25 |
+
mod.is_tf_available = lambda: False
|
| 26 |
+
if not hasattr(mod, 'is_torch_available'):
|
| 27 |
+
mod.is_torch_available = lambda: True
|
| 28 |
|
| 29 |
from transformers import AutoTokenizer, pipeline
|
| 30 |
from optimum.onnxruntime import ORTModelForCausalLM
|
|
|
|
| 34 |
TOTAL_CORES = multiprocessing.cpu_count()
|
| 35 |
AI_CORES = max(1, int(TOTAL_CORES * 0.4))
|
| 36 |
|
| 37 |
+
# Try to strictly enforce CPU Affinity, fallback safely if Docker/Space restricts permissions
|
| 38 |
try:
|
| 39 |
p = psutil.Process()
|
| 40 |
p.cpu_affinity(list(range(AI_CORES)))
|
| 41 |
print(f"🖥️ Total Cores: {TOTAL_CORES} | 🔒 Process strictly bound to {AI_CORES} cores.")
|
| 42 |
+
except Exception as e:
|
| 43 |
+
print(f"🖥️ Total Cores: {TOTAL_CORES} | Allocating {AI_CORES} cores to AI. (Strict affinity skipped: {e})")
|
| 44 |
|
| 45 |
raw_data = []
|
| 46 |
suggestion_list = []
|
|
|
|
| 389 |
async def ai_mode_chat_post(q: str = Form(...)):
|
| 390 |
return await process_rag_chat(q)
|
| 391 |
|
| 392 |
+
# Using `app` variable directly prevents Spaces routing errors if the file is named differently
|
| 393 |
if __name__ == "__main__":
|
| 394 |
+
uvicorn.run(app, host="0.0.0.0", port=7860, workers=1)
|