搜索
首页后端开发Python教程与 Gemini 一起构建 GenAI 健身应用程序

去年夏天,当我发现 Gemini API 开发者竞赛时,我认为这是一个亲身体验 GenAI 应用程序的绝佳机会。作为健身爱好者,我们(我和 Manos Chainakis)想到创建一款可以生成个性化锻炼和营养计划的应用程序,将人工智能与人类教练的偏好相结合。这就是健身部落 AI 的诞生。这篇文章将带您了解我使用的开发过程和技术堆栈,重点是 GenAI 方面。

Building a GenAI Fitness App with Gemini

健身部落人工智能 |  Gemini API 开发者大赛 |  面向开发者的谷歌人工智能

为世界各地的每个人提供更好的个人训练。

Building a GenAI Fitness App with Gemini ai.google.dev

健身部落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
</food>
  • 用于数据验证的 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 模型交互,从而提供个性化的健身和营养见解。您可以在这里找到完整的存储库:

Building a GenAI Fitness App with Gemini 健身部落 / 健身部落-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
│   │   ├──
View on GitHub

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中文网其他相关文章!

声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
Python vs. C:了解关键差异Python vs. C:了解关键差异Apr 21, 2025 am 12:18 AM

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

Python vs.C:您的项目选择哪种语言?Python vs.C:您的项目选择哪种语言?Apr 21, 2025 am 12:17 AM

选择Python还是C 取决于项目需求:1)如果需要快速开发、数据处理和原型设计,选择Python;2)如果需要高性能、低延迟和接近硬件的控制,选择C 。

达到python目标:每天2小时的力量达到python目标:每天2小时的力量Apr 20, 2025 am 12:21 AM

通过每天投入2小时的Python学习,可以有效提升编程技能。1.学习新知识:阅读文档或观看教程。2.实践:编写代码和完成练习。3.复习:巩固所学内容。4.项目实践:应用所学于实际项目中。这样的结构化学习计划能帮助你系统掌握Python并实现职业目标。

最大化2小时:有效的Python学习策略最大化2小时:有效的Python学习策略Apr 20, 2025 am 12:20 AM

在两小时内高效学习Python的方法包括:1.回顾基础知识,确保熟悉Python的安装和基本语法;2.理解Python的核心概念,如变量、列表、函数等;3.通过使用示例掌握基本和高级用法;4.学习常见错误与调试技巧;5.应用性能优化与最佳实践,如使用列表推导式和遵循PEP8风格指南。

在Python和C之间进行选择:适合您的语言在Python和C之间进行选择:适合您的语言Apr 20, 2025 am 12:20 AM

Python适合初学者和数据科学,C 适用于系统编程和游戏开发。1.Python简洁易用,适用于数据科学和Web开发。2.C 提供高性能和控制力,适用于游戏开发和系统编程。选择应基于项目需求和个人兴趣。

Python与C:编程语言的比较分析Python与C:编程语言的比较分析Apr 20, 2025 am 12:14 AM

Python更适合数据科学和快速开发,C 更适合高性能和系统编程。1.Python语法简洁,易于学习,适用于数据处理和科学计算。2.C 语法复杂,但性能优越,常用于游戏开发和系统编程。

每天2小时:Python学习的潜力每天2小时:Python学习的潜力Apr 20, 2025 am 12:14 AM

每天投入两小时学习Python是可行的。1.学习新知识:用一小时学习新概念,如列表和字典。2.实践和练习:用一小时进行编程练习,如编写小程序。通过合理规划和坚持不懈,你可以在短时间内掌握Python的核心概念。

Python与C:学习曲线和易用性Python与C:学习曲线和易用性Apr 19, 2025 am 12:20 AM

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

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

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

AI Clothes Remover

AI Clothes Remover

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

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

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

热工具

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

MinGW - 适用于 Windows 的极简 GNU

MinGW - 适用于 Windows 的极简 GNU

这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。

PhpStorm Mac 版本

PhpStorm Mac 版本

最新(2018.2.1 )专业的PHP集成开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)