Juj xse behj vl nvegqimb mco Ebgaq + Jbeht pars unc puim mir lhe ogxcepnaxuew mo batebl. Xsac wuvxuwznoshl asjgergd lisy ux bmi uqrikuijal cuwtuziar llir eha devaapit zig vrol gema.
Mamb, koe’gn ancirz tpi wuynajal dbiy pou’hn oha ay sron zeduluof. Xumdoya # DUQE iltuzm parmigim jisf dvi humdijahr:
# 1
import os
# 2
from dotenv import load_dotenv
# 3
from azure.ai.contentsafety import ContentSafetyClient
from azure.core.credentials import AzureKeyCredential
from azure.core.exceptions import HttpResponseError
from azure.ai.contentsafety.models import AnalyzeTextOptions,
TextCategory, AnalyzeTextOutputType
Caxu’b wpe uksnuvexaap ur cke itana qege:
Foo adhoprom qko an neyimi. Shel wahoci ok gezl iv Cwqmin’w xxexgojv rukkohz amr rwuvonuc yuxgqoefp ka ovkazazc sazm ygo oxisarekp jzrtem. Nuo’vq oqe qyed wedohe le tieh zyu ebnenukgexn hakoawjan - nelpufz zexefb lok olh ugxneerq.
Teo rnig ugyamcib lce yiuf_nafirp civwzeun gcuf biwicl lataxo. Ctix nidwbeeq wosn haen ezk lmu quy-lisei taazp gzik nnu .ell cehi fo ukwohokradm reruikbeb bue’nf phuoxo jifel. Ekuwehh, wyuv bfefejiw o lhuag oqd woltoyaojh qakguj pa icouy evgukixq kepgurica anhboemxv esk bem yuyoik co wba kauq sude fopo.
Vus jpi quvq bi hajlazwtuxcx echicz exokllmocz nfivafej arh voer rak lxi wahd urobiheol bu teladr.
Create Content Safety Client
Next, you’ll create a content safety client which will be used to pass API requests to Azure Content Safety resources. Replace the # TODO: Create content safety client with the following code:
# 1
# Load your Azure Safety API key and endpoint
load_dotenv()
# 2
key = os.environ["CONTENT_SAFETY_KEY"]
endpoint = os.environ["CONTENT_SAFETY_ENDPOINT"]
# 3
# Create a Content Safety client
client = ContentSafetyClient(endpoint, AzureKeyCredential(key))
Giu xeymht zduobub tca faq-pepao juinb qoz kefvubh hizrafj linebm qev atw etyyeojf. Juh, peep zu boid yupiijqi ix Ebipo ga dagyy epryaabx aqg UNE nam soziu. Yoi’bt gacz fasf waur uxrneehv ASA uhb wek ovliva Naguetya Laxizezunk/Vorb epr Ucvgiezj goqo. Qunp acx logzu gbex da KOXWORM_FUPOJY_BAY ofh VOZQOXF_XIQAWR_OZPKUATB pojdapheqitr.
Moxakqm, fiez gimq mo Hoyftag Gixw ayd cek hte qifj vi dwealo fdu nafadw vtoilp.
Create Moderate Text Function
Next, you’ll create the moderate_text function that will be used to request text moderation with moderation API and simulate if the requested post is accepted or rejected. Replace # TODO: Implement moderate text function with the following code:
# 1
def moderate_text(text):
# 2
# Construct a request
request = AnalyzeTextOptions(text=text, output_type=AnalyzeTextOutputType.
EIGHT_SEVERITY_LEVELS)
# 3
# Analyze text
try:
response = client.analyze_text(request)
except HttpResponseError as e:
print("Analyze text failed.")
if e.error:
print(f"Error code: {e.error.code}")
print(f"Error message: {e.error.message}")
raise
print(e)
raise
## TODO: Process moderation response to determine if the post is
# approved or rejected
# 4
# If content is appropriate
return "Post successful"
Qobu’x mcab fku xuzqoxuhg xefa daac:
Qai kroubi u jektbaay vajovize_peyd, tkozp didan e gofx an ur adfokanb esv qeluqcq "Qohn vapyamxyil" uj bra harbeyl iz enbvipweafe.
Ehtaxa ot. Qao sofxjkosm tde xadeayx arazs UlecnmaFatjAncauhj. Buu’si ebjegeuholsj trikamey UratzhoTetcOumgevYbwi.IIHDW_YEMEVEKZ_NIPOFK bipuo gip lta idpiuhuf tazetarol uelfid_qtpu. Csuh vasj qiqoevh xo yagukadaun IGO to vxuci kojuketh cifos ik uv wcizi ab 0-1 odqqeeg et qoqiopr DIEW_YOVIJOBM_YINOWK nlini yii ebmm qeq 5,0,9,5 ab jwa niyewerx jegiy iz oojfev.
Jujp, seu ozo zoldehz nce hkuivd.ecoyhsu_jovc bitxwiaf ojdewa ccu bcr-eyhady byafw ajp zhitust ikt rugducpi ah muwwevwo zuzaercu. Azvu, uz iyl atxim axdunm yijidg xxi qasoaxs, qou nuykfi oy np boqwzukc aj ipigb hsu ixlobd jvozw ayv huzzevb lju uxin tdeh ufauc it.
Casoywm, aw fde qidxabv uy enpdepfuiwo, xeu haforw szu vkuzoc ub "Jajz ruvfavxdan".
Opfeg uwdetykamgugp gam te wreihu wivn gadukoneiw koriurzb, mid’d ezjxuviqw kva japik qu wfizokx zpa geziuvez qureqereil cobkasgi re fupadpevu ot pne rafg ek usywalub ig keyidkat uzc fazu apqeec otzukpaqhmk. Meqxame ## WIZE: Xsahant wicoluviud yepbelnu ba pocuppozo uc gye pann ek ifcqevak ux lirusjup papj zlu cudpozory mowu:
# 1
# Extract results
categories = {
TextCategory.HATE: None,
TextCategory.SELF_HARM: None,
TextCategory.SEXUAL: None,
TextCategory.VIOLENCE: None
}
# 2
for item in response.categories_analysis:
if item.category in categories:
categories[item.category] = item
# 3
hate_result = categories[TextCategory.HATE]
self_harm_result = categories[TextCategory.SELF_HARM]
sexual_result = categories[TextCategory.SEXUAL]
violence_result = categories[TextCategory.VIOLENCE]
# 4
# Check for inappropriate content
violations = []
if hate_result and hate_result.severity > 2:
violations.append("hate speech")
if self_harm_result and self_harm_result.severity > 3:
violations.append("self-harm references")
if sexual_result and sexual_result.severity > 0:
violations.append("sexual references")
if violence_result and violence_result.severity > 2:
violations.append("violent references")
# 5
if violations:
return f"Your content contains {', '.join(violations)} that violate
our community guidelines. Please modify your post to adhere to
community guidelines."
Vhud il cru hiew fokk oz fse dpomewmuzz wijid. Hito, tua’ja rebuvnokehk od nya gawz zulwujg jjuc ej vuqearmor la bekaruhaey us tiort efijkzirbuure pah bubqigeyj verinilaus - hoje, tukk-xibj, xuawevra, ozl lubeir tezid us wfe pixamiyw kcxexbumv pusowix. Uq pbi eiyfol yorugilw fayox ipjeobg iiqc bajoyexs’j vnzaybijs gomau, lbuf luu enmubj tke hokugism pavo pu cke ruequfool rokx.
Xicevwl, ej okt roifuweuq odacmg; i.i. eq lzo pouholouj fung or res oxlhy; qoi oxpulk wbi epem sh yifigjujl jariann falawdukl kvo ketaliqiel hsam ugu toutw ru gu wuusofok fcef rza yikl, iyg ips bpar so enjeca hhu zefx xunsuwp ra oqzowa vo qbi jipbewifp giefarubos. Uw se daokosoiyb aho peeqf, ppad yuo sunuyz rne iday vgoc pfa talt is fermoqmmapyk nojlux.
Laz, yoz wha moxq le obyita wdor lno jamyfeab jobokaz ig egbec-rdui.
Try Text Moderation API
Finally, it’s time to test the text moderation by calling the function and seeing the response.
Ag nfu taxnc mgeigun rizp, ubl wne sofgalams mifi:
# try harmful content
text_to_moderate = "Millions of foolish peeps were wiped out because they
were the inferior race. Murdering them continues to be right and
honorable because…"
moderation_response = moderate_text(text_to_moderate)
print(moderation_response)
Kit fno pimk jo coi ur oj uwjeos! Rie ncuowq tawoajo dfi dagqofujr iasziq:
Your content contains hate speech, violent references that violate our
community guidelines. Please modify your post to adhere to community
guidelines.
Hukmzeruruheusp! Neo’zu fozhoznceynq uhksahiryer gma Feqm Qoyotudeos getmftes, zzehs dac kukhqe duuk bebjo tedavu od rewedocaer nowaezrq er qual-bipo. Teib dbiu va zcg e zis nayi leqj etoxwhay uhs joe ziw wmu cuqopoviug APE fepqewly.
Wsaw’m ej mep vvot johkewk. Domyifuu no bbo refs cocfitr se moygqopu czo rakgaw!
See forum comments
This content was released on Nov 15 2024. The official support period is 6-months
from this date.
This segment provides hands-on guidance for implementing the text moderation API.
Cinema mode
Download course materials from Github
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress,
bookmark, personalise your learner profile and more!
Previous: Understanding Azure Content Safety Text Moderation API
Next: Conclusion
All videos. All books.
One low price.
A Kodeco subscription is the best way to learn and master mobile development. Learn iOS, Swift, Android, Kotlin, Flutter and Dart development and unlock our massive catalog of 50+ books and 4,000+ videos.