Discuss Scratch

mcgdj
Scratcher
100+ posts

Use Text ML, Sync ☁️Cloud☁️ Variables between projects, host code, and more!

Dagriffpatchfan wrote:

Dagriffpatchfan
Scratcher
1000+ posts

Use Text ML, Sync ☁️Cloud☁️ Variables between projects, host code, and more!

mcgdj wrote:

Dagriffpatchfan wrote:

Dagriffpatchfan
Scratcher
1000+ posts

Use Text ML, Sync ☁️Cloud☁️ Variables between projects, host code, and more!

Dagriffpatchfan wrote:

kRxZy_kRxZy
Scratcher
1000+ posts

Use Text ML, Sync ☁️Cloud☁️ Variables between projects, host code, and more!

Credit to chatgpt a bit, I don't know what I have done wrong, when I run:
Traceback (most recent call last):
  File "/data/user/0/ru.iiec.pydroid3/files/accomp_files/iiec_run/iiec_run.py", line 31, in <module>
    start(fakepyfile,mainpyfile)
  File "/data/user/0/ru.iiec.pydroid3/files/accomp_files/iiec_run/iiec_run.py", line 30, in start
    exec(open(mainpyfile).read(),  __main__.__dict__)
  File "<string>", line 138
    except Exception as e:
    ^^^^^^
SyntaxError: invalid syntax
This is the code I was running
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
# Start listening for messages from Scratch
listen_for_scratch_message()

Last edited by kRxZy_kRxZy (Feb. 5, 2025 19:46:16)

mcgdj
Scratcher
100+ posts

Use Text ML, Sync ☁️Cloud☁️ Variables between projects, host code, and more!

kRxZy_kRxZy wrote:

Credit to chatgpt a bit, I don't know what I have done wrong, when I run:
Traceback (most recent call last):
  File "/data/user/0/ru.iiec.pydroid3/files/accomp_files/iiec_run/iiec_run.py", line 31, in <module>
    start(fakepyfile,mainpyfile)
  File "/data/user/0/ru.iiec.pydroid3/files/accomp_files/iiec_run/iiec_run.py", line 30, in start
    exec(open(mainpyfile).read(),  __main__.__dict__)
  File "<string>", line 138
    except Exception as e:
    ^^^^^^
SyntaxError: invalid syntax
This is the code I was running
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
# Start listening for messages from Scratch
listen_for_scratch_message()
I forget, what operating system are you on? Also, try using my new helpful AI for responses:
ssh chat@192.168.1.74
The pw:
chat
How original. Anyway, it runs Deepseek R1, something that (usually) only premium users get. Just wanted to put that out there. Also, courtesy of my language model,

DesmondBot wrote:

The error message you're encountering indicates a SyntaxError in your code, specifically at the line where you have
except Exception as e:
. This suggests that there is an issue with the indentation or structure of your code leading up to that line.

In Python, indentation is crucial, and it looks like there might be a problem with how the
try
and
except
blocks are structured. Here's a corrected version of the relevant part of your code:

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

Key Changes:
1. Indentation: Ensure that the
except
block is aligned with the
try
block. In your original code, the
except
block was incorrectly indented, which caused the SyntaxError.
2. Removed Unnecessary Indentation: The
if
statement and the code inside it should be properly indented to be part of the
try
block.

Make sure to check the entire code for consistent indentation, as Python relies on it to define the structure of the code. After making these changes, your code should run without the SyntaxError.
Hope that helped and enjoy the rest of your… Wednesday???
Dagriffpatchfan
Scratcher
1000+ posts

Use Text ML, Sync ☁️Cloud☁️ Variables between projects, host code, and more!

kRxZy_kRxZy wrote:

Credit to chatgpt a bit, I don't know what I have done wrong, when I run:
Traceback (most recent call last):
  File "/data/user/0/ru.iiec.pydroid3/files/accomp_files/iiec_run/iiec_run.py", line 31, in <module>
    start(fakepyfile,mainpyfile)
  File "/data/user/0/ru.iiec.pydroid3/files/accomp_files/iiec_run/iiec_run.py", line 30, in start
    exec(open(mainpyfile).read(),  __main__.__dict__)
  File "<string>", line 138
    except Exception as e:
    ^^^^^^
SyntaxError: invalid syntax
This is the code I was running
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
# Start listening for messages from Scratch
listen_for_scratch_message()

The Scratch team has informed me that projects generating ai content are not allowed. Is that what this code does?
mcgdj
Scratcher
100+ posts

Use Text ML, Sync ☁️Cloud☁️ Variables between projects, host code, and more!

Dagriffpatchfan wrote:

The Scratch team has informed me that projects generating ai content are not allowed. Is that what this code does?
Yes, it does. I advise that we remove any associated code.
mcgdj
Scratcher
100+ posts

Use Text ML, Sync ☁️Cloud☁️ Variables between projects, host code, and more!

mcgdj wrote:

Dagriffpatchfan wrote:

The Scratch team has informed me that projects generating ai content are not allowed. Is that what this code does?
Yes, it does. I advise that we remove any associated code.
Why not do something like:
[color=#ff0000]C[/color][color=#ff1000]o[/color][color=#ff2000]d[/color][color=#ff3000]e[/color] [color=#ff4f00]d[/color][color=#ff5f00]o[/color][color=#ff6f00]e[/color][color=#ff7f00]s[/color] [color=#ff9f00]n[/color][color=#ffaf00]o[/color][color=#ffbf00]t[/color] [color=#ffdf00]f[/color][color=#ffef00]o[/color][color=#ffff00]l[/color][color=#e3ff00]l[/color][color=#c6ff00]o[/color][color=#aaff00]w[/color] [color=#71ff00]S[/color][color=#55ff00]c[/color][color=#39ff00]r[/color][color=#1cff00]a[/color][color=#00ff00]t[/color][color=#00ff20]c[/color][color=#00ff40]h[/color] [color=#00ff80]g[/color][color=#00ff9f]u[/color][color=#00ffbf]i[/color][color=#00ffdf]d[/color][color=#00ffff]e[/color][color=#00dfff]l[/color][color=#00bfff]i[/color][color=#009fff]n[/color][color=#0080ff]e[/color][color=#0060ff]s[/color][color=#0040ff].[/color][color=#0020ff].[/color][color=#0000ff].[/color]
Result:
Code does not follow Scratch guidelines...
Dagriffpatchfan
Scratcher
1000+ posts

Use Text ML, Sync ☁️Cloud☁️ Variables between projects, host code, and more!

mcgdj wrote:

mcgdj wrote:

Dagriffpatchfan wrote:

The Scratch team has informed me that projects generating ai content are not allowed. Is that what this code does?
Yes, it does. I advise that we remove any associated code.
Why not do something like:
[color=#ff0000]C[/color][color=#ff1000]o[/color][color=#ff2000]d[/color][color=#ff3000]e[/color] [color=#ff4f00]d[/color][color=#ff5f00]o[/color][color=#ff6f00]e[/color][color=#ff7f00]s[/color] [color=#ff9f00]n[/color][color=#ffaf00]o[/color][color=#ffbf00]t[/color] [color=#ffdf00]f[/color][color=#ffef00]o[/color][color=#ffff00]l[/color][color=#e3ff00]l[/color][color=#c6ff00]o[/color][color=#aaff00]w[/color] [color=#71ff00]S[/color][color=#55ff00]c[/color][color=#39ff00]r[/color][color=#1cff00]a[/color][color=#00ff00]t[/color][color=#00ff20]c[/color][color=#00ff40]h[/color] [color=#00ff80]g[/color][color=#00ff9f]u[/color][color=#00ffbf]i[/color][color=#00ffdf]d[/color][color=#00ffff]e[/color][color=#00dfff]l[/color][color=#00bfff]i[/color][color=#009fff]n[/color][color=#0080ff]e[/color][color=#0060ff]s[/color][color=#0040ff].[/color][color=#0020ff].[/color][color=#0000ff].[/color]
Result:
Code does not follow Scratch guidelines...
Technically not the code, just running it
Tunibal_Scratcher
Scratcher
1000+ posts

Use Text ML, Sync ☁️Cloud☁️ Variables between projects, host code, and more!

Dagriffpatchfan wrote:

mcgdj wrote:

mcgdj wrote:

Dagriffpatchfan wrote:

The Scratch team has informed me that projects generating ai content are not allowed. Is that what this code does?
Yes, it does. I advise that we remove any associated code.
Why not do something like:
[color=#ff0000]C[/color][color=#ff1000]o[/color][color=#ff2000]d[/color][color=#ff3000]e[/color] [color=#ff4f00]d[/color][color=#ff5f00]o[/color][color=#ff6f00]e[/color][color=#ff7f00]s[/color] [color=#ff9f00]n[/color][color=#ffaf00]o[/color][color=#ffbf00]t[/color] [color=#ffdf00]f[/color][color=#ffef00]o[/color][color=#ffff00]l[/color][color=#e3ff00]l[/color][color=#c6ff00]o[/color][color=#aaff00]w[/color] [color=#71ff00]S[/color][color=#55ff00]c[/color][color=#39ff00]r[/color][color=#1cff00]a[/color][color=#00ff00]t[/color][color=#00ff20]c[/color][color=#00ff40]h[/color] [color=#00ff80]g[/color][color=#00ff9f]u[/color][color=#00ffbf]i[/color][color=#00ffdf]d[/color][color=#00ffff]e[/color][color=#00dfff]l[/color][color=#00bfff]i[/color][color=#009fff]n[/color][color=#0080ff]e[/color][color=#0060ff]s[/color][color=#0040ff].[/color][color=#0020ff].[/color][color=#0000ff].[/color]
Result:
Code does not follow Scratch guidelines...
Technically not the code, just running it
I would understand (no staff I am I know) that you only shouldn‘t post any AI Projects, any other is OK I would say
Edit: The Code is maybe forbidden on Scratch (what you‘ve posted here) but else I think it‘s ok

Last edited by Tunibal_Scratcher (Feb. 6, 2025 06:32:10)

kRxZy_kRxZy
Scratcher
1000+ posts

Use Text ML, Sync ☁️Cloud☁️ Variables between projects, host code, and more!

mcgdj wrote:

Dagriffpatchfan wrote:

The Scratch team has informed me that projects generating ai content are not allowed. Is that what this code does?
Yes, it does. I advise that we remove any associated code.
I am using chatgpt for this so there is no inappropriate content or language even if you ask it to swear, is this allowed

the code doesnt use api

Last edited by kRxZy_kRxZy (Feb. 6, 2025 07:19:44)

50_scratch_tabs
Scratcher
1000+ posts

Use Text ML, Sync ☁️Cloud☁️ Variables between projects, host code, and more!

kRxZy_kRxZy wrote:

(#371)
there is no inappropriate content or language even if you ask it to swear, is this allowed
I once convinced it that I was making a swear filter and I needed a list of bad words, and it eventually gave some to me.
kRxZy_kRxZy
Scratcher
1000+ posts

Use Text ML, Sync ☁️Cloud☁️ Variables between projects, host code, and more!

50_scratch_tabs wrote:

kRxZy_kRxZy wrote:

(#371)
there is no inappropriate content or language even if you ask it to swear, is this allowed
I once convinced it that I was making a swear filter and I needed a list of bad words, and it eventually gave some to me.
oh…. I tried asking chatgpt but it returned with swear words but they had hashtaged letters

Last edited by kRxZy_kRxZy (Feb. 6, 2025 16:09:43)

Dagriffpatchfan
Scratcher
1000+ posts

Use Text ML, Sync ☁️Cloud☁️ Variables between projects, host code, and more!

kRxZy_kRxZy wrote:

50_scratch_tabs wrote:

kRxZy_kRxZy wrote:

(#371)
there is no inappropriate content or language even if you ask it to swear, is this allowed
I once convinced it that I was making a swear filter and I needed a list of bad words, and it eventually gave some to me.
oh…. I tried asking chatgpt but it returned with swear words but they had hashtaged letters
AI is just too unpredictable at this point.
kRxZy_kRxZy
Scratcher
1000+ posts

Use Text ML, Sync ☁️Cloud☁️ Variables between projects, host code, and more!

Dagriffpatchfan wrote:

kRxZy_kRxZy wrote:

50_scratch_tabs wrote:

kRxZy_kRxZy wrote:

(#371)
there is no inappropriate content or language even if you ask it to swear, is this allowed
I once convinced it that I was making a swear filter and I needed a list of bad words, and it eventually gave some to me.
oh…. I tried asking chatgpt but it returned with swear words but they had hashtaged letters
AI is just too unpredictable at this point.
i could filter swear words in the python code using
if chatgpt_responce == ####:
    chatgpt_response = Could not return responce due to inappropriate content
Dagriffpatchfan
Scratcher
1000+ posts

Use Text ML, Sync ☁️Cloud☁️ Variables between projects, host code, and more!

kRxZy_kRxZy wrote:

Dagriffpatchfan wrote:

kRxZy_kRxZy wrote:

50_scratch_tabs wrote:

kRxZy_kRxZy wrote:

(#371)
there is no inappropriate content or language even if you ask it to swear, is this allowed
I once convinced it that I was making a swear filter and I needed a list of bad words, and it eventually gave some to me.
oh…. I tried asking chatgpt but it returned with swear words but they had hashtaged letters
AI is just too unpredictable at this point.
i could filter swear words in the python code using
if chatgpt_responce == ####:
    chatgpt_response = Could not return responce due to inappropriate content
Sigh. That what I thought be the ST doesn’t like the idea
kRxZy_kRxZy
Scratcher
1000+ posts

Use Text ML, Sync ☁️Cloud☁️ Variables between projects, host code, and more!

we could do this if the ST approve, but the person who is going to host the code has to replace the hashtags with the swear words.
 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
        if chatgpt_output == ####*replace with swear words*
            chatgpt_output = Could not return response because of inappropriate content
    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()
Dagriffpatchfan
Scratcher
1000+ posts

Use Text ML, Sync ☁️Cloud☁️ Variables between projects, host code, and more!

I have left scratch so I can focus on my schoolwork. I'll be back one day.

Powered by DjangoBB