hpyapali/tinyllama-workout
0
1import os2import gradio as gr3from fastapi import FastAPI, HTTPException4from transformers import pipeline, AutoTokenizer, AutoModelForCausalLM5import uvicorn6 7# โ
Load Model Configuration8MODEL_NAME = "hpyapali/tinyllama-workout"9HF_TOKEN = os.getenv("HF_TOKEN", "your_huggingface_api_key") # Replace with your actual Hugging Face API key10 11app = FastAPI()12 13try:14 print("๐ Loading Model...")15 tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME, token=HF_TOKEN)16 model = AutoModelForCausalLM.from_pretrained(MODEL_NAME, token=HF_TOKEN)17 pipe = pipeline("text-generation", model=model, tokenizer=tokenizer)18 print("โ
Model Loaded Successfully!")19except Exception as e:20 print(f"โ Error loading model: {e}")21 pipe = None22 23 24# โ
AI Function - Generates Structured Workout Recommendations25def recommend_next_workout(last_workouts: str):26 """27 Analyzes and ranks workouts based on intensity and heart rate drop.28 Provides a recommendation for the next workout.29 """30 if pipe is None:31 return "โ AI model not loaded."32 33 instruction = (34 "You are a fitness AI assistant specializing in analyzing workout effectiveness. "35 "Based on the last 7 workouts, rank them from the most to least effective based on:\n"36 "- Heart rate drop after workout (faster drop = better recovery)\n"37 "- Workout intensity (higher effort = more impact)\n"38 "- Duration (longer workouts generally contribute more)\n"39 "- Calories burned (higher calories = higher impact)\n"40 "- Variability (mixing workout types is important)\n\n"41 "### Last 7 Workouts:\n"42 )43 44 full_prompt = instruction + last_workouts + "\n\n### Ranking (Best to Least Effective):\n"45 46 try:47 print(f"๐ง AI Processing: {full_prompt}")48 result = pipe(49 full_prompt, 50 max_new_tokens=150, # ๐ผ Increased token limit for full ranking51 do_sample=True, # ๐ผ Enabled sampling for variability52 temperature=0.7, # ๐ผ Slight randomness for better insights53 top_p=0.9 # ๐ผ Limits unlikely outputs while keeping diversity54 )55 print(f"๐ Raw AI Output: {result}")56 57 if not result or not result[0]["generated_text"].strip():58 return "โ AI did not generate any output."59 60 response_text = result[0]["generated_text"].strip()61 62 # โ
Remove repeated prompt if AI echoes it63 if full_prompt in response_text:64 response_text = response_text.replace(full_prompt, "").strip()65 66 print(f"โ
AI Recommendation: {response_text}")67 return response_text68 except Exception as e:69 print(f"โ AI Processing Error: {e}")70 return "โ Error generating workout recommendation."71 72 73# โ
FastAPI Route - Returns AI Response Directly74@app.post("/gradio_api/call/predict")75async def predict(data: dict):76 try:77 last_workouts = data.get("data", [""])[0]78 if not last_workouts:79 raise HTTPException(status_code=400, detail="Invalid input")80 81 ai_response = recommend_next_workout(last_workouts)82 83 return {"data": [ai_response]} # โ
Directly returning structured response84 except Exception as e:85 return {"error": str(e)}86 87 88# โ
Gradio UI (Optional for Testing)89iface = gr.Interface(90 fn=recommend_next_workout,91 inputs="text",92 outputs="text",93 title="TinyLlama Workout Recommendations",94 description="Enter workout data to receive AI-powered recommendations."95)96 97# โ
Ensure Proper Gradio Launch98iface.launch(server_name="0.0.0.0", server_port=7860)99 100# โ
FastAPI Server Execution101if __name__ == "__main__":102 uvicorn.run(app, host="0.0.0.0", port=7860)103 