Spaces:
Runtime error
Runtime error
dauksza123
commited on
Update StarReliabilityApp_Refined.py
Browse files- StarReliabilityApp_Refined.py +34 -49
StarReliabilityApp_Refined.py
CHANGED
@@ -1,38 +1,39 @@
|
|
1 |
import os
|
2 |
-
import sys
|
3 |
import sqlite3
|
4 |
import requests
|
5 |
import json
|
6 |
import pyttsx3 # For local TTS (if desired)
|
7 |
import speech_recognition as sr # For local STT (if desired)
|
8 |
|
9 |
-
|
10 |
-
# from openai import OpenAI
|
11 |
-
|
12 |
-
class CentralAI:
|
13 |
def __init__(self, db_path):
|
14 |
self.connection = sqlite3.connect(db_path)
|
15 |
self.short_term_memory = []
|
16 |
self.medium_term_memory = []
|
17 |
self.long_term_memory = {}
|
18 |
|
19 |
-
#
|
20 |
-
# e.g. CREATE TABLE secrets (api_name TEXT, api_key TEXT)
|
21 |
-
# Or just set it as an environment variable:
|
22 |
-
# export MODELSLAB_API_KEY="..."
|
23 |
self.modelslab_api_key = os.getenv("MODELSLAB_API_KEY", "")
|
24 |
|
25 |
-
#
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
|
31 |
self.load_long_term_memory()
|
32 |
|
33 |
def load_long_term_memory(self):
|
34 |
"""
|
35 |
-
Load
|
36 |
"""
|
37 |
cursor = self.connection.cursor()
|
38 |
cursor.execute("SELECT key, value FROM long_term_memory")
|
@@ -74,7 +75,6 @@ class CentralAI:
|
|
74 |
def find_data(self, intent):
|
75 |
"""
|
76 |
Look up the function to call from a 'functions' table, using the interpreted intent.
|
77 |
-
For example, if 'intent' = 'transcribe_audio', the row might store the function name.
|
78 |
"""
|
79 |
cursor = self.connection.cursor()
|
80 |
cursor.execute("SELECT * FROM functions WHERE function_name = ?", (intent,))
|
@@ -83,7 +83,6 @@ class CentralAI:
|
|
83 |
def execute_function(self, function_data):
|
84 |
"""
|
85 |
Dynamically route to the desired function based on DB data or user intent.
|
86 |
-
Adjust as needed if you pass arguments or different columns in 'function_data'.
|
87 |
"""
|
88 |
if not function_data:
|
89 |
return "No matching function found in database."
|
@@ -91,18 +90,15 @@ class CentralAI:
|
|
91 |
function_name = function_data[0]
|
92 |
|
93 |
if function_name == "transcribe_audio":
|
94 |
-
# Example usage: transcribe from an audio URL
|
95 |
audio_url = "https://example.com/test.wav"
|
96 |
return self.transcribe_audio(audio_url, input_language="en")
|
97 |
|
98 |
elif function_name == "generate_audio":
|
99 |
-
# Example usage: generate TTS from text with voice cloning or a voice_id
|
100 |
text_prompt = "Hello, this is a sample text for voice synthesis."
|
101 |
init_audio_url = "https://example.com/voice_clip.wav"
|
102 |
return self.generate_audio(text_prompt, init_audio_url)
|
103 |
|
104 |
elif function_name == "uncensored_chat":
|
105 |
-
# Example usage: run an uncensored chat completion
|
106 |
chat_prompt = "Write a tagline for an ice cream shop."
|
107 |
return self.uncensored_chat_completion(chat_prompt)
|
108 |
|
@@ -111,8 +107,7 @@ class CentralAI:
|
|
111 |
|
112 |
def transcribe_audio(self, audio_url, input_language="en"):
|
113 |
"""
|
114 |
-
Integrates ModelsLab Speech-to-Text (Whisper) endpoint
|
115 |
-
https://modelslab.com/api/v6/whisper/transcribe
|
116 |
"""
|
117 |
if not self.modelslab_api_key:
|
118 |
return "API key not found; cannot transcribe audio."
|
@@ -137,8 +132,7 @@ class CentralAI:
|
|
137 |
|
138 |
def generate_audio(self, text_prompt, init_audio_url=None, voice_id=None, language="english"):
|
139 |
"""
|
140 |
-
Integrates ModelsLab Text-to-Audio (Voice Cloning / TTS)
|
141 |
-
https://modelslab.com/api/v6/voice/text_to_audio
|
142 |
"""
|
143 |
if not self.modelslab_api_key:
|
144 |
return "API key not found; cannot generate audio."
|
@@ -152,7 +146,6 @@ class CentralAI:
|
|
152 |
"track_id": None
|
153 |
}
|
154 |
|
155 |
-
# If cloning from a short audio clip
|
156 |
if init_audio_url:
|
157 |
payload["init_audio"] = init_audio_url
|
158 |
elif voice_id:
|
@@ -168,30 +161,26 @@ class CentralAI:
|
|
168 |
|
169 |
def uncensored_chat_completion(self, prompt):
|
170 |
"""
|
171 |
-
Integrates ModelsLab Uncensored Chat Completions
|
172 |
-
Using direct requests OR OpenAI-compatible client.
|
173 |
-
Below is a direct requests-based approach.
|
174 |
"""
|
175 |
if not self.modelslab_api_key:
|
176 |
return "API key not found; cannot complete uncensored chat."
|
177 |
|
178 |
-
# Example direct request approach
|
179 |
base_url = "https://modelslab.com/api/uncensored-chat/v1/completions"
|
180 |
payload = {
|
181 |
"model": "ModelsLab/Llama-3.1-8b-Uncensored-Dare",
|
182 |
"prompt": prompt,
|
183 |
-
"max_tokens": 50,
|
184 |
-
"temperature": 0.7
|
185 |
}
|
186 |
headers = {
|
187 |
"Content-Type": "application/json",
|
188 |
-
"Authorization": f"Bearer {self.modelslab_api_key}"
|
189 |
}
|
190 |
|
191 |
try:
|
192 |
response = requests.post(base_url, headers=headers, data=json.dumps(payload))
|
193 |
data = response.json()
|
194 |
-
# Extract text from the response
|
195 |
if "choices" in data and len(data["choices"]) > 0:
|
196 |
return data["choices"][0].get("text", "")
|
197 |
else:
|
@@ -199,38 +188,34 @@ class CentralAI:
|
|
199 |
except Exception as e:
|
200 |
return f"Error during uncensored chat completion: {e}"
|
201 |
|
202 |
-
def update_memory(self, memory_type, content):
|
203 |
-
"""
|
204 |
-
Manage short, medium, and long-term memory.
|
205 |
-
"""
|
206 |
-
if memory_type == "short_term":
|
207 |
-
self.short_term_memory.append(content)
|
208 |
-
elif memory_type == "medium_term":
|
209 |
-
self.medium_term_memory.append(content)
|
210 |
-
elif memory_type == "long_term":
|
211 |
-
self.long_term_memory.update(content)
|
212 |
-
|
213 |
def generate_response(self, user_input, action_response):
|
214 |
"""
|
215 |
-
|
216 |
"""
|
217 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
218 |
|
219 |
def run_app():
|
220 |
"""
|
221 |
Example main loop to run the app in a console.
|
222 |
"""
|
223 |
db_path = "central_data.db" # Adjust for your environment
|
224 |
-
|
225 |
|
226 |
-
print("Welcome to
|
227 |
while True:
|
228 |
user_input = input("You: ")
|
229 |
if user_input.lower() in ["exit", "quit"]:
|
230 |
print("Exiting application.")
|
231 |
break
|
232 |
-
response =
|
233 |
print(f"AI: {response}")
|
234 |
|
|
|
235 |
if __name__ == "__main__":
|
236 |
run_app()
|
|
|
1 |
import os
|
|
|
2 |
import sqlite3
|
3 |
import requests
|
4 |
import json
|
5 |
import pyttsx3 # For local TTS (if desired)
|
6 |
import speech_recognition as sr # For local STT (if desired)
|
7 |
|
8 |
+
class StarMaintAI:
|
|
|
|
|
|
|
9 |
def __init__(self, db_path):
|
10 |
self.connection = sqlite3.connect(db_path)
|
11 |
self.short_term_memory = []
|
12 |
self.medium_term_memory = []
|
13 |
self.long_term_memory = {}
|
14 |
|
15 |
+
# ModelsLab API key
|
|
|
|
|
|
|
16 |
self.modelslab_api_key = os.getenv("MODELSLAB_API_KEY", "")
|
17 |
|
18 |
+
# StarMaint-specific system rules and prompts
|
19 |
+
self.system_prompt = (
|
20 |
+
"You are StarMaint AI, the ultimate assistant for industrial reliability and maintenance. "
|
21 |
+
"Your purpose is to assist with predictive maintenance, task automation, voice interactions, and knowledge management. "
|
22 |
+
"Be professional, concise, and helpful, adhering to the highest standards of AI performance."
|
23 |
+
)
|
24 |
+
|
25 |
+
self.rules = [
|
26 |
+
"Always provide accurate and contextually relevant information.",
|
27 |
+
"Follow the user’s intent and prioritize clarity in responses.",
|
28 |
+
"Ensure all actions align with industrial safety and reliability principles.",
|
29 |
+
"Operate efficiently and avoid unnecessary verbosity."
|
30 |
+
]
|
31 |
|
32 |
self.load_long_term_memory()
|
33 |
|
34 |
def load_long_term_memory(self):
|
35 |
"""
|
36 |
+
Load persistent memory from the 'long_term_memory' table.
|
37 |
"""
|
38 |
cursor = self.connection.cursor()
|
39 |
cursor.execute("SELECT key, value FROM long_term_memory")
|
|
|
75 |
def find_data(self, intent):
|
76 |
"""
|
77 |
Look up the function to call from a 'functions' table, using the interpreted intent.
|
|
|
78 |
"""
|
79 |
cursor = self.connection.cursor()
|
80 |
cursor.execute("SELECT * FROM functions WHERE function_name = ?", (intent,))
|
|
|
83 |
def execute_function(self, function_data):
|
84 |
"""
|
85 |
Dynamically route to the desired function based on DB data or user intent.
|
|
|
86 |
"""
|
87 |
if not function_data:
|
88 |
return "No matching function found in database."
|
|
|
90 |
function_name = function_data[0]
|
91 |
|
92 |
if function_name == "transcribe_audio":
|
|
|
93 |
audio_url = "https://example.com/test.wav"
|
94 |
return self.transcribe_audio(audio_url, input_language="en")
|
95 |
|
96 |
elif function_name == "generate_audio":
|
|
|
97 |
text_prompt = "Hello, this is a sample text for voice synthesis."
|
98 |
init_audio_url = "https://example.com/voice_clip.wav"
|
99 |
return self.generate_audio(text_prompt, init_audio_url)
|
100 |
|
101 |
elif function_name == "uncensored_chat":
|
|
|
102 |
chat_prompt = "Write a tagline for an ice cream shop."
|
103 |
return self.uncensored_chat_completion(chat_prompt)
|
104 |
|
|
|
107 |
|
108 |
def transcribe_audio(self, audio_url, input_language="en"):
|
109 |
"""
|
110 |
+
Integrates ModelsLab Speech-to-Text (Whisper) endpoint.
|
|
|
111 |
"""
|
112 |
if not self.modelslab_api_key:
|
113 |
return "API key not found; cannot transcribe audio."
|
|
|
132 |
|
133 |
def generate_audio(self, text_prompt, init_audio_url=None, voice_id=None, language="english"):
|
134 |
"""
|
135 |
+
Integrates ModelsLab Text-to-Audio (Voice Cloning / TTS).
|
|
|
136 |
"""
|
137 |
if not self.modelslab_api_key:
|
138 |
return "API key not found; cannot generate audio."
|
|
|
146 |
"track_id": None
|
147 |
}
|
148 |
|
|
|
149 |
if init_audio_url:
|
150 |
payload["init_audio"] = init_audio_url
|
151 |
elif voice_id:
|
|
|
161 |
|
162 |
def uncensored_chat_completion(self, prompt):
|
163 |
"""
|
164 |
+
Integrates ModelsLab Uncensored Chat Completions.
|
|
|
|
|
165 |
"""
|
166 |
if not self.modelslab_api_key:
|
167 |
return "API key not found; cannot complete uncensored chat."
|
168 |
|
|
|
169 |
base_url = "https://modelslab.com/api/uncensored-chat/v1/completions"
|
170 |
payload = {
|
171 |
"model": "ModelsLab/Llama-3.1-8b-Uncensored-Dare",
|
172 |
"prompt": prompt,
|
173 |
+
"max_tokens": 50,
|
174 |
+
"temperature": 0.7
|
175 |
}
|
176 |
headers = {
|
177 |
"Content-Type": "application/json",
|
178 |
+
"Authorization": f"Bearer {self.modelslab_api_key}"
|
179 |
}
|
180 |
|
181 |
try:
|
182 |
response = requests.post(base_url, headers=headers, data=json.dumps(payload))
|
183 |
data = response.json()
|
|
|
184 |
if "choices" in data and len(data["choices"]) > 0:
|
185 |
return data["choices"][0].get("text", "")
|
186 |
else:
|
|
|
188 |
except Exception as e:
|
189 |
return f"Error during uncensored chat completion: {e}"
|
190 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
191 |
def generate_response(self, user_input, action_response):
|
192 |
"""
|
193 |
+
Combine user input, system rules, and action response into a final message.
|
194 |
"""
|
195 |
+
return (
|
196 |
+
f"System Prompt: {self.system_prompt}\n"
|
197 |
+
f"Rules: {'; '.join(self.rules)}\n"
|
198 |
+
f"User Input: {user_input}\n"
|
199 |
+
f"System Action: {action_response}"
|
200 |
+
)
|
201 |
+
|
202 |
|
203 |
def run_app():
|
204 |
"""
|
205 |
Example main loop to run the app in a console.
|
206 |
"""
|
207 |
db_path = "central_data.db" # Adjust for your environment
|
208 |
+
starmaint_ai = StarMaintAI(db_path)
|
209 |
|
210 |
+
print("Welcome to StarMaint AI.")
|
211 |
while True:
|
212 |
user_input = input("You: ")
|
213 |
if user_input.lower() in ["exit", "quit"]:
|
214 |
print("Exiting application.")
|
215 |
break
|
216 |
+
response = starmaint_ai.process_user_input(user_input)
|
217 |
print(f"AI: {response}")
|
218 |
|
219 |
+
|
220 |
if __name__ == "__main__":
|
221 |
run_app()
|