File size: 1,860 Bytes
6cc9163
 
2b3a35a
 
 
c9dd3b1
2b3a35a
 
c9dd3b1
6cc9163
2b3a35a
 
 
 
 
 
 
 
 
6cc9163
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2b3a35a
 
 
6cc9163
 
 
 
2b3a35a
 
 
 
 
 
 
 
 
 
6cc9163
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
from fastapi import FastAPI
from pydantic import BaseModel
import google.generativeai as genai
import os
from dotenv import load_dotenv

# Load environment variables
load_dotenv()

# Set up Gemini API Key
API_KEY = os.getenv("GEMINI_API_KEY")
if not API_KEY:
    raise ValueError("❌ Missing GEMINI_API_KEY. Please set it in the .env file.")

genai.configure(api_key=API_KEY)

# Load Gemini Model
model = genai.GenerativeModel("gemini-2.0-flash")

# Initialize FastAPI app
app = FastAPI(title="APTS Injury Recovery AI API")

# Define input structure
class InjuryInput(BaseModel):
    injury_type: str
    age: int
    past_injuries: str
    sport: str
    mobility: int  # 1-3 scale
    pressure: int  # 1-3 scale
    weight_bearing: int  # 1-3 scale

# Function to analyze injury and suggest recovery
def generate_recovery_plan(data: InjuryInput):
    severity_level = max(data.mobility, data.pressure, data.weight_bearing)

    recovery_prompt = f"""
    Generate a **personalized recovery plan** for an athlete with the following details:
    - **Injury Type**: {data.injury_type}
    - **Age**: {data.age} years
    - **Sport**: {data.sport}
    - **Past Injuries**: {data.past_injuries}
    - **Injury Severity Level**: {severity_level} (1 = Low, 2 = Medium, 3 = High)

    Provide:
    1️⃣ **Rehabilitation Plan**
    2️⃣ **Estimated Recovery Time**
    3️⃣ **Diet & Supplements**
    4️⃣ **Precautions**
    """

    recovery_response = model.generate_content(recovery_prompt)

    return {
        "injury_type": data.injury_type,
        "age": data.age,
        "sport": data.sport,
        "severity_level": severity_level,
        "recovery_plan": recovery_response.text
    }

# API Endpoint
@app.post("/analyze-injury")
async def analyze_injury(data: InjuryInput):
    response = generate_recovery_plan(data)
    return response