Spaces:
Running
Running
Upload folder using huggingface_hub
Browse files- src/llm.py +10 -10
src/llm.py
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
"""
|
| 2 |
-
LLM module.
|
| 3 |
"""
|
| 4 |
|
| 5 |
import os
|
|
@@ -34,7 +34,7 @@ def _init_openrouter():
|
|
| 34 |
base_url="https://openrouter.ai/api/v1",
|
| 35 |
api_key=api_key,
|
| 36 |
)
|
| 37 |
-
logger.info("OpenRouter ready (
|
| 38 |
return True
|
| 39 |
except Exception as e:
|
| 40 |
logger.error(f"OpenRouter init failed: {e}")
|
|
@@ -53,7 +53,7 @@ def _init_hf():
|
|
| 53 |
model="meta-llama/Llama-3.3-70B-Instruct",
|
| 54 |
token=token
|
| 55 |
)
|
| 56 |
-
logger.info("HF Inference API ready (
|
| 57 |
return True
|
| 58 |
except Exception as e:
|
| 59 |
logger.error(f"HF Inference API init failed: {e}")
|
|
@@ -94,18 +94,18 @@ def _call_hf(messages: list) -> str:
|
|
| 94 |
|
| 95 |
|
| 96 |
def _call_with_fallback(messages: list) -> str:
|
| 97 |
-
"""Try
|
| 98 |
-
if
|
| 99 |
try:
|
| 100 |
-
return
|
| 101 |
except Exception as e:
|
| 102 |
-
logger.warning(f"
|
| 103 |
|
| 104 |
-
if
|
| 105 |
try:
|
| 106 |
-
return
|
| 107 |
except Exception as e:
|
| 108 |
-
logger.error(f"
|
| 109 |
|
| 110 |
raise Exception("All LLM providers failed")
|
| 111 |
|
|
|
|
| 1 |
"""
|
| 2 |
+
LLM module. HuggingFace Inference API as primary, OpenRouter as fallback.
|
| 3 |
"""
|
| 4 |
|
| 5 |
import os
|
|
|
|
| 34 |
base_url="https://openrouter.ai/api/v1",
|
| 35 |
api_key=api_key,
|
| 36 |
)
|
| 37 |
+
logger.info("OpenRouter ready (fallback)")
|
| 38 |
return True
|
| 39 |
except Exception as e:
|
| 40 |
logger.error(f"OpenRouter init failed: {e}")
|
|
|
|
| 53 |
model="meta-llama/Llama-3.3-70B-Instruct",
|
| 54 |
token=token
|
| 55 |
)
|
| 56 |
+
logger.info("HF Inference API ready (primary)")
|
| 57 |
return True
|
| 58 |
except Exception as e:
|
| 59 |
logger.error(f"HF Inference API init failed: {e}")
|
|
|
|
| 94 |
|
| 95 |
|
| 96 |
def _call_with_fallback(messages: list) -> str:
|
| 97 |
+
"""Try HF first, then OpenRouter."""
|
| 98 |
+
if _hf_ready and _hf_client:
|
| 99 |
try:
|
| 100 |
+
return _call_hf(messages)
|
| 101 |
except Exception as e:
|
| 102 |
+
logger.warning(f"HF Inference failed: {e}, trying OpenRouter")
|
| 103 |
|
| 104 |
+
if _openrouter_ready and _openrouter_client:
|
| 105 |
try:
|
| 106 |
+
return _call_openrouter(messages)
|
| 107 |
except Exception as e:
|
| 108 |
+
logger.error(f"OpenRouter also failed: {e}")
|
| 109 |
|
| 110 |
raise Exception("All LLM providers failed")
|
| 111 |
|