Spaces:
Runtime error
Runtime error
Download src/recommendation_engine/local_llm.py from InnoTrack/Graduation_Project-v1.2: direct link, hf CLI and curl.
- Browser
- Download file 1.76 kB
-
https://huggingface.co/spaces/InnoTrack/Graduation_Project-v1.2/resolve/main/src/recommendation_engine/local_llm.py
- Command line
-
hf download hf://spaces/InnoTrack/Graduation_Project-v1.2/src/recommendation_engine/local_llm.py
-
curl -L -o local_llm.py https://huggingface.co/spaces/InnoTrack/Graduation_Project-v1.2/resolve/main/src/recommendation_engine/local_llm.py
1.76 kB
| import requests | |
| import time | |
| OLLAMA_URL = "http://localhost:11434/api/generate" | |
| MODEL_NAME = "mistral" | |
| def generate_local(prompt: str, retries: int = 2) -> str: | |
| """ | |
| Stable + fast local LLM call (Ollama) | |
| """ | |
| prompt = prompt[:1200] | |
| payload = { | |
| "model": MODEL_NAME, | |
| "prompt": prompt, | |
| "stream": False, | |
| "options": { | |
| "temperature": 0.7, | |
| "num_predict": 150, | |
| "top_p": 0.9 | |
| } | |
| } | |
| for attempt in range(retries): | |
| try: | |
| start_time = time.time() | |
| response = requests.post( | |
| OLLAMA_URL, | |
| json=payload, | |
| timeout=60 | |
| ) | |
| duration = round(time.time() - start_time, 2) | |
| if response.status_code != 200: | |
| print(f"❌ Local status {response.status_code}") | |
| time.sleep(1) | |
| continue | |
| try: | |
| data = response.json() | |
| except Exception: | |
| print("❌ Invalid JSON from local") | |
| time.sleep(1) | |
| continue | |
| result = data.get("response", "").strip() | |
| if not result or len(result) < 10: | |
| print(f"⚠️ Empty/short response ({duration}s)") | |
| time.sleep(1) | |
| continue | |
| print(f"✅ Local OK ({duration}s)") | |
| return result | |
| except requests.exceptions.Timeout: | |
| print(f"⏱️ Timeout ({attempt+1}/{retries})") | |
| time.sleep(1) | |
| except Exception as e: | |
| print("❌ Local error:", e) | |
| time.sleep(1) | |
| print("🚨 Local LLM failed → fallback") | |
| return "" | |