Spaces:
Runtime error
Runtime error
File size: 2,418 Bytes
7aec585 cb35dc2 1c63c09 b5fc9a6 41f1852 cf25aa7 81721b3 cf25aa7 41f1852 7aec585 cf25aa7 cb35dc2 cf25aa7 cb35dc2 cf25aa7 cb35dc2 cf25aa7 906ce9f cf25aa7 81721b3 cf25aa7 81721b3 d29ae30 b5fc9a6 cb35dc2 b5fc9a6 cf25aa7 7aec585 fdb904d cb35dc2 cf25aa7 1c63c09 cb35dc2 1c63c09 fdb904d cb35dc2 7aec585 cb35dc2 7aec585 cf25aa7 cb35dc2 cf25aa7 cb35dc2 1c63c09 906ce9f cb35dc2 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
import os
import threading
import asyncio
import discord
from dotenv import load_dotenv
from llama_cpp import Llama # Library for GGUF models
# Load environment variables (set these via Hugging Face Secrets)
load_dotenv()
DISCORD_TOKEN = os.getenv("DISCORD_TOKEN")
if not DISCORD_TOKEN:
raise ValueError("Discord bot token is missing. Set DISCORD_TOKEN in the environment variables.")
# Set the local path to your quantized model file.
# Ensure that this file (e.g. DeepScaleR-1.5B-Preview-Q8_0.gguf) is uploaded to your repository.
MODEL_PATH = "./DeepScaleR-1.5B-Preview-Q8_0.gguf"
# Initialize the model with appropriate settings.
# Adjust n_threads and other parameters as needed.
llm = Llama(model_path=MODEL_PATH, n_threads=4)
def generate_response(prompt):
# Generate text using llama-cpp's Llama instance.
# Adjust parameters (max_tokens, temperature, top_p) for speed/quality tradeoffs.
output = llm(prompt=prompt, max_tokens=200, temperature=0.7, top_p=0.9, echo=False)
response = output["text"]
# Optionally enforce your bot identity:
response = response.replace("DeepScaleR", "Shiv Yantra AI")
return response
# ----------------------------
# Discord Bot Setup
# ----------------------------
intents = discord.Intents.default()
intents.message_content = True # Enable reading message content
client = discord.Client(intents=intents)
@client.event
async def on_ready():
print(f"Logged in as {client.user}")
@client.event
async def on_message(message):
# Ignore messages from the bot itself.
if message.author == client.user:
return
user_input = message.content.strip()
if user_input:
try:
# Run the generate_response function in a separate thread so as not to block Discord's event loop.
ai_response = await asyncio.to_thread(generate_response, user_input)
except Exception as e:
print(f"Error during generation: {e}")
ai_response = "Error processing your request."
await message.channel.send(ai_response)
def run_discord_bot():
client.run(DISCORD_TOKEN)
# ----------------------------
# Start the Discord Bot
# ----------------------------
if __name__ == "__main__":
# Start the Discord bot in a separate daemon thread.
threading.Thread(target=run_discord_bot, daemon=True).start()
# Keep the main thread alive.
while True:
pass |