FastAPI में इनपुट वैलिडेशन

FastAPI के साथ प्रोडक्शन में AI डिप्लॉय करना

Matt Eckerle

Software and Data Engineering Leader

इनपुट डेटा का वैलिडेशन

FastAPI के साथ ML प्रेडिक्शन का फ्लोचार्ट

FastAPI के साथ प्रोडक्शन में AI डिप्लॉय करना

इनपुट वैलिडेट क्यों करें?

 

  • डेटा इंटीग्रिटी के लिए वैलिडेशन
  • एप्लिकेशन में एरर रोकें
  • Pydantic के साथ इंटीग्रेटेड
  • डेटा वैलिडेशन के लिए पावरफुल टूल्स देता है

 

Pydantic का लोगो

FastAPI के साथ प्रोडक्शन में AI डिप्लॉय करना

प्री-डिफाइंड फंक्शन के लिए Pydantic

pydantic से फील्ड वैलिडेशन

FastAPI के साथ प्रोडक्शन में AI डिप्लॉय करना

pydantic के साथ कस्टम वैलिडेशन

pydantic से कस्टम वैलिडेशन

FastAPI के साथ प्रोडक्शन में AI डिप्लॉय करना

सुव्यवस्थित एरर रिपोर्टिंग

वैलिडेशन के दौरान एरर रिपोर्टिंग

FastAPI के साथ प्रोडक्शन में AI डिप्लॉय करना

Pydantic फील्ड वेलिडेटर्स

  • यूज़र रजिस्ट्रेशन एंडपॉइंट

  • यूज़र्स द्वारा डाला गया username वैलिडेट करना:

from pydantic import BaseModel, Field
class User(BaseModel):
    username: str = Field(..., min_length=3, max_length=50)
FastAPI के साथ प्रोडक्शन में AI डिप्लॉय करना

कस्टम वेलिडेटर्स जोड़ना

class User(BaseModel):
    username: str = Field(..., 
                          min_length=3, 
                          max_length=50)
    age: int

@field_validator('age') def age_criteria(cls, age): if age < 13: raise ValueError('User must be at least 13') return age
FastAPI के साथ प्रोडक्शन में AI डिप्लॉय करना

कस्टम वेलिडेटर्स इन एक्शन

वैध रिक्वेस्ट:

{"username": "john_doe", "age": 25}
Valid user: username='john_doe' age=25

अवैध रिक्वेस्ट:

{"username": "too_young", "age": 10}
Validation error for {'username': 'too_young', 'age': 10}: User must be at least 13
FastAPI के साथ प्रोडक्शन में AI डिप्लॉय करना

सभी को साथ जोड़ें

वैलिडेशन के दौरान एरर रिपोर्टिंग

  • username के लिए फील्ड वेलिडेटर
  • age के लिए कस्टम वेलिडेटर
  • वैलिडेशन फेल होने पर एरर मैसेज
FastAPI के साथ प्रोडक्शन में AI डिप्लॉय करना

सभी को साथ जोड़ें

@app.post("/users")
def create_user(user: User):
    return {"message": "User created",
            "user": user.model_dump()}

आउटपुट:

{
  "message": "User created successfully",
    "user": {
          "username": "john_doe", 
           "age": 25
    }
}
FastAPI के साथ प्रोडक्शन में AI डिप्लॉय करना

अभ्यास करते हैं!

FastAPI के साथ प्रोडक्शन में AI डिप्लॉय करना

Preparing Video For Download...