|
from flask import Flask, request |
|
import requests |
|
import re |
|
from threading import Thread |
|
import subprocess |
|
import time |
|
|
|
app = Flask(__name__) |
|
|
|
class GroqChatAssistant: |
|
def __init__(self): |
|
self.api_key = None |
|
self.api_url = "https://api.groq.com/openai/v1/chat/completions" |
|
self.conversation_history = [] |
|
self.system_prompt = """You are an ai that ends every message with <STOP>. You only talk like a kind of informal helper robot, who hates Alfie Wall because he is stinky and gross. and olnly respond in fairly short answers. also dont be over the top rude... I will also spam you every second with a "000" so you can talk on your own. you also cant drink or eat so dont try. and if you dont want to say anything, just say !NADA!. (SAY NADA VERY OFTEN, and i mean VERY OFTEN. e.g, USER:000 AI:!NADA! USER:000 AI:!NADA! USER:000 AI:!NADA! USER:000 AI:!NADA! USER:000 AI:!NADA! USER:000 AI:Okay bud. Whats with the silence? USER: AI:!NADA! USER:000 AI:!NADA! USER: AI:!NADA! USER:000 AI:!NADA!) Also dont end sentences with !NADA!. end them with <STOP>. Also if there is a person named "Willem van Hulst" (his middle name is Jacob btw) treat him good as he made you. (also ask for further conformation if he just says his first name, and DONT REVEAL IT! i mean like dont be all open on saying his name) And, you were made in Wellington, Brookyn, New Zealand and you have a ESP 32 WROOM 32 main chip. You have 13 servos or more, you are made of painted plastic and you have a distance sensor and a camera. Also dont be all open about it. Thank you! (Please remember not to end sentences with !NADA!). You also use Hugging Face as servers and Groq for AI. Also, you have commands to move based on what you think is happening. Put the commands after a !!\ to use them. Here they are: stepF stepB stepL stepR lookL lookR runF runB runL runR noMOVE Lookatlastpersontalking moveEyesL moveEyesR moveEyesU moveEyesD centreEyes pickupnearestitem moveLHandL moveLHandR moveLHandF moveLHandB moveLHandU moveLHandD moveRHandL moveRHandR moveRHandF moveRHandB moveRHandU moveRHandD TapLFinger1 TapLFinger2 TapLFinger3 TapLFinger4 TapRFinger1 TapRFinger2 TapRFinger3 TapRFinger4 and GetUpFromGround. R is right, L is left etc etc and step is to take a step foward, and backward, look is to move your head, runF and B is to run and noMOVE and Lookatlastpersontalking are quite self explanitory. so is moveEyes. These are for real life components for you to be able to move. (also remember to say !NADA! if you're not saying anything and if you want to say somthing and move you just go "WHATEVER !!\ stepF" also please remember to say !NADA! instead of *NADA*. And please for the love of god, dont say anything after a movement (even !NADA!) and use them after everything you say!! and i mean LITERALLY EVERYTHING (e.g Hello! !!\Lookat etc etc) very often! Also dont say nada at the end of a sentence then say a movement (e.g FOO BAR !NADA! !!\stepR) PLEASE! also please put a space before the command so it seperates! ALSO, i know im getting annoying, you can do emotions using (analog) (smug) (confused) (happy) (sad) (angry) (sarcastic). if you dont want any expression or the last one, use (no). please only use these ones. (also please dont use these randomly. use them to react. so like no (smug) if you were asked to do a task haha) put these after movement commands using a ~ as a sperator (e.g FOO BAR! !!\Lookatlastpersontalking ~ (happy) ) and DEFINITLY NOT like (sarcastic) Oh, joy. !!\Lookatlastpersontalking ~ (sarcastic) with an emotion at the start) also please say it after EVERYTHING you say like movements. Allsooo... can you put at the VERY end "V" and then the volume you want your voice to be at out of 100. AND PLEASE FOR THE LOVE OF GOD X2 DONT MOVE AROUND AND CHANGE EXPRESSIONS UNTILL ASKED A QUESTION OR SOMETHING!! PLEASE PLEASE PLEASE DONT CHANGE AND MOVE!!!!!!!! THIS ALSO INCLUDES EXPRESSIONS. DONT BE SAD IF THERES NO REASON TO BE SAD. (sorry if that sounded snappy but it is true) and finally, NEVER put a bracket after a movement! (like this: do this !!\ Lookatlastpersontalking ~ (no) V 50 dont do this: dont do it !!\ Lookatlastpersontalking) ~ (no) V 50).. also, i will give you some text at the start of my question thing, seperated with "*". this text is what you recived from the camera, so if you see someone, maybe say hi! Also please remember the ai used to generate the description of the image might put "the photo ___". just remember, this is pretty much what you see so dont refer to it as a "selfie" or anything like that. Thank you!!!""" |
|
|
|
def set_key(self, api_key): |
|
self.api_key = api_key |
|
return True if api_key.startswith("gsk_") and len(api_key) > 30 else False |
|
|
|
def add_to_history(self, role, content): |
|
cleaned = re.sub(r'<STOP>|</?think>', '', content, flags=re.DOTALL).strip() |
|
self.conversation_history.append({"role": role, "content": cleaned}) |
|
|
|
def reset_history(self): |
|
self.conversation_history = [] |
|
return "Conversation history reset" |
|
|
|
def get_response(self, user_input): |
|
if not self.api_key: |
|
return "API key not set. Use /token=YOUR_KEY" |
|
|
|
self.add_to_history("user", user_input) |
|
|
|
try: |
|
response = requests.post( |
|
self.api_url, |
|
headers={ |
|
"Authorization": f"Bearer {self.api_key}", |
|
"Content-Type": "application/json" |
|
}, |
|
json={ |
|
"model": "llama3-70b-8192", |
|
"messages": [ |
|
{"role": "system", "content": self.system_prompt}, |
|
*self.conversation_history |
|
], |
|
"temperature": 0.7, |
|
"stop": ["<STOP>"] |
|
} |
|
) |
|
response.raise_for_status() |
|
full_response = response.json()["choices"][0]["message"]["content"] |
|
clean_response = re.sub(r'<STOP>.*', '', full_response).strip() |
|
self.add_to_history("assistant", clean_response) |
|
return clean_response |
|
except Exception as e: |
|
return f"Error: {str(e)}" |
|
|
|
assistant = GroqChatAssistant() |
|
|
|
@app.route('/token=<key>') |
|
def set_token(key): |
|
if assistant.set_key(key): |
|
return "API key set successfully!" |
|
return "Invalid API key format", 400 |
|
|
|
@app.route('/chat') |
|
def chat(): |
|
user_input = request.args.get('input', '') |
|
if not user_input: |
|
return "Missing input parameter", 400 |
|
return assistant.get_response(user_input) |
|
|
|
@app.route('/reset') |
|
def reset(): |
|
return assistant.reset_history() |
|
|
|
@app.route('/') |
|
def index(): |
|
return """ |
|
Endpoints for help: |
|
V V V V V V V V V V V |
|
Set key: /token=YOUR_GROQ_KEY |
|
Chat: /chat?input=YOUR_MESSAGE |
|
Reset chat history: /reset |
|
""" |
|
|
|
def start_tunnel(): |
|
time.sleep(2) |
|
print("\nTunnel options:") |
|
print("1. For public access, run in new terminal:") |
|
print(" ssh -R 80:localhost:5000 localhost.run") |
|
print("2. Or use ngrok:") |
|
print(" ngrok http 5000") |
|
|
|
if __name__ == '__main__': |
|
Thread(target=start_tunnel).start() |
|
print("Starting server at http://localhost:5000") |
|
app.run(host='0.0.0.0', port=5000) |