Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import scratchattach as sa
|
| 2 |
+
from groq import Groq
|
| 3 |
+
|
| 4 |
+
# Initialize Scratch session
|
| 5 |
+
session = sa.login_by_id(".eJxVkMFugzAQRP-Fc6E2YBtySw8Vh7SKqlZqekFrvASXYEdgRJWq_95F4hKtvIeZ0duRf6N5wtHBgNEuiuMD3HDsIcRx9BAF36MjWTNoCzAK04zltEtQaau5AW40yFLsllodq-ev2S3H0-HFVyz-edeZqz4-e8Jc_Nm62F6JlKokFWnCGT1ZkFfDHLp6rVBbQwEuaLjKc_LMN7izr4Md8Obd2m8_4GgbeHzFpT75sb8HdDB1FEKmMskbIVrBsMhLoxjKBqQQ3EjNSykyg5itFwJOofG-tyt8ISCae6SGhr5gLbZq6AJdD9a7ZDOm5A2vl0182sJ__wOwbSg:1uMIsI:0WrcA-cUZ4-PPGmKUGGpjP_ldzw", username="--Lazerkat--")
|
| 6 |
+
cloud = session.connect_cloud("1183313747")
|
| 7 |
+
client = cloud.requests()
|
| 8 |
+
|
| 9 |
+
# Initialize Groq client
|
| 10 |
+
groq_client = Groq(api_key="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")
|
| 11 |
+
|
| 12 |
+
# Global chat history list
|
| 13 |
+
chat_history = []
|
| 14 |
+
|
| 15 |
+
# Request 1: Get Groq response (with context retention)
|
| 16 |
+
@client.request
|
| 17 |
+
def get_groq_response(user_input):
|
| 18 |
+
global chat_history
|
| 19 |
+
|
| 20 |
+
try:
|
| 21 |
+
# Add user input to history
|
| 22 |
+
chat_history.append({"role": "user", "content": user_input})
|
| 23 |
+
|
| 24 |
+
# Get response from Groq (with full history)
|
| 25 |
+
chat_completion = groq_client.chat.completions.create(
|
| 26 |
+
messages=chat_history,
|
| 27 |
+
model="deepseek-r1-distill-llama-70b",
|
| 28 |
+
)
|
| 29 |
+
|
| 30 |
+
# Extract and store the AI's response
|
| 31 |
+
ai_response = chat_completion.choices[0].message.content
|
| 32 |
+
chat_history.append({"role": "assistant", "content": ai_response})
|
| 33 |
+
|
| 34 |
+
return ai_response
|
| 35 |
+
|
| 36 |
+
except Exception as e:
|
| 37 |
+
return f"Error: {str(e)}"
|
| 38 |
+
|
| 39 |
+
# Request 2: Clear chat history
|
| 40 |
+
@client.request
|
| 41 |
+
def clear_history():
|
| 42 |
+
global chat_history
|
| 43 |
+
chat_history = []
|
| 44 |
+
return "Chat history cleared!"
|
| 45 |
+
|
| 46 |
+
client.start(thread=True)
|