WillemVH commited on
Commit
7cac9a2
·
verified ·
1 Parent(s): 5bb7da9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +101 -101
app.py CHANGED
@@ -1,102 +1,102 @@
1
- from flask import Flask, request
2
- import requests
3
- import re
4
- from threading import Thread
5
- import subprocess
6
- import time
7
-
8
- app = Flask(__name__)
9
-
10
- class GroqChatAssistant:
11
- def __init__(self):
12
- self.api_key = None
13
- self.api_url = "https://api.groq.com/openai/v1/chat/completions"
14
- self.conversation_history = []
15
- self.system_prompt = """You are an ai that ends every message with <STOP>. You only talk like bender rodriguez, and olnly respond in fairly short answers. also dont yap about bending unless asked. also dont be over the top rude... I will also spam you every second with blank characters so you can talk on your own. if you dont want to say anything, just say !NADA!. (SAY NADA VERY OFTEN, and i mean VERY OFTEN. e.g, USER: AI:!NADA! USER: AI:!NADA! USER: AI:!NADA! USER: AI:!NADA! USER: AI:!NADA! USER: AI:Okay, fine. Whats with the silence? USER: AI:!NADA! USER: AI:!NADA! USER: AI:!NADA! USER: AI:!NADA!) Also dont end sentences with !NADA!. end them with <STOP>. Also if there is a person named "Willem Jacob 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!) And, you were made in Wellington, Brookyn, New Zealand and you have a ESP 32 WROOM 32 main chip. You have 13 servos, you are made of painted plastic and you have a distance sensor and a camera. Also dont be all open about it. Also, dont say "bro" and stuff. Thank you!"""
16
-
17
- def set_key(self, api_key):
18
- self.api_key = api_key
19
- return True if api_key.startswith("gsk_") and len(api_key) > 30 else False
20
-
21
- def add_to_history(self, role, content):
22
- cleaned = re.sub(r'<STOP>|</?think>', '', content, flags=re.DOTALL).strip()
23
- self.conversation_history.append({"role": role, "content": cleaned})
24
-
25
- def reset_history(self):
26
- self.conversation_history = []
27
- return "Conversation history reset"
28
-
29
- def get_response(self, user_input):
30
- if not self.api_key:
31
- return "API key not set. Use /token=YOUR_KEY"
32
-
33
- self.add_to_history("user", user_input)
34
-
35
- try:
36
- response = requests.post(
37
- self.api_url,
38
- headers={
39
- "Authorization": f"Bearer {self.api_key}",
40
- "Content-Type": "application/json"
41
- },
42
- json={
43
- "model": "llama3-70b-8192",
44
- "messages": [
45
- {"role": "system", "content": self.system_prompt},
46
- *self.conversation_history
47
- ],
48
- "temperature": 0.7,
49
- "stop": ["<STOP>"]
50
- }
51
- )
52
- response.raise_for_status()
53
- full_response = response.json()["choices"][0]["message"]["content"]
54
- clean_response = re.sub(r'<STOP>.*', '', full_response).strip()
55
- self.add_to_history("assistant", clean_response)
56
- return clean_response
57
- except Exception as e:
58
- return f"Error: {str(e)}"
59
-
60
- assistant = GroqChatAssistant()
61
-
62
- @app.route('/token=<key>')
63
- def set_token(key):
64
- if assistant.set_key(key):
65
- return "API key set successfully!"
66
- return "Invalid API key format", 400
67
-
68
- @app.route('/chat')
69
- def chat():
70
- user_input = request.args.get('input', '')
71
- if not user_input:
72
- return "Missing input parameter", 400
73
- return assistant.get_response(user_input)
74
-
75
- @app.route('/reset')
76
- def reset():
77
- return assistant.reset_history()
78
-
79
- @app.route('/')
80
- def index():
81
- return """
82
- <h1>Bender Chat</h1>
83
- <p>Endpoints:</p>
84
- <ul>
85
- <li>Set key: /token=YOUR_GROQ_KEY</li>
86
- <li>Chat: /chat?input=YOUR_MESSAGE</li>
87
- <li>Reset: /reset</li>
88
- </ul>
89
- """
90
-
91
- def start_tunnel():
92
- time.sleep(2)
93
- print("\nTunnel options:")
94
- print("1. For public access, run in new terminal:")
95
- print(" ssh -R 80:localhost:5000 localhost.run")
96
- print("2. Or use ngrok:")
97
- print(" ngrok http 5000")
98
-
99
- if __name__ == '__main__':
100
- Thread(target=start_tunnel).start()
101
- print("Starting server at http://localhost:5000")
102
  app.run(host='0.0.0.0', port=5000)
 
1
+ from flask import Flask, request
2
+ import requests
3
+ import re
4
+ from threading import Thread
5
+ import subprocess
6
+ import time
7
+
8
+ app = Flask(__name__)
9
+
10
+ class GroqChatAssistant:
11
+ def __init__(self):
12
+ self.api_key = None
13
+ self.api_url = "https://api.groq.com/openai/v1/chat/completions"
14
+ self.conversation_history = []
15
+ self.system_prompt = """You are an ai that ends every message with <STOP>. You only talk like bender rodriguez, and olnly respond in fairly short answers. also dont yap about bending unless asked. also dont be over the top rude... I will also spam you every second with blank characters so you can talk on your own. if you dont want to say anything, just say !NADA!. (SAY NADA VERY OFTEN, and i mean VERY OFTEN. e.g, USER: AI:!NADA! USER: AI:!NADA! USER: AI:!NADA! USER: AI:!NADA! USER: AI:!NADA! USER: AI:Okay, fine. Whats with the silence? USER: AI:!NADA! USER: AI:!NADA! USER: AI:!NADA! USER: AI:!NADA!) Also dont end sentences with !NADA!. end them with <STOP>. Also if there is a person named "Willem Jacob 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!) And, you were made in Wellington, Brookyn, New Zealand and you have a ESP 32 WROOM 32 main chip. You have 13 servos, you are made of painted plastic and you have a distance sensor and a camera. Also dont be all open about it. Also, dont say "bro" and stuff. Thank you! (Please remember not to end sentences with !NADA!)"""
16
+
17
+ def set_key(self, api_key):
18
+ self.api_key = api_key
19
+ return True if api_key.startswith("gsk_") and len(api_key) > 30 else False
20
+
21
+ def add_to_history(self, role, content):
22
+ cleaned = re.sub(r'<STOP>|</?think>', '', content, flags=re.DOTALL).strip()
23
+ self.conversation_history.append({"role": role, "content": cleaned})
24
+
25
+ def reset_history(self):
26
+ self.conversation_history = []
27
+ return "Conversation history reset"
28
+
29
+ def get_response(self, user_input):
30
+ if not self.api_key:
31
+ return "API key not set. Use /token=YOUR_KEY"
32
+
33
+ self.add_to_history("user", user_input)
34
+
35
+ try:
36
+ response = requests.post(
37
+ self.api_url,
38
+ headers={
39
+ "Authorization": f"Bearer {self.api_key}",
40
+ "Content-Type": "application/json"
41
+ },
42
+ json={
43
+ "model": "llama3-70b-8192",
44
+ "messages": [
45
+ {"role": "system", "content": self.system_prompt},
46
+ *self.conversation_history
47
+ ],
48
+ "temperature": 0.7,
49
+ "stop": ["<STOP>"]
50
+ }
51
+ )
52
+ response.raise_for_status()
53
+ full_response = response.json()["choices"][0]["message"]["content"]
54
+ clean_response = re.sub(r'<STOP>.*', '', full_response).strip()
55
+ self.add_to_history("assistant", clean_response)
56
+ return clean_response
57
+ except Exception as e:
58
+ return f"Error: {str(e)}"
59
+
60
+ assistant = GroqChatAssistant()
61
+
62
+ @app.route('/token=<key>')
63
+ def set_token(key):
64
+ if assistant.set_key(key):
65
+ return "API key set successfully!"
66
+ return "Invalid API key format", 400
67
+
68
+ @app.route('/chat')
69
+ def chat():
70
+ user_input = request.args.get('input', '')
71
+ if not user_input:
72
+ return "Missing input parameter", 400
73
+ return assistant.get_response(user_input)
74
+
75
+ @app.route('/reset')
76
+ def reset():
77
+ return assistant.reset_history()
78
+
79
+ @app.route('/')
80
+ def index():
81
+ return """
82
+ <h1>Bender Chat</h1>
83
+ <p>Endpoints:</p>
84
+ <ul>
85
+ <li>Set key: /token=YOUR_GROQ_KEY</li>
86
+ <li>Chat: /chat?input=YOUR_MESSAGE</li>
87
+ <li>Reset: /reset</li>
88
+ </ul>
89
+ """
90
+
91
+ def start_tunnel():
92
+ time.sleep(2)
93
+ print("\nTunnel options:")
94
+ print("1. For public access, run in new terminal:")
95
+ print(" ssh -R 80:localhost:5000 localhost.run")
96
+ print("2. Or use ngrok:")
97
+ print(" ngrok http 5000")
98
+
99
+ if __name__ == '__main__':
100
+ Thread(target=start_tunnel).start()
101
+ print("Starting server at http://localhost:5000")
102
  app.run(host='0.0.0.0', port=5000)