去年夏天,當我發現 Gemini API 開發者競賽時,我認為這是一個親身體驗 GenAI 應用程式的絕佳機會。作為健身愛好者,我們(我和 Manos Chainakis)想到創建一款可以生成個性化鍛煉和營養計劃的應用程序,將人工智能與人類教練的偏好相結合。這就是健身部落 AI 的誕生。這篇文章將帶您了解我使用的開發流程和技術堆疊,重點是 GenAI 方面。
Fitness Tribe AI 將人類教練的專業知識與人工智慧模型的功能相結合,創建滿足每個運動員需求和目標的客製化健身計劃。
技術堆疊的主要組成部分是:
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
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)
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
from pydantic import BaseModel from typing import Dict class Meal(BaseModel): food_name: str total_calories: int calories_per_ingredient: Dict[str, int]
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 模型交互,從而提供個人化的健身和營養見解。您可以在這裡找到完整的儲存庫:
Fitness Tribe AI 是一款由人工智慧驅動的健身 API,專為教練和運動員設計。該 API 透過分析膳食照片和人工智慧驅動的鍛鍊建立器提供膳食分析功能,該建立器可以根據運動員資料產生鍛鍊計劃。健身部落AI已建立雙子座模型。
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 │ │ ├──
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.
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.
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.
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中文網其他相關文章!