Hardik5456's picture
Update app.py
4a56061 verified
raw
history blame
2.58 kB
import os
import threading
import asyncio
import discord
from dotenv import load_dotenv
from huggingface_hub import hf_hub_download
from llama_cpp import Llama
# Load environment variables (from Hugging Face Secrets and .env if available)
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 model details
MODEL_REPO = "bartowski/agentica-org_DeepScaleR-1.5B-Preview-GGUF"
MODEL_FILENAME = "agentica-org_DeepScaleR-1.5B-Preview-Q8_0.gguf"
MODEL_PATH = f"./{MODEL_FILENAME}"
# If the model file doesn't exist locally, download it.
if not os.path.exists(MODEL_PATH):
print("Model file not found, downloading...")
MODEL_PATH = hf_hub_download(repo_id=MODEL_REPO, filename=MODEL_FILENAME)
print(f"Model downloaded to {MODEL_PATH}")
# Initialize the model using llama-cpp-python.
# Adjust n_threads if needed.
llm = Llama(model_path=MODEL_PATH, n_threads=4)
# Define a function to generate a response.
def generate_response(prompt):
output = llm(prompt=prompt, max_tokens=200, temperature=0.7, top_p=0.9, echo=False)
response = output["text"]
# Replace the internal model name with your bot's identity.
response = response.replace("DeepScaleR", "Shiv Yantra AI")
return response
# ---------------------------
# Discord Bot Setup
# ---------------------------
intents = discord.Intents.default()
intents.message_content = True # Enable access to 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 generate_response in a separate thread so as not to block the Discord 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__":
# Run the Discord bot on a separate thread.
threading.Thread(target=run_discord_bot, daemon=True).start()
# Keep the main thread alive.
while True:
pass