Spaces:
Build error
Build error
Update main.py
Browse files
main.py
CHANGED
|
@@ -1,76 +1,111 @@
|
|
| 1 |
import os
|
| 2 |
-
|
| 3 |
-
from
|
|
|
|
|
|
|
| 4 |
from huggingface_hub import hf_hub_download
|
| 5 |
from llama_cpp import Llama
|
| 6 |
|
| 7 |
-
app = FastAPI()
|
| 8 |
-
|
| 9 |
-
# --- CONFIGURATION ---
|
| 10 |
-
# REPO_ID = "CoolShotSystems/Axiom-3.1-Sovereign"
|
| 11 |
-
# FILENAME = "Meta-Llama-3.1-8B.Q4_K_M.gguf"
|
| 12 |
-
|
| 13 |
# --- CONFIGURATION ---
|
| 14 |
-
#
|
| 15 |
-
# NEW (Fast): "hugging-quants/Llama-3.2-3B-Instruct-Q4_K_M-GGUF"
|
| 16 |
-
|
| 17 |
REPO_ID = "hugging-quants/Llama-3.2-3B-Instruct-Q4_K_M-GGUF"
|
| 18 |
FILENAME = "llama-3.2-3b-instruct-q4_k_m.gguf"
|
| 19 |
|
| 20 |
-
# Global
|
|
|
|
| 21 |
axiom_model = None
|
|
|
|
| 22 |
|
| 23 |
-
|
| 24 |
-
def
|
| 25 |
global axiom_model
|
| 26 |
-
print("📡 DOWNLOADING AXIOM...")
|
| 27 |
try:
|
| 28 |
model_path = hf_hub_download(
|
| 29 |
repo_id=REPO_ID,
|
| 30 |
filename=FILENAME,
|
| 31 |
-
token=os.environ.get("HF_TOKEN")
|
| 32 |
)
|
| 33 |
-
print("🧠 LOADING
|
| 34 |
-
# OPTIMIZATION: n_ctx=512 makes it MUCH faster on Free Tier
|
| 35 |
axiom_model = Llama(
|
| 36 |
model_path=model_path,
|
| 37 |
-
n_ctx=
|
| 38 |
-
n_threads=2,
|
| 39 |
verbose=False
|
| 40 |
)
|
| 41 |
-
print("✅ AXIOM ONLINE
|
| 42 |
except Exception as e:
|
| 43 |
-
print(f"❌
|
| 44 |
|
| 45 |
-
|
| 46 |
-
|
|
|
|
|
|
|
| 47 |
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
@app.post("/v1/chat/completions")
|
| 53 |
-
async def chat(request: ChatRequest):
|
| 54 |
if not axiom_model:
|
| 55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
-
|
| 58 |
-
# We removed <|begin_of_text|> to fix the warning
|
| 59 |
-
# We inject the Identity immediately
|
| 60 |
-
prompt = "<|start_header_id|>system<|end_header_id|>\n\nYou are Axiom 3.1, a Sovereign AI created by Professor Heritage at Cool Shot Systems.<|eot_id|>"
|
| 61 |
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
|
|
|
|
|
|
|
|
|
| 67 |
|
| 68 |
-
#
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
|
| 76 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
+
import asyncio
|
| 3 |
+
from fastapi import FastAPI
|
| 4 |
+
from telegram import Update
|
| 5 |
+
from telegram.ext import Application, CommandHandler, MessageHandler, filters, ContextTypes
|
| 6 |
from huggingface_hub import hf_hub_download
|
| 7 |
from llama_cpp import Llama
|
| 8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
# --- CONFIGURATION ---
|
| 10 |
+
# We use the FAST model (Llama 3.2 3B) for Telegram speed
|
|
|
|
|
|
|
| 11 |
REPO_ID = "hugging-quants/Llama-3.2-3B-Instruct-Q4_K_M-GGUF"
|
| 12 |
FILENAME = "llama-3.2-3b-instruct-q4_k_m.gguf"
|
| 13 |
|
| 14 |
+
# Global Variables
|
| 15 |
+
app = FastAPI()
|
| 16 |
axiom_model = None
|
| 17 |
+
bot_app = None
|
| 18 |
|
| 19 |
+
# --- AXIOM BRAIN SETUP ---
|
| 20 |
+
def load_brain():
|
| 21 |
global axiom_model
|
| 22 |
+
print("📡 DOWNLOADING AXIOM BRAIN...")
|
| 23 |
try:
|
| 24 |
model_path = hf_hub_download(
|
| 25 |
repo_id=REPO_ID,
|
| 26 |
filename=FILENAME,
|
| 27 |
+
token=os.environ.get("HF_TOKEN")
|
| 28 |
)
|
| 29 |
+
print("🧠 LOADING AXIOM INTO RAM...")
|
|
|
|
| 30 |
axiom_model = Llama(
|
| 31 |
model_path=model_path,
|
| 32 |
+
n_ctx=2048, # 2k Context is fine for 3B model
|
| 33 |
+
n_threads=2,
|
| 34 |
verbose=False
|
| 35 |
)
|
| 36 |
+
print("✅ AXIOM ONLINE")
|
| 37 |
except Exception as e:
|
| 38 |
+
print(f"❌ MODEL FAILURE: {e}")
|
| 39 |
|
| 40 |
+
# --- TELEGRAM BOT LOGIC ---
|
| 41 |
+
async def start_command(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
| 42 |
+
"""The /start command"""
|
| 43 |
+
await update.message.reply_text("Axiom 3.1 Sovereign Interface Online.\nCool Shot Systems Proprietary.\n\nSend me a message.")
|
| 44 |
|
| 45 |
+
async def handle_message(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
| 46 |
+
"""Handles normal text messages"""
|
| 47 |
+
user_text = update.message.text
|
| 48 |
+
|
|
|
|
|
|
|
| 49 |
if not axiom_model:
|
| 50 |
+
await update.message.reply_text("⚠️ Axiom is still waking up... please wait 30 seconds.")
|
| 51 |
+
return
|
| 52 |
+
|
| 53 |
+
# Notify user we are thinking (Typing status)
|
| 54 |
+
await context.bot.send_chat_action(chat_id=update.effective_chat.id, action="typing")
|
| 55 |
+
|
| 56 |
+
# 1. Format Prompt with System Identity
|
| 57 |
+
prompt = f"""<|start_header_id|>system<|end_header_id|>
|
| 58 |
+
|
| 59 |
+
You are Axiom 3.1, the Sovereign AI of Cool Shot Systems, created by Professor Heritage.
|
| 60 |
+
You are helpful, strategic, and concise.<|eot_id|><|start_header_id|>user<|end_header_id|>
|
| 61 |
+
|
| 62 |
+
{user_text}<|eot_id|><|start_header_id|>assistant<|end_header_id|>
|
| 63 |
+
|
| 64 |
+
"""
|
| 65 |
+
# 2. Generate
|
| 66 |
+
# We run this in a thread so we don't block the bot
|
| 67 |
+
output = await asyncio.to_thread(
|
| 68 |
+
axiom_model,
|
| 69 |
+
prompt,
|
| 70 |
+
max_tokens=256,
|
| 71 |
+
stop=["<|eot_id|>", "<|end_of_text|>"],
|
| 72 |
+
echo=False
|
| 73 |
+
)
|
| 74 |
|
| 75 |
+
response = output['choices'][0]['text']
|
|
|
|
|
|
|
|
|
|
| 76 |
|
| 77 |
+
# 3. Reply
|
| 78 |
+
await update.message.reply_text(response)
|
| 79 |
+
|
| 80 |
+
# --- STARTUP SEQUENCE ---
|
| 81 |
+
@app.on_event("startup")
|
| 82 |
+
async def startup_event():
|
| 83 |
+
# 1. Load Brain
|
| 84 |
+
load_brain()
|
| 85 |
|
| 86 |
+
# 2. Start Telegram Bot (Background Task)
|
| 87 |
+
token = os.environ.get("TELEGRAM_TOKEN")
|
| 88 |
+
if not token:
|
| 89 |
+
print("❌ NO TELEGRAM TOKEN FOUND!")
|
| 90 |
+
return
|
| 91 |
+
|
| 92 |
+
print("🤖 STARTING TELEGRAM BOT...")
|
| 93 |
+
global bot_app
|
| 94 |
+
bot_app = Application.builder().token(token).build()
|
| 95 |
+
|
| 96 |
+
# Add Handlers
|
| 97 |
+
bot_app.add_handler(CommandHandler("start", start_command))
|
| 98 |
+
bot_app.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, handle_message))
|
| 99 |
+
|
| 100 |
+
# Initialize and Start Polling
|
| 101 |
+
await bot_app.initialize()
|
| 102 |
+
await bot_app.start()
|
| 103 |
|
| 104 |
+
# Run polling in a separate non-blocking task
|
| 105 |
+
asyncio.create_task(bot_app.updater.start_polling())
|
| 106 |
+
print("✅ TELEGRAM BOT LISTENING")
|
| 107 |
+
|
| 108 |
+
# --- DUMMY SERVER (Keeps Space Alive) ---
|
| 109 |
+
@app.get("/")
|
| 110 |
+
def home():
|
| 111 |
+
return {"status": "Axiom Telegram Bot Running"}
|