首頁 > 後端開發 > Python教學 > 與 Gemini 一起建立 GenAI 健身應用程式

與 Gemini 一起建立 GenAI 健身應用程式

DDD
發布: 2024-09-27 18:16:02
原創
384 人瀏覽過

去年夏天,當我發現 Gemini API 開發者競賽時,我認為這是一個親身體驗 GenAI 應用程式的絕佳機會。作為健身愛好者,我們(我和 Manos Chainakis)想到創建一款可以生成個性化鍛煉和營養計劃的應用程序,將人工智能與人類教練的偏好相結合。這就是健身部落 AI 的誕生。這篇文章將帶您了解我使用的開發流程和技術堆疊,重點是 GenAI 方面。

Building a GenAI Fitness App with Gemini

健身部落人工智慧 |  Gemini API 開發者競賽 |  開發者的Google人工智慧

為世界各地的每個人提供更好的個人訓練。

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
登入後複製
  • 用於資料驗證的 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中文網其他相關文章!

來源:dev.to
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板