Update model.py
Browse files
model.py
CHANGED
|
@@ -1,25 +1,58 @@
|
|
| 1 |
-
from huggingface_hub import InferenceClient
|
| 2 |
import os
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
-
def
|
| 5 |
-
|
| 6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
token=HF_TOKENN
|
| 11 |
-
)
|
| 12 |
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
],
|
| 18 |
-
max_tokens=900,
|
| 19 |
-
temperature=0.7
|
| 20 |
-
)
|
| 21 |
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
except Exception as e:
|
| 25 |
-
return f"Error: {str(e)}"
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
+
import requests
|
| 3 |
+
import json
|
| 4 |
+
import time
|
| 5 |
|
| 6 |
+
def generate_workout(name, age, goal, level, equipment, bmi):
|
| 7 |
+
# Get HF Token from environment secrets
|
| 8 |
+
hf_token = os.getenv("HUGGINGFACE_TOKEN")
|
| 9 |
+
|
| 10 |
+
if not hf_token:
|
| 11 |
+
return "Error: HUGGINGFACE_TOKEN not found. Please add it to your Hugging Face Space Settings > Secrets."
|
| 12 |
+
|
| 13 |
+
# Using the most stable Serverless Inference endpoint (OpenAI-compatible)
|
| 14 |
+
# This path is the new standard that replaces the old 410-Gone endpoint
|
| 15 |
+
api_url = "https://api-inference.huggingface.co/v1/chat/completions"
|
| 16 |
+
headers = {
|
| 17 |
+
"Authorization": f"Bearer {hf_token}",
|
| 18 |
+
"Content-Type": "application/json"
|
| 19 |
+
}
|
| 20 |
|
| 21 |
+
# Model ID - Llama-3.2-3B-Instruct is extremely reliable and 'warm' on the free tier
|
| 22 |
+
model_id = "meta-llama/Llama-3.2-3B-Instruct"
|
|
|
|
|
|
|
| 23 |
|
| 24 |
+
messages = [
|
| 25 |
+
{"role": "system", "content": "You are a professional fitness coach. Return ONLY the plan in Markdown. No filler."},
|
| 26 |
+
{"role": "user", "content": f"Create a 5-day workout for {name}. Goal: {goal}, Level: {level}, Equipment: {equipment}, BMI: {bmi}. Include Day headers and 3-4 exercises per day."}
|
| 27 |
+
]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
+
payload = {
|
| 30 |
+
"model": model_id,
|
| 31 |
+
"messages": messages,
|
| 32 |
+
"max_tokens": 1200,
|
| 33 |
+
"temperature": 0.5,
|
| 34 |
+
"stream": False
|
| 35 |
+
}
|
| 36 |
|
| 37 |
+
try:
|
| 38 |
+
# We try up to 3 times if the model is loading
|
| 39 |
+
for attempt in range(3):
|
| 40 |
+
response = requests.post(api_url, headers=headers, json=payload, timeout=120)
|
| 41 |
+
|
| 42 |
+
if response.status_code == 200:
|
| 43 |
+
result = response.json()
|
| 44 |
+
if "choices" in result and len(result["choices"]) > 0:
|
| 45 |
+
return result["choices"][0]["message"]["content"]
|
| 46 |
+
return f"Error: No content in response: {result}"
|
| 47 |
+
|
| 48 |
+
# If model is loading, wait and retry
|
| 49 |
+
elif response.status_code == 503:
|
| 50 |
+
time.sleep(15)
|
| 51 |
+
continue
|
| 52 |
+
else:
|
| 53 |
+
break
|
| 54 |
+
|
| 55 |
+
return f"Error: API returned status {response.status_code} - {response.text}"
|
| 56 |
+
|
| 57 |
except Exception as e:
|
| 58 |
+
return f"Error: Connection failed: {str(e)}"
|