Discuss Scratch

haasvi13
Scratcher
100+ posts

Is Ai possibol in scratch?

I Wonder maybe it is possibol using an neural network
Catscratcher07
Scratcher
1000+ posts

Is Ai possibol in scratch?

yes. example
AceCoderClaws
Scratcher
27 posts

Is Ai possibol in scratch?

Indeed! Here's a rather fascinating example:
https://scratch-mit-edu.ezproxyberklee.flo.org/projects/557412338/

If you're interested in making your own I'd recommend learning how to make neural networks in a language such as Python first then taking that knowledge and applying it to Scratch.
BigNate469
Scratcher
1000+ posts

Is Ai possibol in scratch?

While it is possible, due to project size limits and speed, such a model would be fairly slow, and something like a LLM wouldn't be easily done (if possible at all) due to having more training data than the project can save.
haasvi13
Scratcher
100+ posts

Is Ai possibol in scratch?

Sorry for writing problems!!
GvYoutube
Scratcher
500+ posts

Is Ai possibol in scratch?

It is indeedly possible.
There are some technical limitations, and I also wish you know how to code in Python.
To do AI stuff you need to use ScratchAttach, in which it uses python
Iamnotarobot124
Scratcher
100+ posts

Is Ai possibol in scratch?

GvYoutube wrote:

It is indeedly possible.
There are some technical limitations, and I also wish you know how to code in Python.
To do AI stuff you need to use ScratchAttach, in which it uses python
No. I think he meant creating a nueral network entirely in Scratch. Plus I think Scratch has banned the use of online Large Language Models, seeing how they got rid of several AI image generator projects.
BigNate469
Scratcher
1000+ posts

Is Ai possibol in scratch?

GvYoutube wrote:

To do AI stuff you need to use ScratchAttach, in which it uses python
That's not true at all.

1. Scratch is Turing-complete, and can do anything Python (or any other Turing-complete language)
2. scratchattach is not the only way to connect to Scratch's servers, even if it's the most popular. Anything that can make and receive HTTP requests and/or WebSockets (HTTP is used for most everything, WebSockets is used for cloud vars), preferably with encryption, can do anything that scratchattach can. That actually is most text-based languages, including the most popular language in the world, JavaScript.
3. While most AI libraries are written for Python, they are also written for other languages (such as JavaScript, C and Swift (which has almost built-in support when deploying to newer Apple devices), and Cuda, used to program NVIDIA GPUs)
4. Such projects aren't allowed anyways most of the time, as they often result in the capability for inappropriate outputs, while due to speed and storage limitations things like image generation and LLMs are pretty much impossible on Scratch if you plan on uploading it, greatly limiting inappropriate content.
haasvi13
Scratcher
100+ posts

Is Ai possibol in scratch?

Thanks!!
snoopythe3
Scratcher
500+ posts

Is Ai possibol in scratch?

I would recommend going in the requests forum and asking there for code
haasvi13
Scratcher
100+ posts

Is Ai possibol in scratch?

Ok, thanks!!!
kRxZy_kRxZy
Scratcher
1000+ posts

Is Ai possibol in scratch?

BigNate469 wrote:

GvYoutube wrote:

To do AI stuff you need to use ScratchAttach, in which it uses python
That's not true at all.

1. Scratch is Turing-complete, and can do anything Python (or any other Turing-complete language)
2. scratchattach is not the only way to connect to Scratch's servers, even if it's the most popular. Anything that can make and receive HTTP requests and/or WebSockets (HTTP is used for most everything, WebSockets is used for cloud vars), preferably with encryption, can do anything that scratchattach can. That actually is most text-based languages, including the most popular language in the world, JavaScript.
3. While most AI libraries are written for Python, they are also written for other languages (such as JavaScript, C and Swift (which has almost built-in support when deploying to newer Apple devices), and Cuda, used to program NVIDIA GPUs)
4. Such projects aren't allowed anyways most of the time, as they often result in the capability for inappropriate outputs, while due to speed and storage limitations things like image generation and LLMs are pretty much impossible on Scratch if you plan on uploading it, greatly limiting inappropriate content.
For image generating you can parse the image, someone done it before, I saw a project called like ‘Profile Picture Viewer’ (this uses python).
You can censor the swears in the python code instead of doing it in the scratch project.
You can just remove all images and just return ‘Output could not be returned’
I have made AI in scratch but I took it down cos it sometimes gave inappropriate content, I used this script
import scratchattach as scratch
from scratchattach import Encoding  as encoding# Import encoding from scratchattach
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
import json
# --- SCRATCHATTACH: CONNECT TO ☁️Scratch Cloud --- #
session = scratch.login("us", "Password")  # Replace with your Scratch username & password
cloud = session.connect_cloud("1129800008")  # Replace with your Scratch project ID
# --- INTERACT WITH CHATGPT --- #
def interact_with_chatgpt(input_value):
    driver = webdriver.Chrome()  # Or use webdriver.Firefox()
    driver.get("https://chatgpt.com/")
    # Wait for the chat input field to be visible (this indicates the page has fully loaded)
    WebDriverWait(driver, 30).until(
        EC.visibility_of_element_located((By.XPATH, "//textarea"))
    )
    try:
        # Find the input box and enter the decoded value
        input_box = driver.find_element(By.XPATH, "//textarea")  # Adjust if needed
        input_box.send_keys(input_value)
        input_box.send_keys(Keys.RETURN)
        # Wait until ChatGPT stops typing (Modify class if needed)
        WebDriverWait(driver, 30).until_not(
            EC.presence_of_element_located((By.CLASS_NAME, "loading-spinner"))
        )
        # Extract ChatGPT's response
        response = driver.find_element(By.XPATH, "//div[contains(@class, 'message') and contains(@class, 'response')]")
        chatgpt_output = response.text
    except Exception as e:
        print("Error during ChatGPT interaction:", e)
        chatgpt_output = None
    # Close the browser
    driver.quit()
    return chatgpt_output
# --- LOG INPUTS AND OUTPUTS TO TEXT FILE --- #
def log_to_file(input_value, chatgpt_output):
    with open("chatgpt_logs.txt", "a") as log_file:
        log_file.write(f"Input: {input_value}\n")
        log_file.write(f"Output: {chatgpt_output}\n")
        log_file.write("-" * 50 + "\n")
# --- LOG INPUTS AND OUTPUTS TO JSON FILE --- #
def log_to_json(input_value, chatgpt_output):
    log_data = {
        "input": input_value,
        "output": chatgpt_output,
        "timestamp": time.strftime("%Y-%m-%d %H:%M:%S")
    }
    # Check if the log file exists, if not create it with an empty list
    try:
        with open("chatgpt_logs.json", "r") as log_file:
            logs = json.load(log_file)
    except FileNotFoundError:
        logs = []
    # Append the new log entry
    logs.append(log_data)
    # Write the updated log to the JSON file
    with open("chatgpt_logs.json", "w") as log_file:
        json.dump(logs, log_file, indent=4)
# --- SCRATCHATTACH: UPDATE THE RESPONSE BACK TO ☁️Scratch Cloud --- #
def update_to_cloud(chatgpt_output):
    if chatgpt_output:
        encoded_response = encoding.encode(chatgpt_output)  # Use encoding.encode() from scratchattach to encode
        cloud.set_var("☁️Cloudvar2", encoded_response)  # Update ☁️ Scratch cloud variable
        print("Encoded ChatGPT Response Sent to ☁️Scratch:", encoded_response)
    else:
        print("No response to send.")
# --- ADDITIONAL CODE TO STORE INPUTS AND OUTPUTS --- #
def store_inputs_and_outputs(input_value, chatgpt_output):
    if chatgpt_output:
        # Fetch existing inputs and outputs from the cloud
        existing_inputs = cloud.get_var("☁️Chatgpt_inputs") or ""
        existing_outputs = cloud.get_var("☁️Chatgpt_outputs") or ""
        # Append the new input and output to the existing ones
        updated_inputs = existing_inputs + "\n" + input_value  # Append new input
        updated_outputs = existing_outputs + "\n" + chatgpt_output  # Append new output
        # Encode and store them in the cloud
        encoded_inputs = encoding.encode(updated_inputs)  # Use encoding.encode() to encode updated inputs
        encoded_outputs = encoding.encode(updated_outputs)  # Use encoding.encode() to encode updated outputs
        # Set the new values to Scratch cloud variables
        cloud.set_var("☁️Chatgpt_inputs", encoded_inputs)  # Store in Chatgpt_inputs cloud variable
        cloud.set_var("☁️Chatgpt_outputs", encoded_outputs)  # Store in Chatgpt_outputs cloud variable
# --- SCRATCHATTACH: WAIT FOR MESSAGES FROM SCRATCH --- #
def listen_for_scratch_message():
    while True:
        try:
            val1 = cloud.get_var("☁️Cloudvar1")
            if val1 == 1:
                # Get the value of the Scratch cloud variable where the command is stored
                message1 = cloud.get_var("☁️Scratch_message")  # Scratch cloud variable where messages are sent
                message = encoding.decode(message1)
                if message and message.startswith("chatgpt("):
                    # Extract the argument (assuming it's a single argument inside the parentheses)
                    argument = message  # Strip the "chatgpt(" and ")"
                    print(f"Received message: chatgpt({argument})")
                    
                    # Perform the ChatGPT interaction
                    chatgpt_output = interact_with_chatgpt(argument)
                    
                    if chatgpt_output:
                        # Log input and output to both text and JSON files
                        log_to_file(argument, chatgpt_output)
                        log_to_json(argument, chatgpt_output)
                        # Update the response to Scratch cloud
                        update_to_cloud(chatgpt_output)
                        # Store inputs and outputs to Scratch cloud variables
                        store_inputs_and_outputs(argument, chatgpt_output)
                    else:
                        print("No response received from ChatGPT.")
                
                # You can add a small delay to avoid overloading the server with requests
                time.sleep(2)
        
        except Exception as e:
            print(f"Error occurred while processing: {e}")
            time.sleep(5)  # Wait before retrying to prevent overloading on errors
listen_for_scratch_message()

Last edited by kRxZy_kRxZy (Feb. 16, 2025 14:19:50)

haasvi13
Scratcher
100+ posts

Is Ai possibol in scratch?

Wow you really took time too write all of that? Cuz im not sure, but did you just give me a whole script too an AI
AceCoderClaws
Scratcher
27 posts

Is Ai possibol in scratch?

kRxZy_kRxZy wrote:

~snip~
You can censor the swears in the python code instead of doing it in the scratch project.
This still isn't allowed. It's not just it could return bad words but also that it could return harmfull messages which you couldn't really filter out. This is also why chatrooms are banned, even if filtered outside of Scratch.
kRxZy_kRxZy
Scratcher
1000+ posts

Is Ai possibol in scratch?

AceCoderClaws wrote:

kRxZy_kRxZy wrote:

~snip~
You can censor the swears in the python code instead of doing it in the scratch project.
This still isn't allowed. It's not just it could return bad words but also that it could return harmfull messages which you couldn't really filter out. This is also why chatrooms are banned, even if filtered outside of Scratch.
I know, I was so desperate to make it but then after 10 days of working and the help of AI, I made it.

haasvi13 wrote:

Wow you really took time too write all of that? Cuz im not sure, but did you just give me a whole script too an AI
Used some help from AI

Powered by DjangoBB