Amrita09 commited on
Commit
6cc9163
·
verified ·
1 Parent(s): fad984b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -29
app.py CHANGED
@@ -1,4 +1,5 @@
1
- import gradio as gr
 
2
  import google.generativeai as genai
3
  import os
4
  from dotenv import load_dotenv
@@ -6,7 +7,7 @@ from dotenv import load_dotenv
6
  # Load environment variables
7
  load_dotenv()
8
 
9
- # Set API Key securely
10
  API_KEY = os.getenv("GEMINI_API_KEY")
11
  if not API_KEY:
12
  raise ValueError("❌ Missing GEMINI_API_KEY. Please set it in the .env file.")
@@ -16,16 +17,29 @@ genai.configure(api_key=API_KEY)
16
  # Load Gemini Model
17
  model = genai.GenerativeModel("gemini-2.0-flash")
18
 
19
- # Function for AI-powered injury risk and recovery analysis
20
- def injury_recovery(injury_type, age, past_injuries, sport, mobility, pressure, weight_bearing):
21
- severity_level = max(mobility, pressure, weight_bearing)
 
 
 
 
 
 
 
 
 
 
 
 
 
22
 
23
  recovery_prompt = f"""
24
  Generate a **personalized recovery plan** for an athlete with the following details:
25
- - **Injury Type**: {injury_type}
26
- - **Age**: {age} years
27
- - **Sport**: {sport}
28
- - **Past Injuries**: {past_injuries}
29
  - **Injury Severity Level**: {severity_level} (1 = Low, 2 = Medium, 3 = High)
30
 
31
  Provide:
@@ -36,23 +50,17 @@ def injury_recovery(injury_type, age, past_injuries, sport, mobility, pressure,
36
  """
37
 
38
  recovery_response = model.generate_content(recovery_prompt)
39
- return recovery_response.text
40
-
41
- # Gradio UI
42
- iface = gr.Interface(
43
- fn=injury_recovery,
44
- inputs=[
45
- gr.Textbox(label="Injury Type"),
46
- gr.Number(label="Age"),
47
- gr.Textbox(label="Past Injuries"),
48
- gr.Textbox(label="Sport"),
49
- gr.Slider(1, 3, step=1, label="Mobility Test (1 = Low, 3 = High)"),
50
- gr.Slider(1, 3, step=1, label="Pressure Test (1 = Low, 3 = High)"),
51
- gr.Slider(1, 3, step=1, label="Weight-Bearing Test (1 = Low, 3 = High)"),
52
- ],
53
- outputs=gr.Textbox(label="AI-Powered Recovery Plan"),
54
- title="AI-Powered Injury Recovery Assistant",
55
- description="Enter injury details and severity test results to get a personalized recovery plan.",
56
- )
57
-
58
- iface.launch()
 
1
+ from fastapi import FastAPI
2
+ from pydantic import BaseModel
3
  import google.generativeai as genai
4
  import os
5
  from dotenv import load_dotenv
 
7
  # Load environment variables
8
  load_dotenv()
9
 
10
+ # Set up Gemini API Key
11
  API_KEY = os.getenv("GEMINI_API_KEY")
12
  if not API_KEY:
13
  raise ValueError("❌ Missing GEMINI_API_KEY. Please set it in the .env file.")
 
17
  # Load Gemini Model
18
  model = genai.GenerativeModel("gemini-2.0-flash")
19
 
20
+ # Initialize FastAPI app
21
+ app = FastAPI(title="APTS Injury Recovery AI API")
22
+
23
+ # Define input structure
24
+ class InjuryInput(BaseModel):
25
+ injury_type: str
26
+ age: int
27
+ past_injuries: str
28
+ sport: str
29
+ mobility: int # 1-3 scale
30
+ pressure: int # 1-3 scale
31
+ weight_bearing: int # 1-3 scale
32
+
33
+ # Function to analyze injury and suggest recovery
34
+ def generate_recovery_plan(data: InjuryInput):
35
+ severity_level = max(data.mobility, data.pressure, data.weight_bearing)
36
 
37
  recovery_prompt = f"""
38
  Generate a **personalized recovery plan** for an athlete with the following details:
39
+ - **Injury Type**: {data.injury_type}
40
+ - **Age**: {data.age} years
41
+ - **Sport**: {data.sport}
42
+ - **Past Injuries**: {data.past_injuries}
43
  - **Injury Severity Level**: {severity_level} (1 = Low, 2 = Medium, 3 = High)
44
 
45
  Provide:
 
50
  """
51
 
52
  recovery_response = model.generate_content(recovery_prompt)
53
+
54
+ return {
55
+ "injury_type": data.injury_type,
56
+ "age": data.age,
57
+ "sport": data.sport,
58
+ "severity_level": severity_level,
59
+ "recovery_plan": recovery_response.text
60
+ }
61
+
62
+ # API Endpoint
63
+ @app.post("/analyze-injury")
64
+ async def analyze_injury(data: InjuryInput):
65
+ response = generate_recovery_plan(data)
66
+ return response