टेक्स्टुअल रिक्वेस्ट डेटा हैंडल करें
कंटेंट मॉडरेशन सिस्टम की एक और ज़रूरत है कि यूज़र कमेंट्स के sentiment को भी ध्यान में रखा जाए. सिस्टम को विशेष समस्या-जनक वाक्यांशों की पहचान करनी है ताकि मॉडरेटर्स संभावित अनुचित कंटेंट की समीक्षा कर सकें.
आप एक endpoint बनाएँगे जो यूज़र्स से आने वाले टेक्स्ट का विश्लेषण करेगा और मानकीकृत मॉडरेशन फ़्लैग्स निकालेगा.
यह अभ्यास पाठ्यक्रम का हिस्सा है
FastAPI के साथ प्रोडक्शन में AI डिप्लॉय करना
अभ्यास निर्देश
analyze_comment()फंक्शन में आने वाले टेक्स्ट को लोअरकेस में बदलें ताकि यह case-insensitive हो जाए.- प्रोसेस्ड टेक्स्ट से
problem_keywordsलिस्ट का उपयोग करकेfound_issuesमें समस्या वाले कीवर्ड निकालें. - निम्न keys के साथ JSON response रिटर्न करें -
issues(कीवर्ड्स की लिस्ट),issue_count(इंटिजर), औरoriginal_text(स्ट्रिंग).
इंटरैक्टिव व्यावहारिक अभ्यास
इस अभ्यास को इस नमूना कोड को पूरा करके आज़माएँ।
@app.post("/analyze_comment")
def analyze_comment(text: str):
problem_keywords = ["spam", "hate", "offensive", "abuse"]
# Convert the input text to lowercase
text_lower = ____
# Extract matching flags using list comprehension
found_issues = [____ for ____ in problem_keywords if keyword in text_lower]
# Return the dictionary with required keys
return {
"____": found_issues,
"____": len(found_issues),
"____": text
}