Update app.py
Browse files
app.py
CHANGED
|
@@ -7,13 +7,17 @@ from flask import Flask
|
|
| 7 |
import scratchattach as sa
|
| 8 |
|
| 9 |
# ==================== CONFIGURATION ====================
|
| 10 |
-
# Use
|
|
|
|
|
|
|
|
|
|
| 11 |
SCRATCH_USER = os.environ.get("SCRATCH_USER")
|
| 12 |
SCRATCH_PASS = os.environ.get("SCRATCH_PASS")
|
| 13 |
-
|
|
|
|
| 14 |
AI_URL = "https://16dvnk-spirit-kings-ai.hf.space/generate"
|
| 15 |
|
| 16 |
-
# ====================
|
| 17 |
PROXY_LIST = [
|
| 18 |
"http://8.219.97.248:80",
|
| 19 |
"http://38.158.83.233:999",
|
|
@@ -151,10 +155,10 @@ class ProxyRotator:
|
|
| 151 |
|
| 152 |
proxy_rotator = ProxyRotator(PROXY_LIST)
|
| 153 |
|
| 154 |
-
# ==================== AI CALL
|
| 155 |
def call_ai(prompt):
|
| 156 |
print(f"[AI] Calling AI with prompt: {prompt[:50]}...")
|
| 157 |
-
for attempt in range(len(PROXY_LIST)):
|
| 158 |
proxy = proxy_rotator.get_next_proxy()
|
| 159 |
proxy_url = proxy.get("http", "")
|
| 160 |
print(f"[AI] Attempt {attempt+1} using proxy {proxy_url}")
|
|
@@ -162,15 +166,15 @@ def call_ai(prompt):
|
|
| 162 |
response = requests.post(AI_URL, json={"prompt": prompt}, proxies=proxy, timeout=30)
|
| 163 |
if response.status_code == 200:
|
| 164 |
result = response.json().get("response", "Sorry, no response.")
|
| 165 |
-
print(f"[AI] Success
|
| 166 |
return result
|
| 167 |
else:
|
| 168 |
-
print(f"[AI] Proxy
|
| 169 |
proxy_rotator.mark_proxy_failed(proxy)
|
| 170 |
except Exception as e:
|
| 171 |
-
print(f"[AI] Proxy
|
| 172 |
proxy_rotator.mark_proxy_failed(proxy)
|
| 173 |
-
|
| 174 |
try:
|
| 175 |
response = requests.post(AI_URL, json={"prompt": prompt}, timeout=30)
|
| 176 |
if response.status_code == 200:
|
|
@@ -181,75 +185,56 @@ def call_ai(prompt):
|
|
| 181 |
|
| 182 |
# ==================== BRIDGE WORKER ====================
|
| 183 |
def bridge_worker():
|
| 184 |
-
print("[BRIDGE] Starting bridge with
|
| 185 |
print(f"[BRIDGE] Project ID: {PROJECT_ID}")
|
| 186 |
|
| 187 |
-
# Check if credentials are set
|
| 188 |
if not SCRATCH_USER or not SCRATCH_PASS:
|
| 189 |
-
print("[BRIDGE] ERROR: SCRATCH_USER and SCRATCH_PASS environment variables
|
| 190 |
-
return
|
| 191 |
-
|
| 192 |
-
# Login once using standard method
|
| 193 |
-
print(f"[BRIDGE] Logging in as {SCRATCH_USER}...")
|
| 194 |
-
try:
|
| 195 |
-
session = sa.login(SCRATCH_USER, SCRATCH_PASS)
|
| 196 |
-
print("[BRIDGE] Login successful. Connecting to cloud variables...")
|
| 197 |
-
conn = session.connect_cloud(PROJECT_ID)
|
| 198 |
-
print(f"[BRIDGE] Connected to project {PROJECT_ID}")
|
| 199 |
-
except Exception as e:
|
| 200 |
-
print(f"[BRIDGE] Login/Connect failed: {e}")
|
| 201 |
-
import traceback
|
| 202 |
-
traceback.print_exc()
|
| 203 |
return
|
| 204 |
|
| 205 |
-
last_prompt = "0"
|
| 206 |
while True:
|
| 207 |
try:
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
print(f"[BRIDGE]
|
| 213 |
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
|
| 230 |
-
|
| 231 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 232 |
except Exception as e:
|
| 233 |
-
print(f"[BRIDGE] Error
|
| 234 |
import traceback
|
| 235 |
traceback.print_exc()
|
| 236 |
print("[BRIDGE] Reconnecting in 10 seconds...")
|
| 237 |
time.sleep(10)
|
| 238 |
-
# Attempt to reconnect
|
| 239 |
-
try:
|
| 240 |
-
session = sa.login(SCRATCH_USER, SCRATCH_PASS)
|
| 241 |
-
conn = session.connect_cloud(PROJECT_ID)
|
| 242 |
-
print("[BRIDGE] Reconnected successfully")
|
| 243 |
-
last_prompt = "0" # Reset to avoid duplicate processing
|
| 244 |
-
except Exception as reconnect_error:
|
| 245 |
-
print(f"[BRIDGE] Reconnect failed: {reconnect_error}")
|
| 246 |
|
| 247 |
# ==================== FLASK APP ====================
|
| 248 |
app = Flask(__name__)
|
| 249 |
|
| 250 |
@app.route('/')
|
| 251 |
def health():
|
| 252 |
-
return "Scratch AI Bridge is running with
|
| 253 |
|
| 254 |
if __name__ == '__main__':
|
| 255 |
print("[MAIN] Starting bridge thread...")
|
|
|
|
| 7 |
import scratchattach as sa
|
| 8 |
|
| 9 |
# ==================== CONFIGURATION ====================
|
| 10 |
+
# Use the correct project ID (from your Scratch URL)
|
| 11 |
+
PROJECT_ID = "1298553820" # <-- Hardcoded, no need for environment variable
|
| 12 |
+
|
| 13 |
+
# Use username/password authentication (more reliable than session string)
|
| 14 |
SCRATCH_USER = os.environ.get("SCRATCH_USER")
|
| 15 |
SCRATCH_PASS = os.environ.get("SCRATCH_PASS")
|
| 16 |
+
|
| 17 |
+
# Your AI endpoint
|
| 18 |
AI_URL = "https://16dvnk-spirit-kings-ai.hf.space/generate"
|
| 19 |
|
| 20 |
+
# ==================== PROXY LIST ====================
|
| 21 |
PROXY_LIST = [
|
| 22 |
"http://8.219.97.248:80",
|
| 23 |
"http://38.158.83.233:999",
|
|
|
|
| 155 |
|
| 156 |
proxy_rotator = ProxyRotator(PROXY_LIST)
|
| 157 |
|
| 158 |
+
# ==================== AI CALL ====================
|
| 159 |
def call_ai(prompt):
|
| 160 |
print(f"[AI] Calling AI with prompt: {prompt[:50]}...")
|
| 161 |
+
for attempt in range(min(5, len(PROXY_LIST))):
|
| 162 |
proxy = proxy_rotator.get_next_proxy()
|
| 163 |
proxy_url = proxy.get("http", "")
|
| 164 |
print(f"[AI] Attempt {attempt+1} using proxy {proxy_url}")
|
|
|
|
| 166 |
response = requests.post(AI_URL, json={"prompt": prompt}, proxies=proxy, timeout=30)
|
| 167 |
if response.status_code == 200:
|
| 168 |
result = response.json().get("response", "Sorry, no response.")
|
| 169 |
+
print(f"[AI] Success")
|
| 170 |
return result
|
| 171 |
else:
|
| 172 |
+
print(f"[AI] Proxy failed: {response.status_code}")
|
| 173 |
proxy_rotator.mark_proxy_failed(proxy)
|
| 174 |
except Exception as e:
|
| 175 |
+
print(f"[AI] Proxy error: {e}")
|
| 176 |
proxy_rotator.mark_proxy_failed(proxy)
|
| 177 |
+
# Fallback direct
|
| 178 |
try:
|
| 179 |
response = requests.post(AI_URL, json={"prompt": prompt}, timeout=30)
|
| 180 |
if response.status_code == 200:
|
|
|
|
| 185 |
|
| 186 |
# ==================== BRIDGE WORKER ====================
|
| 187 |
def bridge_worker():
|
| 188 |
+
print("[BRIDGE] Starting bridge with correct project ID...")
|
| 189 |
print(f"[BRIDGE] Project ID: {PROJECT_ID}")
|
| 190 |
|
|
|
|
| 191 |
if not SCRATCH_USER or not SCRATCH_PASS:
|
| 192 |
+
print("[BRIDGE] ERROR: SCRATCH_USER and SCRATCH_PASS environment variables not set!")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 193 |
return
|
| 194 |
|
|
|
|
| 195 |
while True:
|
| 196 |
try:
|
| 197 |
+
print(f"[BRIDGE] Logging in as {SCRATCH_USER}...")
|
| 198 |
+
session = sa.login(SCRATCH_USER, SCRATCH_PASS)
|
| 199 |
+
print("[BRIDGE] Login successful. Connecting to cloud...")
|
| 200 |
+
cloud = session.connect_cloud(PROJECT_ID)
|
| 201 |
+
print(f"[BRIDGE] Connected to project {PROJECT_ID}")
|
| 202 |
|
| 203 |
+
# Set up event listener for real-time updates
|
| 204 |
+
events = cloud.events()
|
| 205 |
+
print("[BRIDGE] Event listener active. Waiting for 'prompt' changes...")
|
| 206 |
+
|
| 207 |
+
@events.event
|
| 208 |
+
def on_set(activity):
|
| 209 |
+
if activity.var == "prompt" and activity.value != "0":
|
| 210 |
+
print(f"[EVENT] Received prompt: {activity.value}")
|
| 211 |
+
decoded = decode_text(activity.value)
|
| 212 |
+
print(f"[EVENT] Decoded: {decoded}")
|
| 213 |
+
ai_response = call_ai(decoded)
|
| 214 |
+
print(f"[EVENT] AI response: {ai_response[:100]}...")
|
| 215 |
+
encoded_response = encode_text(ai_response)
|
| 216 |
+
cloud.set_var("response", encoded_response)
|
| 217 |
+
print("[EVENT] Response sent to Scratch")
|
| 218 |
|
| 219 |
+
events.start()
|
| 220 |
|
| 221 |
+
# Keep the thread alive
|
| 222 |
+
while True:
|
| 223 |
+
time.sleep(1)
|
| 224 |
+
|
| 225 |
except Exception as e:
|
| 226 |
+
print(f"[BRIDGE] Error: {e}")
|
| 227 |
import traceback
|
| 228 |
traceback.print_exc()
|
| 229 |
print("[BRIDGE] Reconnecting in 10 seconds...")
|
| 230 |
time.sleep(10)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 231 |
|
| 232 |
# ==================== FLASK APP ====================
|
| 233 |
app = Flask(__name__)
|
| 234 |
|
| 235 |
@app.route('/')
|
| 236 |
def health():
|
| 237 |
+
return "Scratch AI Bridge is running with correct project ID."
|
| 238 |
|
| 239 |
if __name__ == '__main__':
|
| 240 |
print("[MAIN] Starting bridge thread...")
|