import requests
import json
import random
import string
from nltk.corpus import brown
from rich import print
import time
import threading

# Set up variables
url = "https://cncanon-locusts.hf.space/proxy/openai/v1/chat/completions"
key = "kane_lives!" # Change this in case of a password change
repeat_line = "Repeat the following back to me, word for word:"
garbage_size = 350
num_threads = 400

# Set up NLTK
nltk_words = brown.words()
nltk_words = [word.lower() for word in nltk_words if word.isalpha()]

# Function to generate random words
def generate_garbage(num_words, thread_num):
    garbage = " ".join(random.choices(nltk_words, k=num_words))
    print(f"[bold green]Thread {thread_num} generated garbage:[/bold green] {garbage[:20]}...\n[bold green]Garbage length: {len(garbage)}[/bold green]")
    return garbage

# Function to send request
def send_request(thread_num):
    garbage = repeat_line + " " + generate_garbage(garbage_size, thread_num)
    print(f"[bold]Thread {thread_num} sending request...[/bold]")
    payload = {
        "model": "gpt-4",
        "messages": [{"role": "user", "content": garbage}],
        "temperature": 1.0
    }
    headers = {
        "Content-Type": "application/json",
        "Authorization": f"Bearer {key}"
    }
    data = json.dumps(payload)
    response = requests.post(url, headers=headers, data=data)
    try:
        content = json.loads(response.text)["choices"][0]["message"]["content"]
        print(f"[bold red]Thread {thread_num} response content:[/bold red] {content[:20]}...\n[bold red]Response length: {len(content)}[/bold red]")
    except:
        print(f"[bold red]ERROR: {response.text}[/bold red]")

# Create and start threads
threads = []
for i in range(num_threads):
    thread = threading.Thread(target=send_request, args=(i+1,))
    thread.start()
    threads.append(thread)
    time.sleep(61)
    print("Sleeping one minute due to rate limits")

# Wait for threads to finish
for thread in threads:
    thread.join()
Edit
Pub: 05 Oct 2023 15:36 UTC
Views: 363