去年夏天,当我发现 Gemini API 开发者竞赛时,我认为这是一个亲身体验 GenAI 应用程序的绝佳机会。作为健身爱好者,我们(我和 Manos Chainakis)想到创建一款可以生成个性化锻炼和营养计划的应用程序,将人工智能与人类教练的偏好相结合。这就是健身部落 AI 的诞生。这篇文章将带您了解我使用的开发过程和技术堆栈,重点是 GenAI 方面。
Fitness Tribe AI 将人类教练的专业知识与人工智能模型的功能相结合,创建满足每个运动员需求和目标的定制健身计划。
技术堆栈的主要组成部分是:
FastAPI 是 Fitness Tribe AI 的支柱,负责处理 AI 驱动的分析。
该项目的结构如下:
fitness-tribe-ai/ ├── app/ │ ├── main.py # Entry point for FastAPI app │ ├── routers/ # Handles API routes (meals, nutrition, workouts) │ ├── models/ # Manages interactions with AI models │ ├── schemas/ # Pydantic models for input validation │ ├── services/ # Business logic for each feature
from fastapi import FastAPI from app.routers import meals, nutrition, workouts app = FastAPI() app.include_router(meals.router) app.include_router(nutrition.router) app.include_router(workouts.router)
class GeminiModel: @staticmethod def analyze_meal(image_data): prompt = ( "Analyze the following meal image and provide the name of the food, " "total calorie count, and calories per ingredient..." "Respond in the following JSON format:" "{'food_name': '<food name>' ...}" ) image = Image.open(BytesIO(image_data)) response = model.generate_content([prompt, image]) return response.text
from pydantic import BaseModel from typing import Dict class Meal(BaseModel): food_name: str total_calories: int calories_per_ingredient: Dict[str, int]
from app.models.gemini_model import GeminiModel from app.schemas.meal import Meal from fastapi import HTTPException import logging import json def analyze_meal(image_data: bytes) -> Meal: try: result_text = GeminiModel.analyze_meal(image_data) if not result_text: raise HTTPException(status_code=500, detail="No response from Gemini API") clean_result_text = result_text.strip("``` json\n").strip(" ```") result = json.loads(clean_result_text) return Meal( food_name=result.get("food_name"), total_calories=result.get("total_calories"), calories_per_ingredient=result.get("calories_per_ingredient"), ) except Exception as e: raise HTTPException(status_code=500, detail=str(e))
通过利用 FastAPI 的模块化结构、清晰的 API 路由、用于数据验证的 Pydantic 以及组织良好的服务逻辑,Fitness Tribe AI 可以通过自定义提示有效处理 AI 模型交互,从而提供个性化的健身和营养见解。您可以在这里找到完整的存储库:
Fitness Tribe AI 是一款由人工智能驱动的健身 API,专为教练和运动员设计。该 API 通过分析膳食照片和人工智能驱动的锻炼构建器提供膳食分析功能,该构建器可以根据运动员资料生成锻炼计划。健身部落AI已构建双子座模型。
fitness-tribe-ai/ ├── app/ │ ├── __init__.py │ ├── main.py │ ├── models/ │ │ ├── __init__.py │ │ ├── gemini_model.py │ ├── routers/ │ │ ├── __init__.py │ │ ├── meals.py │ │ ├── nutrition.py │ │ ├── workouts.py │ ├── schemas/ │ │ ├── __init__.py │ │ ├── meal.py │ │ ├── nutrition.py │ │ ├──
For user authentication and account management, I used Supabase, which provided a secure, scalable solution without requiring a custom-built authentication system.
Key features I leveraged:
Authentication: Supabase's built-in authentication enabled users to log in and manage their profiles with ease.
Database Management: Using Supabase’s PostgreSQL-backed database, I stored user preferences, workout routines, and meal plans to ensure updates reflected immediately in the app.
For the frontend, I chose Ionic and Angular, which enabled me to create a mobile-first app that could be deployed on the web right away while it could also be shipped as native for both iOS and Android.
For the landing page, I opted for Astro, which focuses on performance by shipping minimal JavaScript. Astro allowed me to build a fast, lightweight page that efficiently showcased the app.
Developing Fitness Tribe AI was a learning journey that enabled me to explore the power that AI models give us nowadays. Each framework played a role, from FastAPI’s robust backend capabilities and ease of use to Supabase’s user management, Ionic’s cross-platform frontend and Astro’s high-performance landing pages.
For anyone looking to build a GenAI app, I highly recommend exploring these frameworks (and especially FastAPI) for their powerful features and smooth developer experience.
Have questions or want to learn more about it? Let me know in the comments!
以上是与 Gemini 一起构建 GenAI 健身应用程序的详细内容。更多信息请关注PHP中文网其他相关文章!