Update system to use remote Ollama host with graceful fallback handling
Browse files- .env +12 -14
- README.md +6 -0
- services/ollama_monitor.py +22 -13
- utils/config.py +2 -1
.env
CHANGED
|
@@ -1,20 +1,18 @@
|
|
| 1 |
-
# Hugging Face
|
| 2 |
-
HF_TOKEN=
|
| 3 |
-
HF_API_ENDPOINT_URL=https://
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
TAVILY_API_KEY=
|
| 7 |
-
OPENWEATHER_API_KEY=
|
| 8 |
-
NASA_API_KEY=
|
| 9 |
|
| 10 |
-
# Redis
|
| 11 |
REDIS_HOST=localhost
|
| 12 |
REDIS_PORT=6379
|
| 13 |
-
REDIS_USERNAME=
|
| 14 |
-
REDIS_PASSWORD=
|
| 15 |
|
| 16 |
-
#
|
| 17 |
LOCAL_MODEL_NAME=mistral-7b
|
| 18 |
-
|
| 19 |
-
# Ollama
|
| 20 |
-
OLLAMA_HOST=http://localhost:11434
|
|
|
|
| 1 |
+
# Hugging Face Settings
|
| 2 |
+
HF_TOKEN=your_huggingface_token_here
|
| 3 |
+
HF_API_ENDPOINT_URL=https://api-inference.huggingface.co/v1/
|
| 4 |
|
| 5 |
+
# API Keys
|
| 6 |
+
TAVILY_API_KEY=your_tavily_api_key_here
|
| 7 |
+
OPENWEATHER_API_KEY=your_openweather_api_key_here
|
| 8 |
+
NASA_API_KEY=your_nasa_api_key_here
|
| 9 |
|
| 10 |
+
# Redis Configuration
|
| 11 |
REDIS_HOST=localhost
|
| 12 |
REDIS_PORT=6379
|
| 13 |
+
REDIS_USERNAME=
|
| 14 |
+
REDIS_PASSWORD=
|
| 15 |
|
| 16 |
+
# Model Configuration
|
| 17 |
LOCAL_MODEL_NAME=mistral-7b
|
| 18 |
+
OLLAMA_HOST=https://a877ef1aa487.ngrok-free.app
|
|
|
|
|
|
README.md
CHANGED
|
@@ -21,3 +21,9 @@ A personal development assistant powered by LLMs.
|
|
| 21 |
- User sessions (Rob & Sarah)
|
| 22 |
- FastAPI backend
|
| 23 |
- Streamlit UI
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
- User sessions (Rob & Sarah)
|
| 22 |
- FastAPI backend
|
| 23 |
- Streamlit UI
|
| 24 |
+
|
| 25 |
+
## Deployment
|
| 26 |
+
|
| 27 |
+
The application is designed to work in Hugging Face Spaces environment. For local LLM inference, it connects to a remote Ollama instance via ngrok tunnel at . This allows the application to access powerful local models without requiring them to be installed directly in the Space.
|
| 28 |
+
|
| 29 |
+
In case the remote Ollama instance is unavailable, the system gracefully falls back to checking a local instance, and handles unavailability by showing appropriate status messages in the UI.
|
services/ollama_monitor.py
CHANGED
|
@@ -1,4 +1,5 @@
|
|
| 1 |
import requests
|
|
|
|
| 2 |
from utils.config import config
|
| 3 |
|
| 4 |
def check_ollama_status():
|
|
@@ -14,23 +15,31 @@ def check_ollama_status():
|
|
| 14 |
}
|
| 15 |
"""
|
| 16 |
ngrok_url = "https://a877ef1aa487.ngrok-free.app/"
|
| 17 |
-
local_url =
|
| 18 |
|
| 19 |
-
def _get_model_from_url(base_url):
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
if
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
return None
|
| 29 |
|
| 30 |
-
|
| 31 |
remote_model = _get_model_from_url(ngrok_url)
|
| 32 |
-
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
running = bool(model_loaded)
|
| 35 |
|
| 36 |
return {
|
|
|
|
| 1 |
import requests
|
| 2 |
+
import time
|
| 3 |
from utils.config import config
|
| 4 |
|
| 5 |
def check_ollama_status():
|
|
|
|
| 15 |
}
|
| 16 |
"""
|
| 17 |
ngrok_url = "https://a877ef1aa487.ngrok-free.app/"
|
| 18 |
+
local_url = "http://localhost:11434/" # Always check localhost as fallback
|
| 19 |
|
| 20 |
+
def _get_model_from_url(base_url, retries=3, delay=1):
|
| 21 |
+
"""Try to get model info with retry logic"""
|
| 22 |
+
for attempt in range(retries):
|
| 23 |
+
try:
|
| 24 |
+
response = requests.get(f"{base_url}/api/tags", timeout=5)
|
| 25 |
+
if response.status_code == 200:
|
| 26 |
+
models = response.json().get("models", [])
|
| 27 |
+
if models:
|
| 28 |
+
return models[0].get("name")
|
| 29 |
+
except Exception as e:
|
| 30 |
+
if attempt < retries - 1: # Don't sleep on last attempt
|
| 31 |
+
time.sleep(delay * (2 ** attempt)) # Exponential backoff
|
| 32 |
+
continue
|
| 33 |
return None
|
| 34 |
|
| 35 |
+
# First try remote ngrok URL, then fall back to local
|
| 36 |
remote_model = _get_model_from_url(ngrok_url)
|
| 37 |
+
local_model = None
|
| 38 |
+
|
| 39 |
+
if not remote_model: # Only check local if remote failed
|
| 40 |
+
local_model = _get_model_from_url(local_url)
|
| 41 |
+
|
| 42 |
+
model_loaded = remote_model or local_model
|
| 43 |
running = bool(model_loaded)
|
| 44 |
|
| 45 |
return {
|
utils/config.py
CHANGED
|
@@ -14,6 +14,7 @@ class Config:
|
|
| 14 |
self.redis_username = os.getenv("REDIS_USERNAME")
|
| 15 |
self.redis_password = os.getenv("REDIS_PASSWORD")
|
| 16 |
self.local_model_name = os.getenv("LOCAL_MODEL_NAME", "mistral-7b")
|
| 17 |
-
|
|
|
|
| 18 |
|
| 19 |
config = Config()
|
|
|
|
| 14 |
self.redis_username = os.getenv("REDIS_USERNAME")
|
| 15 |
self.redis_password = os.getenv("REDIS_PASSWORD")
|
| 16 |
self.local_model_name = os.getenv("LOCAL_MODEL_NAME", "mistral-7b")
|
| 17 |
+
# Use remote Ollama host as default, falling back to environment variable
|
| 18 |
+
self.ollama_host = os.getenv("OLLAMA_HOST", "https://a877ef1aa487.ngrok-free.app")
|
| 19 |
|
| 20 |
config = Config()
|