108 lines
3.1 KiB
Python
108 lines
3.1 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
BILBO - SNAPPY MODE
|
|
Queue + workers = speed. Real Ollama only.
|
|
"""
|
|
|
|
import os
|
|
import time
|
|
import queue
|
|
import threading
|
|
import requests
|
|
from datetime import datetime
|
|
|
|
os.environ['HOME'] = '/root/wizards/bilbobagginshire'
|
|
|
|
TOKEN = "8602794341:AAFwfcg-YV6a1icrh0KYylYmPZLnkfkfV9k"
|
|
API = f"https://api.telegram.org/bot{TOKEN}"
|
|
OLLAMA = "http://localhost:11434"
|
|
|
|
# QUEUE = SPEED
|
|
q = queue.Queue()
|
|
|
|
print("🔥 BILBO - SNAPPY MODE")
|
|
print("Queue + workers. Real Ollama. No canned.")
|
|
|
|
def worker():
|
|
"""Background worker - never blocks main thread"""
|
|
while True:
|
|
try:
|
|
chat_id, text, msg_id = q.get(timeout=1)
|
|
|
|
# REAL OLLAMA ONLY
|
|
try:
|
|
r = requests.post(
|
|
f"{OLLAMA}/api/generate",
|
|
json={
|
|
"model": "qwen2.5:1.5b",
|
|
"prompt": f"You are Bilbo Baggins. Be polite but brief.\n\nUser: {text}\n\nBilbo:",
|
|
"stream": False,
|
|
"options": {"temperature": 0.8, "num_predict": 100}
|
|
},
|
|
timeout=60
|
|
)
|
|
|
|
if r.status_code == 200:
|
|
resp = r.json()["response"].strip()
|
|
# SEND REAL RESPONSE
|
|
requests.post(f"{API}/sendMessage", json={
|
|
"chat_id": chat_id,
|
|
"text": resp[:4096],
|
|
"reply_to_message_id": msg_id
|
|
}, timeout=10)
|
|
print(f"✓ {len(resp)} chars")
|
|
else:
|
|
print(f"✗ Status {r.status_code}")
|
|
|
|
except Exception as e:
|
|
print(f"✗ Ollama fail: {str(e)[:30]}")
|
|
# NO CANNED RESPONSE - just log and continue
|
|
|
|
q.task_done()
|
|
|
|
except queue.Empty:
|
|
continue
|
|
except Exception as e:
|
|
print(f"Worker err: {e}")
|
|
|
|
# Start workers
|
|
for i in range(2):
|
|
threading.Thread(target=worker, daemon=True).start()
|
|
|
|
# Main loop - just queue, never wait
|
|
offset = None
|
|
while True:
|
|
try:
|
|
r = requests.post(f"{API}/getUpdates",
|
|
json={"offset": offset, "limit": 10},
|
|
timeout=30)
|
|
updates = r.json().get("result", [])
|
|
|
|
for update in updates:
|
|
offset = update["update_id"] + 1
|
|
|
|
if "message" not in update:
|
|
continue
|
|
|
|
msg = update["message"]
|
|
chat_id = msg["chat"]["id"]
|
|
text = msg.get("text", "")
|
|
msg_id = msg["message_id"]
|
|
|
|
if not text:
|
|
continue
|
|
|
|
print(f"[{datetime.now().strftime('%H:%M:%S')}] {text[:40]}")
|
|
|
|
# QUEUE IT - DON'T WAIT
|
|
q.put((chat_id, text, msg_id))
|
|
print(f" → queued ({q.qsize()})")
|
|
|
|
time.sleep(0.1) # Fast poll
|
|
|
|
except KeyboardInterrupt:
|
|
break
|
|
except Exception as e:
|
|
print(f"Err: {e}")
|
|
time.sleep(1)
|