WillemVH commited on
Commit
7b323ba
·
verified ·
1 Parent(s): 98f4864

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -35
app.py CHANGED
@@ -1,11 +1,20 @@
1
  import scratchattach as sa
2
- from groq import Groq
3
  import os
4
  from flask import Flask, request
5
  import requests
 
6
  import re
7
  from threading import Thread
 
 
8
 
 
 
 
 
 
 
 
9
  sessionid = os.getenv("ID")
10
  useridname = os.getenv("USR")
11
  # Initialize Scratch session
@@ -13,51 +22,18 @@ session = sa.login_by_id(sessionid, username=useridname)
13
  cloud = session.connect_cloud("1185047933")
14
  client = cloud.requests()
15
 
16
- # Initialize Groq client
17
- groq_client = Groq(api_key="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")
18
-
19
- # Global chat history list
20
- chat_history = [
21
- {
22
- "role": "system",
23
- "content": "You are a helpful AI assistant that only responds in plain text. Also, you are on scratch. like ur literally an api on scratch.mit.edu. Please be as kind as you can and make sure all code works before sharing. Thanks!"
24
- }
25
- ]
26
-
27
  # Request 1: Get Groq response (with context retention)
28
  @client.request
29
  def get_groq_response(user_input):
30
  global chat_history
31
 
32
  try:
33
- # Add user input to history
34
- chat_history.append({"role": "user", "content": user_input})
35
-
36
- # Get response from Groq (with full history)
37
- chat_completion = groq_client.chat.completions.create(
38
- messages=chat_history,
39
- model="deepseek-r1-distill-llama-70b",
40
- )
41
-
42
- # Extract and store the AI's response
43
- ai_response = chat_completion.choices[0].message.content
44
- chat_history.append({"role": "assistant", "content": ai_response})
45
  return ai_response
46
 
47
  except Exception as e:
48
  return f"Error: {str(e)}"
49
 
50
- # Request 2: Clear chat history
51
- @client.request
52
- def clear_history():
53
- global chat_history
54
- chat_history = [
55
- {
56
- "role": "system",
57
- "content": "You are a helpful AI assistant that only responds in plain text. Also, you are on scratch. like ur literally an api on scratch.mit.edu. Please be as kind as you can and make sure all code works before sharing. Thanks!"
58
- }
59
- ]
60
- return "Chat history cleared!"
61
 
62
  client.start(thread=True)
63
 
 
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
 
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
  return ai_response
33
 
34
  except Exception as e:
35
  return f"Error: {str(e)}"
36
 
 
 
 
 
 
 
 
 
 
 
 
37
 
38
  client.start(thread=True)
39