from fastapi import FastAPI from transformers import pipeline # NOTE - we configure docs_url to serve the interactive Docs at the root path # of the app. This way, we can use the docs as a landing page for the app on Spaces. app = FastAPI(docs_url="/") pipe = pipeline("text2text-generation", model="google/flan-t5-small") def calculate_food(activity, weight): """ Calculates the recommended amount of dog food based on activity level and weight. Args: activity: The dog's activity level, as a number from 1 to 5. weight: The dog's weight in kilograms. Returns: A dictionary containing the recommended amount of food in cups. """ # Calculate the resting energy requirement (RER). rer = 70 * weight ** 0.75 # Multiply the RER by the appropriate factor to account for the dog's activity level. activity_factor = { 1: 1.2, 2: 1.4, 3: 1.6, 4: 1.8, 5: 2.0, } recommended_food = rer * activity_factor[activity] / weight return {"recommendedFood": round(recommended_food, 2)} @app.get("/calculate-food") def calculate_food_endpoint(activity: int, weight: int): """ Calculates the recommended amount of dog food based on activity level and weight. Args: activity: The dog's activity level, as a number from 1 to 5. weight: The dog's weight in kilograms. Returns: A JSON object containing the recommended amount of food in cups. """ result = calculate_food(activity, weight) return result if __name__ == "__main__": import uvicorn uvicorn.run(app, host="0.0.0.0", port=8000)