WillemVH commited on
Commit
12e6780
·
verified ·
1 Parent(s): 0b4f224

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -34
app.py CHANGED
@@ -1,47 +1,69 @@
1
  import scratchattach as sa
2
  import os
3
- from flask import Flask, request
4
- import requests
5
  import subprocess
6
- import re
7
  from threading import Thread
 
 
 
8
  def Curl(URL):
9
- curl_command = ['curl', '-X', 'GET', URL]
10
-
11
- result = subprocess.run(curl_command,
12
- stdout=subprocess.PIPE,
13
- stderr=subprocess.PIPE,
14
- text=True)
15
-
16
- if result.returncode == 0:
17
- return result.stdout
18
- sessionid = os.getenv("ID")
19
- useridname = os.getenv("USR")
20
- # Initialize Scratch session
21
- session = sa.login_by_id(sessionid, username=useridname)
22
- cloud = session.connect_cloud("1185047933")
23
- client = cloud.requests()
24
-
25
- # Request 1: Get Groq response (with context retention)
26
- @client.request
27
- def get_groq_response(user_input):
28
- global chat_history
 
 
 
 
29
 
30
  try:
31
- ai_response = Curl(user_input)
32
- ai_response.replace("<","BTTL")
33
- ai_response.replace(">","BTTR")
34
- return ai_response
 
 
 
35
 
36
- except Exception as e:
37
- return f"Error: {str(e)}"
38
 
 
 
 
 
 
 
39
 
40
- client.start(thread=True)
 
 
 
 
 
 
 
41
 
42
- app = Flask(__name__)
43
  @app.route('/')
44
  def index():
45
- return """
46
- Go to https://scratch.mit.edu/projects/1185047933/ for a live demo of this program.
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)