Rumah >pembangunan bahagian belakang >Tutorial Python >Membina Apl Kecergasan GenAI dengan Gemini
Musim panas lalu, apabila saya mengetahui tentang Pertandingan Pembangun API Gemini, saya melihatnya sebagai peluang yang baik untuk mengotorkan tangan saya dengan aplikasi GenAI. Sebagai peminat kecergasan, kami (saya & Manos Chainakis) terfikir untuk mencipta apl yang boleh menjana pelan senaman dan pemakanan yang diperibadikan—menggabungkan AI dengan keutamaan jurulatih manusia. Begitulah cara Fitness Tribe AI dilahirkan. Siaran ini akan membimbing anda melalui proses pembangunan dan susunan teknologi yang saya gunakan, dengan tumpuan pada aspek GenAI.
Fitness Tribe AI menggabungkan kepakaran jurulatih manusia dengan keupayaan model AI untuk mencipta program kecergasan tersuai yang memenuhi keperluan dan matlamat setiap atlet.
Komponen utama timbunan teknologi ialah:
FastAPI berfungsi sebagai tulang belakang Fitness Tribe AI, mengendalikan analisis dikuasakan AI.
Begini cara projek distrukturkan:
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': 'df57eb746fe8e90f79b5901b2a85225c' ...}" ) 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))
Dengan memanfaatkan struktur modular FastAPI, penghalaan API yang jelas, Pydantic untuk pengesahan data dan logik perkhidmatan yang teratur, Fitness Tribe AI dengan cekap mengendalikan interaksi model AI dengan gesaan tersuai untuk menyampaikan cerapan kecergasan dan pemakanan yang diperibadikan. Anda boleh dapatkan repo penuh di sini:
Fitness Tribe AI ialah API kecergasan dikuasakan AI yang direka untuk jurulatih dan atlet. API menyediakan fungsi analisis makanan dengan menganalisis foto makanan dan pembina senaman dikuasakan AI, yang boleh menjana rancangan senaman berdasarkan profil atlet. Fitness Tribe AI telah membina model Gemini.
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!
Atas ialah kandungan terperinci Membina Apl Kecergasan GenAI dengan Gemini. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!