-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathroutine_generator.py
More file actions
144 lines (110 loc) · 4.38 KB
/
routine_generator.py
File metadata and controls
144 lines (110 loc) · 4.38 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# import os
# import sys
from openai_client import generate_response, load_exercises_from_file, client, generate_chat_completion
from openai import OpenAI
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
from fastapi.responses import JSONResponse
# 🧠 Fuerza UTF-8 en todo el entorno
# os.environ["PYTHONIOENCODING"] = "utf-8"
# sys.stdout.reconfigure(encoding='utf-8')
# ✅ Verifica la codificación por consola
#print("Codificación por defecto:", sys.getdefaultencoding())
app = FastAPI()
def generate_routine(goal: str, experience: str = "principiante") -> str:
"""
Generates a workout routine based on the user's goal and experience.
Args:
goal (str): The user's fitness goal (e.g., "strength", "weight loss").
experience (str): The user's experience level (e.g., "beginner", "advanced").
Returns:
dict: A dictionary containing the generated routine.
"""
try:
# Construct the AI prompt
prompt = (
f"Eres un entrenador personal experto. Genera una rutina de ejercicios "
f"para un usuario con el objetivo de '{goal}' y nivel de experiencia '{experience}'. "
f"Incluye ejercicios específicos y una breve descripción para cada uno."
)
# Call the OpenAI API to generate the routine
response = generate_chat_completion(
messages=[
{"role": "system", "content": "Eres un entrenador personal experto."},
{"role": "user", "content": prompt}
]
)
# Extract the AI-generated response
routine = response.choices[0].message.content.strip()
return {"goal": goal, "experience": experience, "routine": routine}
except Exception as e:
# Raise an exception if the API call fails
raise HTTPException(status_code=500, detail=f"Error generating routine: {str(e)}")
class RoutineRequest(BaseModel):
goal: str
experience: str = "principiante"
@app.get("/generate-routine")
def get_routine(goal: str, experience: str = "principiante"):
"""
Endpoint para generar una rutina de entrenamiento.
"""
try:
routine = generate_routine(goal, experience)
return {"routine": routine}
except ValueError as e:
raise HTTPException(status_code=500, detail="Error generating routine: " + str(e))
@app.post("/generate-routine/")
async def create_routine(request: RoutineRequest):
"""
Endpoint para generar una rutina de entrenamiento.
Args:
request (RoutineRequest): Datos de entrada con el objetivo y nivel de experiencia.
Returns:
dict: Rutina generada o mensaje de error.
"""
return generate_routine(request.goal, request.experience)
# Filter exercises by name
def get_exercise_by_name(name: str) -> dict:
"""
Filters exercises by name.
Args:
name (str): The name of the exercise to search for.
Returns:
dict: The matching exercise details.
Raises:
HTTPException: If no exercise is found.
"""
exercises = load_exercises_from_file()
for exercise in exercises:
if exercise["name"].lower() == name.lower():
return exercise
raise HTTPException(status_code=404, detail="Exercise not found.")
class ExerciseRequest(BaseModel):
name: str
@app.post("/get-exercise/")
async def get_exercise(request: ExerciseRequest):
"""
Endpoint to get exercise details by name.
Args:
request (ExerciseRequest): Input data with the exercise name.
Returns:
dict: The matching exercise details or an error message.
"""
return get_exercise_by_name(request.name)
# se ejecuta solo cuanto corras el script directamente
# commad: python routine_generator.py
if __name__ == "__main__":
try:
routine = generate_routine("ganar músculo", "principiante")
if routine:
with open("rutina.txt", "w", encoding="utf-8") as f:
f.write(routine)
print("✅ Rutina generada y guardada en rutina.txt")
else:
print("⚠️ No se generó rutina. Revisa error_log.txt")
except Exception as e:
with open("error_log.txt", "w", encoding="utf-8") as f:
f.write(str(e))
safe_error = str(e).encode("utf-8", errors="replace").decode("utf-8")
print("❌ Error. Detalles guardados en error_log.txt")
print("Mensaje:", safe_error)