New paste Repaste Download
ominiverdi@minto:~/tmp/olllama_exp$ python3 stream_stop.py
Press 'd' to stop the stream...
The rain in Aethelgard wasn’t just rain; it was memory. It fell as a shimmering, silver mist, carrying whispers of forgotten tales and half-remembered emotions. And it always, inevitably, led to the Obsidian Library.
    It wasn't built, not in the way humans understood it. It simply *was*, nestled within a perpetually twilight valley,
                                                                                                                        [Stopping...]
                                                                                                                                     ominiverdi@minto:~/tmp/olllama_exp$ cat stream_stop.py
import requests
import json
import sys
import termios
import tty
import threading
# Flag to track if we should stop streaming
stop_streaming = False
# Function to handle 'd' key press
def key_listener():
    global stop_streaming
    print("Press 'd' to stop the stream...")
    
    # Set terminal to raw mode
    old_settings = termios.tcgetattr(sys.stdin)
    try:
        tty.setraw(sys.stdin.fileno())
        while not stop_streaming:
            # Check for key press
            if sys.stdin.read(1) == 'd':
                stop_streaming = True
                print("\n[Stopping...]")
                break
    finally:
        termios.tcsetattr(sys.stdin, termios.TCSADRAIN, old_settings)
# Start key listener thread
listener = threading.Thread(target=key_listener)
listener.daemon = True
listener.start()
# Make streaming request
url = "http://localhost:11434/api/generate"
payload = {
    "model": "gemma3",
    "prompt": "Write a fantasy story about a magical library",
    "stream": True
}
try:
    response = requests.post(url, json=payload, stream=True)
    
    for line in response.iter_lines():
        if stop_streaming:
            break
            
        if line:
            chunk = json.loads(line)
            if "response" in chunk:
                print(chunk["response"], end="", flush=True)
            if chunk.get("done", False):
                break
except Exception as e:
    print(f"\nError: {e}")
finally:
    stop_streaming = True
Filename: None. Size: 2kb. View raw, , hex, or download this file.

This paste expires on 2025-05-16 12:14:18.623982. Pasted through web.