| import os |
| import requests |
|
|
| def generate_workout(name, age, goal, level, equipment, bmi): |
| |
| 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." |
|
|
| |
| |
| API_URL = f"{NGROK_URL.rstrip('/')}/v1/chat/completions" |
| |
| |
| headers = { |
| "Content-Type": "application/json", |
| "ngrok-skip-browser-warning": "true" |
| } |
|
|
| |
| 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: |
| |
| |
| 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)}" |