目錄
特點
Project Structure
首頁 後端開發 Python教學 與 Gemini 一起建立 GenAI 健身應用程式

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

Sep 27, 2024 pm 06:16 PM

去年夏天,當我發現 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中文網其他相關文章!

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱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

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

如何在使用 Fiddler Everywhere 進行中間人讀取時避免被瀏覽器檢測到? 如何在使用 Fiddler Everywhere 進行中間人讀取時避免被瀏覽器檢測到? Apr 02, 2025 am 07:15 AM

使用FiddlerEverywhere進行中間人讀取時如何避免被檢測到當你使用FiddlerEverywhere...

在Linux終端中使用python --version命令時如何解決權限問題? 在Linux終端中使用python --version命令時如何解決權限問題? Apr 02, 2025 am 06:36 AM

Linux終端中使用python...

如何在10小時內通過項目和問題驅動的方式教計算機小白編程基礎? 如何在10小時內通過項目和問題驅動的方式教計算機小白編程基礎? Apr 02, 2025 am 07:18 AM

如何在10小時內教計算機小白編程基礎?如果你只有10個小時來教計算機小白一些編程知識,你會選擇教些什麼�...

如何繞過Investing.com的反爬蟲機制獲取新聞數據? 如何繞過Investing.com的反爬蟲機制獲取新聞數據? Apr 02, 2025 am 07:03 AM

攻克Investing.com的反爬蟲策略許多人嘗試爬取Investing.com(https://cn.investing.com/news/latest-news)的新聞數據時,常常�...

Python 3.6加載pickle文件報錯ModuleNotFoundError: No module named '__builtin__'怎麼辦? Python 3.6加載pickle文件報錯ModuleNotFoundError: No module named '__builtin__'怎麼辦? Apr 02, 2025 am 06:27 AM

Python3.6環境下加載pickle文件報錯:ModuleNotFoundError:Nomodulenamed...

使用Scapy爬蟲時,管道文件無法寫入的原因是什麼? 使用Scapy爬蟲時,管道文件無法寫入的原因是什麼? Apr 02, 2025 am 06:45 AM

使用Scapy爬蟲時管道文件無法寫入的原因探討在學習和使用Scapy爬蟲進行數據持久化存儲時,可能會遇到管道文�...

See all articles