去年夏天,當我發現 Gemini API 開發者競賽時,我認為這是一個親身體驗 GenAI 應用程式的絕佳機會。作為健身愛好者,我們(我和 Manos Chainakis)想到創建一款可以生成個性化鍛煉和營養計劃的應用程序,將人工智能與人類教練的偏好相結合。這就是健身部落 AI 的誕生。這篇文章將帶您了解我使用的開發流程和技術堆疊,重點是 GenAI 方面。
健身部落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 模型交互,從而提供個人化的健身和營養見解。您可以在這裡找到完整的儲存庫:
健身部落
/
健身部落-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 │ │ ├──…
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中文網其他相關文章!

Python和C 各有優勢,選擇應基於項目需求。 1)Python適合快速開發和數據處理,因其簡潔語法和動態類型。 2)C 適用於高性能和系統編程,因其靜態類型和手動內存管理。

選擇Python還是C 取決於項目需求:1)如果需要快速開發、數據處理和原型設計,選擇Python;2)如果需要高性能、低延遲和接近硬件的控制,選擇C 。

通過每天投入2小時的Python學習,可以有效提升編程技能。 1.學習新知識:閱讀文檔或觀看教程。 2.實踐:編寫代碼和完成練習。 3.複習:鞏固所學內容。 4.項目實踐:應用所學於實際項目中。這樣的結構化學習計劃能幫助你係統掌握Python並實現職業目標。

在兩小時內高效學習Python的方法包括:1.回顧基礎知識,確保熟悉Python的安裝和基本語法;2.理解Python的核心概念,如變量、列表、函數等;3.通過使用示例掌握基本和高級用法;4.學習常見錯誤與調試技巧;5.應用性能優化與最佳實踐,如使用列表推導式和遵循PEP8風格指南。

Python適合初學者和數據科學,C 適用於系統編程和遊戲開發。 1.Python簡潔易用,適用於數據科學和Web開發。 2.C 提供高性能和控制力,適用於遊戲開發和系統編程。選擇應基於項目需求和個人興趣。

Python更適合數據科學和快速開發,C 更適合高性能和系統編程。 1.Python語法簡潔,易於學習,適用於數據處理和科學計算。 2.C 語法複雜,但性能優越,常用於遊戲開發和系統編程。

每天投入兩小時學習Python是可行的。 1.學習新知識:用一小時學習新概念,如列表和字典。 2.實踐和練習:用一小時進行編程練習,如編寫小程序。通過合理規劃和堅持不懈,你可以在短時間內掌握Python的核心概念。

Python更易學且易用,C 則更強大但複雜。 1.Python語法簡潔,適合初學者,動態類型和自動內存管理使其易用,但可能導致運行時錯誤。 2.C 提供低級控制和高級特性,適合高性能應用,但學習門檻高,需手動管理內存和類型安全。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

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

熱門文章

熱工具

SublimeText3漢化版
中文版,非常好用

Safe Exam Browser
Safe Exam Browser是一個安全的瀏覽器環境,安全地進行線上考試。該軟體將任何電腦變成一個安全的工作站。它控制對任何實用工具的訪問,並防止學生使用未經授權的資源。

記事本++7.3.1
好用且免費的程式碼編輯器

SAP NetWeaver Server Adapter for Eclipse
將Eclipse與SAP NetWeaver應用伺服器整合。

SublimeText3 Linux新版
SublimeText3 Linux最新版