ScratchWeb / app.py
WillemVH's picture
Create app.py
eb0b460 verified
raw
history blame
1.65 kB
import scratchattach as sa
from groq import Groq
# Initialize Scratch session
session = sa.login_by_id(".eJxVkMFugzAQRP-Fc6E2YBtySw8Vh7SKqlZqekFrvASXYEdgRJWq_95F4hKtvIeZ0duRf6N5wtHBgNEuiuMD3HDsIcRx9BAF36MjWTNoCzAK04zltEtQaau5AW40yFLsllodq-ev2S3H0-HFVyz-edeZqz4-e8Jc_Nm62F6JlKokFWnCGT1ZkFfDHLp6rVBbQwEuaLjKc_LMN7izr4Md8Obd2m8_4GgbeHzFpT75sb8HdDB1FEKmMskbIVrBsMhLoxjKBqQQ3EjNSykyg5itFwJOofG-tyt8ISCae6SGhr5gLbZq6AJdD9a7ZDOm5A2vl0182sJ__wOwbSg:1uMIsI:0WrcA-cUZ4-PPGmKUGGpjP_ldzw", username="--Lazerkat--")
cloud = session.connect_cloud("1183313747")
client = cloud.requests()
# Initialize Groq client
groq_client = Groq(api_key="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")
# Global chat history list
chat_history = []
# Request 1: Get Groq response (with context retention)
@client.request
def get_groq_response(user_input):
global chat_history
try:
# Add user input to history
chat_history.append({"role": "user", "content": user_input})
# Get response from Groq (with full history)
chat_completion = groq_client.chat.completions.create(
messages=chat_history,
model="deepseek-r1-distill-llama-70b",
)
# Extract and store the AI's response
ai_response = chat_completion.choices[0].message.content
chat_history.append({"role": "assistant", "content": ai_response})
return ai_response
except Exception as e:
return f"Error: {str(e)}"
# Request 2: Clear chat history
@client.request
def clear_history():
global chat_history
chat_history = []
return "Chat history cleared!"
client.start(thread=True)