与 Gemini 一起构建 GenAI 健身应用程序
去年夏天,当我发现 Gemini API 开发者竞赛时,我认为这是一个亲身体验 GenAI 应用程序的绝佳机会。作为健身爱好者,我们(我和 Manos Chainakis)想到创建一款可以生成个性化锻炼和营养计划的应用程序,将人工智能与人类教练的偏好相结合。这就是健身部落 AI 的诞生。这篇文章将带您了解我使用的开发过程和技术堆栈,重点是 GenAI 方面。
健身部落AI背后的理念
Fitness Tribe AI 将人类教练的专业知识与人工智能模型的功能相结合,创建满足每个运动员需求和目标的定制健身计划。
技术堆栈
技术堆栈的主要组成部分是:
- FastAPI 用于后端和 AI 模型集成
- Supabase 用于用户身份验证和数据管理
- 前端移动应用的 Ionic 和 Angular
- Astro 用于登陆页面
FastAPI:后端和人工智能集成
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
FastAPI 实现的关键要素:
- API 路由:路由分为膳食 (meals.py)、锻炼 (workouts.py) 和营养 (nutrition.py) 的单独文件,保持 API 结构有序且可扩展。每个路由器都在 main.py 中连接,FastAPI 的路由系统将所有内容连接在一起。
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)
- Gemini 模型集成:gemini_model.py 中的 GeminiModel 类处理 AI 模型交互。以膳食分析方法为例,我使用 Pillow 处理图像数据,应用程序将图像和自定义提示发送给 Gemini AI 来分析膳食详细信息。这里的一个重要细节是提示应该足够具体,当涉及到预期响应的格式时,以便它可以被服务层处理。
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
- 用于数据验证的 Pydantic 架构:使用 Pydantic 模型对 AI 模型的响应进行验证和结构化。例如,schemas/meal.py 中的 Meal 模式可确保响应在返回给用户之前保持一致。
from pydantic import BaseModel from typing import Dict class Meal(BaseModel): food_name: str total_calories: int calories_per_ingredient: Dict[str, int]
- 服务层:服务层,位于services/中,封装了各个功能的逻辑。例如,meal_service.py 处理膳食分析,确保在返回 AI 结果之前正确处理数据。
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 模型交互,从而提供个性化的健身和营养见解。您可以在这里找到完整的存储库:
健身部落
/
健身部落-ai
Fitness Tribe AI 是一种人工智能驱动的 API,为教练和运动员提供端点。
健身部落API
Fitness Tribe AI 是一款由人工智能驱动的健身 API,专为教练和运动员设计。该 API 通过分析膳食照片和人工智能驱动的锻炼构建器提供膳食分析功能,该构建器可以根据运动员资料生成锻炼计划。健身部落AI已构建双子座模型。
特点
- Meal Analysis: Upload a photo of a meal to receive a detailed analysis of its ingredients and calorie count.
- Workout Builder: Input an athlete's profile details to receive a personalized workout plan tailored to the athlete's fitness goal.
Project Structure
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 │ │ ├──
Supabase: User Management & Auth
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.
Ionic & Angular: Cross-Platform Frontend
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.
Astro: A Lightning-Fast Landing Page
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.
Conclusion
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中文网其他相关文章!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

Python更易学且易用,C 则更强大但复杂。1.Python语法简洁,适合初学者,动态类型和自动内存管理使其易用,但可能导致运行时错误。2.C 提供低级控制和高级特性,适合高性能应用,但学习门槛高,需手动管理内存和类型安全。

要在有限的时间内最大化学习Python的效率,可以使用Python的datetime、time和schedule模块。1.datetime模块用于记录和规划学习时间。2.time模块帮助设置学习和休息时间。3.schedule模块自动化安排每周学习任务。

Python在开发效率上优于C ,但C 在执行性能上更高。1.Python的简洁语法和丰富库提高开发效率。2.C 的编译型特性和硬件控制提升执行性能。选择时需根据项目需求权衡开发速度与执行效率。

每天学习Python两个小时是否足够?这取决于你的目标和学习方法。1)制定清晰的学习计划,2)选择合适的学习资源和方法,3)动手实践和复习巩固,可以在这段时间内逐步掌握Python的基本知识和高级功能。

Python和C 各有优势,选择应基于项目需求。1)Python适合快速开发和数据处理,因其简洁语法和动态类型。2)C 适用于高性能和系统编程,因其静态类型和手动内存管理。

pythonlistsarepartofthestAndArdLibrary,herilearRaysarenot.listsarebuilt-In,多功能,和Rused ForStoringCollections,而EasaraySaraySaraySaraysaraySaraySaraysaraySaraysarrayModuleandleandleandlesscommonlyusedDduetolimitedFunctionalityFunctionalityFunctionality。

Python在自动化、脚本编写和任务管理中表现出色。1)自动化:通过标准库如os、shutil实现文件备份。2)脚本编写:使用psutil库监控系统资源。3)任务管理:利用schedule库调度任务。Python的易用性和丰富库支持使其在这些领域中成为首选工具。

Python在科学计算中的应用包括数据分析、机器学习、数值模拟和可视化。1.Numpy提供高效的多维数组和数学函数。2.SciPy扩展Numpy功能,提供优化和线性代数工具。3.Pandas用于数据处理和分析。4.Matplotlib用于生成各种图表和可视化结果。
