Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,32 +3,49 @@ import json
|
|
| 3 |
import uuid
|
| 4 |
import httpx
|
| 5 |
import gradio as gr
|
|
|
|
| 6 |
from fastapi import FastAPI, HTTPException, Request
|
| 7 |
-
from transformers import pipeline, AutoTokenizer, AutoModelForCausalLM
|
| 8 |
import uvicorn
|
| 9 |
import asyncio
|
| 10 |
|
| 11 |
-
# β
|
|
|
|
|
|
|
|
|
|
| 12 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 13 |
MODEL_NAME = "hpyapali/tinyllama-workout"
|
| 14 |
-
event_store = {}
|
| 15 |
|
| 16 |
app = FastAPI()
|
| 17 |
|
| 18 |
-
# β
Load AI Model
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
pipe
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
|
| 29 |
# β
AI Function - Processes and ranks workouts
|
| 30 |
def analyze_workouts(last_workouts: str):
|
| 31 |
"""Generates AI-based workout rankings based on heart rate recovery."""
|
|
|
|
| 32 |
if pipe is None:
|
| 33 |
return "β AI model is not loaded."
|
| 34 |
|
|
@@ -101,6 +118,25 @@ async def root():
|
|
| 101 |
return {"message": "Workout Analysis & Ranking AI is running!"}
|
| 102 |
|
| 103 |
|
| 104 |
-
# β
|
| 105 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
uvicorn.run(app, host="0.0.0.0", port=7861)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
import uuid
|
| 4 |
import httpx
|
| 5 |
import gradio as gr
|
| 6 |
+
import torch
|
| 7 |
from fastapi import FastAPI, HTTPException, Request
|
| 8 |
+
from transformers import pipeline, AutoTokenizer, AutoModelForCausalLM, set_default_dtype
|
| 9 |
import uvicorn
|
| 10 |
import asyncio
|
| 11 |
|
| 12 |
+
# β
Use float16 to reduce memory usage (for Hugging Face Spaces)
|
| 13 |
+
set_default_dtype(torch.float16)
|
| 14 |
+
|
| 15 |
+
# β
Hugging Face API Token
|
| 16 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 17 |
MODEL_NAME = "hpyapali/tinyllama-workout"
|
| 18 |
+
event_store = {} # Store AI responses for polling fallback
|
| 19 |
|
| 20 |
app = FastAPI()
|
| 21 |
|
| 22 |
+
# β
Lazy Load AI Model (to prevent Space timeout)
|
| 23 |
+
pipe = None
|
| 24 |
+
|
| 25 |
+
def get_pipeline():
|
| 26 |
+
global pipe
|
| 27 |
+
if pipe is None:
|
| 28 |
+
try:
|
| 29 |
+
print("π Loading AI Model...")
|
| 30 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME, token=HF_TOKEN)
|
| 31 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 32 |
+
MODEL_NAME,
|
| 33 |
+
token=HF_TOKEN,
|
| 34 |
+
torch_dtype=torch.float16, # Lower memory usage
|
| 35 |
+
device_map="auto" # Load on available device (CPU/GPU)
|
| 36 |
+
)
|
| 37 |
+
pipe = pipeline("text-generation", model=model, tokenizer=tokenizer)
|
| 38 |
+
print("β
AI Model Loaded Successfully!")
|
| 39 |
+
except Exception as e:
|
| 40 |
+
print(f"β Error loading model: {e}")
|
| 41 |
+
pipe = None
|
| 42 |
+
return pipe
|
| 43 |
|
| 44 |
|
| 45 |
# β
AI Function - Processes and ranks workouts
|
| 46 |
def analyze_workouts(last_workouts: str):
|
| 47 |
"""Generates AI-based workout rankings based on heart rate recovery."""
|
| 48 |
+
pipe = get_pipeline()
|
| 49 |
if pipe is None:
|
| 50 |
return "β AI model is not loaded."
|
| 51 |
|
|
|
|
| 118 |
return {"message": "Workout Analysis & Ranking AI is running!"}
|
| 119 |
|
| 120 |
|
| 121 |
+
# β
Gradio UI for Testing
|
| 122 |
+
iface = gr.Interface(
|
| 123 |
+
fn=analyze_workouts,
|
| 124 |
+
inputs="text",
|
| 125 |
+
outputs="text",
|
| 126 |
+
title="Workout Analysis & Ranking AI",
|
| 127 |
+
description="Enter workout data to analyze effectiveness, rank workouts, and receive improvement recommendations."
|
| 128 |
+
)
|
| 129 |
+
|
| 130 |
+
|
| 131 |
+
# β
Start Both FastAPI & Gradio
|
| 132 |
+
def start_gradio():
|
| 133 |
+
iface.launch(server_name="0.0.0.0", server_port=7860, share=True)
|
| 134 |
+
|
| 135 |
+
def start_fastapi():
|
| 136 |
uvicorn.run(app, host="0.0.0.0", port=7861)
|
| 137 |
+
|
| 138 |
+
# β
Run both servers in parallel
|
| 139 |
+
if __name__ == "__main__":
|
| 140 |
+
import threading
|
| 141 |
+
threading.Thread(target=start_gradio).start()
|
| 142 |
+
threading.Thread(target=start_fastapi).start()
|