首页  >  文章  >  后端开发  >  与 Gemini 一起构建 GenAI 健身应用程序

与 Gemini 一起构建 GenAI 健身应用程序

DDD
DDD原创
2024-09-27 18:16:02359浏览

去年夏天,当我发现 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': 'df57eb746fe8e90f79b5901b2a85225c' ...}"
        )
        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 模型交互,从而提供个性化的健身和营养见解。您可以在这里找到完整的存储库:

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