与 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)

Linux终端中查看Python版本时遇到权限问题的解决方法当你在Linux终端中尝试查看Python的版本时,输入python...

使用FiddlerEverywhere进行中间人读取时如何避免被检测到当你使用FiddlerEverywhere...

在使用Python的pandas库时,如何在两个结构不同的DataFrame之间进行整列复制是一个常见的问题。假设我们有两个Dat...

如何在10小时内教计算机小白编程基础?如果你只有10个小时来教计算机小白一些编程知识,你会选择教些什么�...

Uvicorn是如何持续监听HTTP请求的?Uvicorn是一个基于ASGI的轻量级Web服务器,其核心功能之一便是监听HTTP请求并进�...

攻克Investing.com的反爬虫策略许多人尝试爬取Investing.com(https://cn.investing.com/news/latest-news)的新闻数据时,常常�...
