File size: 2,333 Bytes
4373270 46be78c 4373270 7ce0582 b5efaed 7ce0582 b5efaed 46be78c b5efaed 7ce0582 b5efaed 3bb40e0 b5efaed 3bb40e0 2733c05 b5efaed 46be78c b5efaed 6b73c43 0869059 6b73c43 6730036 6b73c43 f1745d7 3a6cd92 46be78c ae8853a b5efaed 7ce0582 b5efaed | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | import os
import requests
def generate_workout(name, age, goal, level, equipment, bmi):
# 1. Pull the URL you saved in Hugging Face Secrets
NGROK_URL = os.getenv("NGROK_URL")
if not NGROK_URL:
return "Error: NGROK_URL not found in Hugging Face Secrets. Please add it in Settings."
# 2. Point to the local LM Studio server endpoint
# We use /v1/chat/completions to match LM Studio's OpenAI-style API
API_URL = f"{NGROK_URL.rstrip('/')}/v1/chat/completions"
# 3. CRITICAL: Add the ngrok-skip header for the free tier
headers = {
"Content-Type": "application/json",
"ngrok-skip-browser-warning": "true"
}
# 4. Create the request for your local Llama model
payload = {
"model": "llama-3.2-3b-instruct",
"messages": [
{
"role": "system",
"content": "You are a fitness coach. Generate a 5-day workout plan. Each day must include exactly 3 exercises with sets and reps. Keep it concise."
},
{
"role": "user",
"content": f"Age: {age}, Goal: {goal}, Level: {level}, Equipment: {equipment}. Provide 3 exercises per day with sets and reps. Add a short title for each day (e.g., Upper Body, Cardio)."
}
],
"max_tokens": 350,
"temperature": 0.5,
"stop": ["\n\n\n"]
}
try:
# 5. Send the request to your home computer
# Timeout is set high (180s) because local CPUs can take longer than cloud GPUs
response = requests.post(API_URL, headers=headers, json=payload, timeout=180)
if response.status_code == 200:
result = response.json()
if "choices" in result and len(result["choices"]) > 0:
return result["choices"][0]["message"]["content"]
return "Error: Received an empty response from your local AI."
else:
return f"Server Error ({response.status_code}): Ensure LM Studio and ngrok are both running on your PC."
except requests.exceptions.Timeout:
return "The request timed out. Your computer is taking too long to process the plan."
except Exception as e:
return f"Connection Failed: Is your CMD window still open with ngrok? Error: {str(e)}" |