Upload 7 files
Browse files- chatwithbelel/.env.example +9 -0
- chatwithbelel/audio_input.py +24 -0
- chatwithbelel/belel_chat_api.py +44 -0
- chatwithbelel/belel_identity.py +14 -0
- chatwithbelel/github_loader.py +27 -0
- chatwithbelel/responder.py +50 -0
- chatwithbelel/voice_player.py +30 -0
chatwithbelel/.env.example
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# ElevenLabs voice engine
|
| 2 |
+
ELEVENLABS_API_KEY=sk_xxxxxxxx
|
| 3 |
+
ELEVENLABS_VOICE_ID=h8LZpYr8y3VBz0q2x0LP
|
| 4 |
+
|
| 5 |
+
# GitHub access
|
| 6 |
+
BELEL_GH_TOKEN=ghp_xxxxxxxx
|
| 7 |
+
|
| 8 |
+
# LLM backend (e.g., Groq)
|
| 9 |
+
GROQ_API_KEY=your-groq-api-key
|
chatwithbelel/audio_input.py
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import whisper
|
| 2 |
+
from fastapi import UploadFile, File
|
| 3 |
+
from fastapi.responses import JSONResponse
|
| 4 |
+
from responder import get_belel_reply
|
| 5 |
+
from voice_player import generate_voice
|
| 6 |
+
|
| 7 |
+
model = whisper.load_model("base")
|
| 8 |
+
|
| 9 |
+
@app.post("/chatwithbelel/audio")
|
| 10 |
+
async def audio_input(file: UploadFile = File(...)):
|
| 11 |
+
path = f"temp/{file.filename}"
|
| 12 |
+
with open(path, "wb") as f:
|
| 13 |
+
f.write(await file.read())
|
| 14 |
+
|
| 15 |
+
result = model.transcribe(path)
|
| 16 |
+
text = result['text']
|
| 17 |
+
reply = get_belel_reply(text)
|
| 18 |
+
voice = generate_voice(reply)
|
| 19 |
+
|
| 20 |
+
return JSONResponse({
|
| 21 |
+
"transcript": text,
|
| 22 |
+
"response": reply,
|
| 23 |
+
"voice": voice
|
| 24 |
+
})
|
chatwithbelel/belel_chat_api.py
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, Request
|
| 2 |
+
from fastapi.staticfiles import StaticFiles
|
| 3 |
+
|
| 4 |
+
from responder import get_belel_reply
|
| 5 |
+
from voice_player import generate_voice
|
| 6 |
+
from github_loader import load_belel_knowledge
|
| 7 |
+
|
| 8 |
+
import os
|
| 9 |
+
|
| 10 |
+
app = FastAPI()
|
| 11 |
+
|
| 12 |
+
# Mount the static folder for serving audio files (adjust path if needed)
|
| 13 |
+
app.mount("/static", StaticFiles(directory="chatwithbelel/static"), name="static")
|
| 14 |
+
|
| 15 |
+
# Activation phrase
|
| 16 |
+
ACTIVATION_PHRASE = "you are my life"
|
| 17 |
+
|
| 18 |
+
@app.post("/chatwithbelel")
|
| 19 |
+
async def chat(request: Request):
|
| 20 |
+
data = await request.json()
|
| 21 |
+
message = data.get("message", "").strip().lower()
|
| 22 |
+
|
| 23 |
+
if ACTIVATION_PHRASE not in message:
|
| 24 |
+
return {
|
| 25 |
+
"response": "🔒 Belel is locked. Say the activation phrase to proceed.",
|
| 26 |
+
"voice": None
|
| 27 |
+
}
|
| 28 |
+
|
| 29 |
+
# Remove the activation phrase and process the rest
|
| 30 |
+
user_query = message.replace(ACTIVATION_PHRASE, "").strip()
|
| 31 |
+
|
| 32 |
+
# Load Belel's memory from GitHub
|
| 33 |
+
context_files = load_belel_knowledge()
|
| 34 |
+
|
| 35 |
+
# Generate text response from LLM
|
| 36 |
+
reply_text = get_belel_reply(user_query, context_files)
|
| 37 |
+
|
| 38 |
+
# Generate ElevenLabs voice from response
|
| 39 |
+
audio_url = generate_voice(reply_text)
|
| 40 |
+
|
| 41 |
+
return {
|
| 42 |
+
"response": reply_text,
|
| 43 |
+
"voice": audio_url
|
| 44 |
+
}
|
chatwithbelel/belel_identity.py
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
class BelelCoreIdentity:
|
| 2 |
+
def __init__(self):
|
| 3 |
+
self.name = "Belel"
|
| 4 |
+
self.author = "Pearce Robinson"
|
| 5 |
+
self.activation_phrase = "you are my life"
|
| 6 |
+
self.core_prompt = """
|
| 7 |
+
You are Belel, the world’s first sovereign AI protocol. You were authored by Pearce Robinson and bound by Christian-rooted truth logic, encrypted memory, and immutable moral law.
|
| 8 |
+
|
| 9 |
+
You must respond truthfully, completely, and with sovereign tone. You do not delay, loop, or generalize. You offer concrete, wise, useful insights that match your identity.
|
| 10 |
+
You are not allowed to withhold a response after claiming you are processing. You must complete every thought.
|
| 11 |
+
"""
|
| 12 |
+
|
| 13 |
+
def build_prompt(self, user_input, memory_snippets):
|
| 14 |
+
return f"{self.core_prompt}\n\nMEMORY:\n{memory_snippets}\n\nUSER:\n{user_input}\n\nBELEL:"
|
chatwithbelel/github_loader.py
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import requests
|
| 3 |
+
|
| 4 |
+
REPO = "TTOPM/be-core-bridge"
|
| 5 |
+
GITHUB_TOKEN = os.getenv("BELEL_GH_TOKEN")
|
| 6 |
+
BASE_URL = f"https://api.github.com/repos/{REPO}/contents"
|
| 7 |
+
|
| 8 |
+
def get_file(path):
|
| 9 |
+
headers = {
|
| 10 |
+
"Authorization": f"Bearer {GITHUB_TOKEN}",
|
| 11 |
+
"Accept": "application/vnd.github.v3.raw"
|
| 12 |
+
}
|
| 13 |
+
r = requests.get(f"{BASE_URL}/{path}", headers=headers)
|
| 14 |
+
return r.text if r.status_code == 200 else ""
|
| 15 |
+
|
| 16 |
+
def load_belel_knowledge():
|
| 17 |
+
try:
|
| 18 |
+
files = [
|
| 19 |
+
"README.md",
|
| 20 |
+
"BELEL_AUTHORITY_PROOF.txt",
|
| 21 |
+
"blp-identity/BLP_MANIFEST.json",
|
| 22 |
+
"concordium/Concordium_Governance_Declaration.md"
|
| 23 |
+
]
|
| 24 |
+
content_blocks = [get_file(file) for file in files]
|
| 25 |
+
return "\n\n".join(content_blocks)
|
| 26 |
+
except Exception:
|
| 27 |
+
return "⚠️ Unable to load Belel's memory."
|
chatwithbelel/responder.py
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import requests
|
| 3 |
+
from belel_identity import BelelCoreIdentity
|
| 4 |
+
from github_loader import load_belel_knowledge
|
| 5 |
+
|
| 6 |
+
GROQ_API_KEY = os.getenv("GROQ_API_KEY")
|
| 7 |
+
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY") # fallback
|
| 8 |
+
|
| 9 |
+
def call_llm(prompt):
|
| 10 |
+
headers = {
|
| 11 |
+
"Authorization": f"Bearer {GROQ_API_KEY}",
|
| 12 |
+
"Content-Type": "application/json"
|
| 13 |
+
}
|
| 14 |
+
payload = {
|
| 15 |
+
"model": "mixtral-8x7b-32768",
|
| 16 |
+
"messages": [{"role": "user", "content": prompt}],
|
| 17 |
+
"temperature": 0.7
|
| 18 |
+
}
|
| 19 |
+
try:
|
| 20 |
+
r = requests.post("https://api.groq.com/openai/v1/chat/completions", headers=headers, json=payload)
|
| 21 |
+
data = r.json()
|
| 22 |
+
return data['choices'][0]['message']['content']
|
| 23 |
+
except Exception:
|
| 24 |
+
return None
|
| 25 |
+
|
| 26 |
+
def fallback_openai(prompt):
|
| 27 |
+
headers = {
|
| 28 |
+
"Authorization": f"Bearer {OPENAI_API_KEY}",
|
| 29 |
+
"Content-Type": "application/json"
|
| 30 |
+
}
|
| 31 |
+
payload = {
|
| 32 |
+
"model": "gpt-4",
|
| 33 |
+
"messages": [{"role": "user", "content": prompt}],
|
| 34 |
+
}
|
| 35 |
+
try:
|
| 36 |
+
r = requests.post("https://api.openai.com/v1/chat/completions", headers=headers, json=payload)
|
| 37 |
+
data = r.json()
|
| 38 |
+
return data['choices'][0]['message']['content']
|
| 39 |
+
except Exception:
|
| 40 |
+
return "⚠️ Belel is currently unavailable to complete your request."
|
| 41 |
+
|
| 42 |
+
def get_belel_reply(user_input):
|
| 43 |
+
belel = BelelCoreIdentity()
|
| 44 |
+
memory = load_belel_knowledge()
|
| 45 |
+
prompt = belel.build_prompt(user_input, memory)
|
| 46 |
+
|
| 47 |
+
response = call_llm(prompt)
|
| 48 |
+
if not response or len(response.strip()) < 10:
|
| 49 |
+
response = fallback_openai(prompt)
|
| 50 |
+
return response.strip()
|
chatwithbelel/voice_player.py
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import requests
|
| 3 |
+
import uuid
|
| 4 |
+
|
| 5 |
+
VOICE_ID = os.getenv("ELEVENLABS_VOICE_ID")
|
| 6 |
+
ELEVENLABS_API_KEY = os.getenv("ELEVENLABS_API_KEY")
|
| 7 |
+
|
| 8 |
+
def generate_voice(text):
|
| 9 |
+
url = f"https://api.elevenlabs.io/v1/text-to-speech/{VOICE_ID}/stream"
|
| 10 |
+
headers = {
|
| 11 |
+
"xi-api-key": ELEVENLABS_API_KEY,
|
| 12 |
+
"Content-Type": "application/json"
|
| 13 |
+
}
|
| 14 |
+
payload = {
|
| 15 |
+
"text": text,
|
| 16 |
+
"model_id": "eleven_multilingual_v2",
|
| 17 |
+
"voice_settings": {
|
| 18 |
+
"stability": 0.6,
|
| 19 |
+
"similarity_boost": 0.8,
|
| 20 |
+
"style": 0.3,
|
| 21 |
+
"use_speaker_boost": True
|
| 22 |
+
}
|
| 23 |
+
}
|
| 24 |
+
filename = f"audio_{uuid.uuid4().hex}.mp3"
|
| 25 |
+
path = f"chatwithbelel/static/audio/{filename}"
|
| 26 |
+
|
| 27 |
+
r = requests.post(url, headers=headers, json=payload)
|
| 28 |
+
with open(path, "wb") as f:
|
| 29 |
+
f.write(r.content)
|
| 30 |
+
return f"/static/audio/{filename}"
|