Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,47 +1,69 @@
|
|
| 1 |
import scratchattach as sa
|
| 2 |
import os
|
| 3 |
-
from flask import Flask
|
| 4 |
-
import requests
|
| 5 |
import subprocess
|
| 6 |
-
import re
|
| 7 |
from threading import Thread
|
|
|
|
|
|
|
|
|
|
| 8 |
def Curl(URL):
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
try:
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
-
|
| 37 |
-
|
| 38 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
-
app = Flask(__name__)
|
| 43 |
@app.route('/')
|
| 44 |
def index():
|
| 45 |
-
return ""
|
| 46 |
-
|
| 47 |
-
|
|
|
|
|
|
|
|
|
| 1 |
import scratchattach as sa
|
| 2 |
import os
|
| 3 |
+
from flask import Flask
|
|
|
|
| 4 |
import subprocess
|
|
|
|
| 5 |
from threading import Thread
|
| 6 |
+
|
| 7 |
+
app = Flask(__name__)
|
| 8 |
+
|
| 9 |
def Curl(URL):
|
| 10 |
+
# Added a timeout to prevent the app from hanging forever
|
| 11 |
+
curl_command = ['curl', '-s', '-X', 'GET', URL]
|
| 12 |
+
try:
|
| 13 |
+
result = subprocess.run(curl_command,
|
| 14 |
+
stdout=subprocess.PIPE,
|
| 15 |
+
stderr=subprocess.PIPE,
|
| 16 |
+
text=True,
|
| 17 |
+
timeout=10)
|
| 18 |
+
if result.returncode == 0:
|
| 19 |
+
# Note: .replace() returns a NEW string, it doesn't modify the original
|
| 20 |
+
response = result.stdout
|
| 21 |
+
response = response.replace("<", "BTTL").replace(">", "BTTR")
|
| 22 |
+
return response
|
| 23 |
+
except Exception as e:
|
| 24 |
+
return f"Curl Error: {str(e)}"
|
| 25 |
+
return "No response"
|
| 26 |
+
|
| 27 |
+
def run_scratch_bot():
|
| 28 |
+
sessionid = os.getenv("ID")
|
| 29 |
+
useridname = os.getenv("USR")
|
| 30 |
+
|
| 31 |
+
if not sessionid:
|
| 32 |
+
print("CRITICAL: ID (Session ID) environment variable is missing!")
|
| 33 |
+
return
|
| 34 |
|
| 35 |
try:
|
| 36 |
+
# Initialize Scratch session
|
| 37 |
+
session = sa.login_by_id(sessionid, username=useridname)
|
| 38 |
+
|
| 39 |
+
# Check if login actually worked
|
| 40 |
+
if session.xtoken is None:
|
| 41 |
+
print("FAILED: Session ID is invalid or expired.")
|
| 42 |
+
return
|
| 43 |
|
| 44 |
+
cloud = session.connect_cloud("1185047933")
|
| 45 |
+
client = cloud.requests()
|
| 46 |
|
| 47 |
+
@client.request
|
| 48 |
+
def get_groq_response(user_input):
|
| 49 |
+
try:
|
| 50 |
+
return Curl(user_input)
|
| 51 |
+
except Exception as e:
|
| 52 |
+
return f"Error: {str(e)}"
|
| 53 |
|
| 54 |
+
print("Scratch bot is starting...")
|
| 55 |
+
client.run() # This runs inside the thread
|
| 56 |
+
except Exception as e:
|
| 57 |
+
print(f"Scratch connection error: {e}")
|
| 58 |
+
|
| 59 |
+
# Start the Scratch bot in a background thread so Flask can boot independently
|
| 60 |
+
thread = Thread(target=run_scratch_bot)
|
| 61 |
+
thread.start()
|
| 62 |
|
|
|
|
| 63 |
@app.route('/')
|
| 64 |
def index():
|
| 65 |
+
return "Go to https://scratch.mit.edu/projects/1185047933/ for a live demo."
|
| 66 |
+
|
| 67 |
+
if __name__ == "__main__":
|
| 68 |
+
# Port 7860 is required for Hugging Face
|
| 69 |
+
app.run(host="0.0.0.0", port=7860)
|