🚀 asyncio.Queue.shutdown() — shutting down handlers without special markers
Starting with Python 3.13, the asyncio.Queue can be closed using shutdown(), without the need to use None or other special values.
After shutdown(), no new items are added, and existing items are processed. When the queue is empty, get() will raise asyncio.QueueShutDown.
For a forced shutdown, there is shutdown(immediate=True), which clears the queue but may disrupt the normal behavior of join().
#Python #Asyncio #Programming #DevTips #Coding #Tech
✨ Join Best TG Channels https://xn--r1a.website/addlist/0f6vfFbEMdAwODBk
⭐️ Join Our WhatsApp Channel https://whatsapp.com/channel/0029VaC7Weq29753hpcggW2A
Starting with Python 3.13, the asyncio.Queue can be closed using shutdown(), without the need to use None or other special values.
import asyncio
async def worker(queue):
while True:
try:
task = await queue.get()
except asyncio.QueueShutDown:
break
print(task)
queue.task_done()
async def main():
queue = asyncio.Queue()
task = asyncio.create_task(worker(queue))
await queue.put("task-1")
await queue.put("task-2")
queue.shutdown()
await queue.join()
await task
asyncio.run(main())
After shutdown(), no new items are added, and existing items are processed. When the queue is empty, get() will raise asyncio.QueueShutDown.
For a forced shutdown, there is shutdown(immediate=True), which clears the queue but may disrupt the normal behavior of join().
#Python #Asyncio #Programming #DevTips #Coding #Tech
✨ Join Best TG Channels https://xn--r1a.website/addlist/0f6vfFbEMdAwODBk
⭐️ Join Our WhatsApp Channel https://whatsapp.com/channel/0029VaC7Weq29753hpcggW2A
❤1
Hallucinations in language models lead to the generation of false, yet convincingly sounding, facts.
Calculating the perplexity of the generated text allows us to assess the model's confidence in its responses.
We will write a Python script to calculate perplexity and filter out questionable generations.
Let's install the
transformers
and
torch
libraries to work with language models and calculate perplexity.
pip install transformers torch
The libraries for calculating perplexity have been successfully installed.
Let's create a Python script called
perplexity_check.py
that calculates the perplexity level for a given text.
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer
model_id = "gpt2"
tokenizer = AutoTokenizer.from_pretrained(model_id)
model = AutoModelForCausalLM.from_pretrained(model_id)
inputs = tokenizer("Example text for perplexity check.", return_tensors="pt")
with torch.no_grad():
outputs = model(**inputs, labels=inputs["input_ids"])
print("Loss value:", outputs.loss.item())
The perplexity calculation script has been successfully created and is ready to run. 🚀
# Verify the script's functionality
python3 -c "import torch; print('PyTorch Ready')"
Expected output:
PyTorch Ready
# cleanup (remove the test script)
rm -f perplexity_check.py
An abnormally high perplexity often indicates that the model has started "hallucinating" and generating illogical text.
Integrate this threshold into safety filters (Guardrails) to automatically block incorrect responses. 🛡️
https://xn--r1a.website/pythonRe
#Python #AI #LLM #MachineLearning #Coding #Tech
https://xn--r1a.website/addlist/0f6vfFbEMdAwODBk
https://whatsapp.com/channel/0029VaC7Weq29753hpcggW2A
Please open Telegram to view this post
VIEW IN TELEGRAM
Telegram
Learn Python Coding
Learn Python through simple, practical examples and real coding ideas. Clear explanations, useful snippets, and hands-on learning for anyone starting or improving their programming skills.
Admin: @HusseinSheikho || @Hussein_Sheikho
Admin: @HusseinSheikho || @Hussein_Sheikho
❤6