hpyapali commited on
Commit
1c8377a
Β·
verified Β·
1 Parent(s): da62dd2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -14
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
- # βœ… Load Model Configuration
 
 
 
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
- try:
20
- tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME, token=HF_TOKEN)
21
- model = AutoModelForCausalLM.from_pretrained(MODEL_NAME, token=HF_TOKEN)
22
- pipe = pipeline("text-generation", model=model, tokenizer=tokenizer)
23
- print("βœ… AI Model Loaded Successfully!")
24
- except Exception as e:
25
- print(f"❌ Error loading model: {e}")
26
- pipe = None
 
 
 
 
 
 
 
 
 
 
 
 
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
- # βœ… Start FastAPI
105
- if __name__ == "__main__":
 
 
 
 
 
 
 
 
 
 
 
 
 
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()