Hardik5456 commited on
Commit
4a56061
·
verified ·
1 Parent(s): 731adee

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -17
app.py CHANGED
@@ -3,37 +3,44 @@ import threading
3
  import asyncio
4
  import discord
5
  from dotenv import load_dotenv
6
- from llama_cpp import Llama # Library for GGUF models
 
7
 
8
- # Load environment variables (set these via Hugging Face Secrets)
9
  load_dotenv()
10
  DISCORD_TOKEN = os.getenv("DISCORD_TOKEN")
11
 
12
  if not DISCORD_TOKEN:
13
  raise ValueError("Discord bot token is missing. Set DISCORD_TOKEN in the environment variables.")
14
 
15
- # Set the local path to your quantized model file.
16
- # Ensure that this file (e.g. DeepScaleR-1.5B-Preview-Q8_0.gguf) is uploaded to your repository.
17
- MODEL_PATH = "./DeepScaleR-1.5B-Preview-Q8_0.gguf"
 
18
 
19
- # Initialize the model with appropriate settings.
20
- # Adjust n_threads and other parameters as needed.
 
 
 
 
 
 
21
  llm = Llama(model_path=MODEL_PATH, n_threads=4)
22
 
 
23
  def generate_response(prompt):
24
- # Generate text using llama-cpp's Llama instance.
25
- # Adjust parameters (max_tokens, temperature, top_p) for speed/quality tradeoffs.
26
  output = llm(prompt=prompt, max_tokens=200, temperature=0.7, top_p=0.9, echo=False)
27
  response = output["text"]
28
- # Optionally enforce your bot identity:
29
  response = response.replace("DeepScaleR", "Shiv Yantra AI")
30
  return response
31
 
32
- # ----------------------------
33
  # Discord Bot Setup
34
- # ----------------------------
35
  intents = discord.Intents.default()
36
- intents.message_content = True # Enable reading message content
37
  client = discord.Client(intents=intents)
38
 
39
  @client.event
@@ -49,7 +56,7 @@ async def on_message(message):
49
  user_input = message.content.strip()
50
  if user_input:
51
  try:
52
- # Run the generate_response function in a separate thread so as not to block Discord's event loop.
53
  ai_response = await asyncio.to_thread(generate_response, user_input)
54
  except Exception as e:
55
  print(f"Error during generation: {e}")
@@ -59,11 +66,11 @@ async def on_message(message):
59
  def run_discord_bot():
60
  client.run(DISCORD_TOKEN)
61
 
62
- # ----------------------------
63
  # Start the Discord Bot
64
- # ----------------------------
65
  if __name__ == "__main__":
66
- # Start the Discord bot in a separate daemon thread.
67
  threading.Thread(target=run_discord_bot, daemon=True).start()
68
 
69
  # Keep the main thread alive.
 
3
  import asyncio
4
  import discord
5
  from dotenv import load_dotenv
6
+ from huggingface_hub import hf_hub_download
7
+ from llama_cpp import Llama
8
 
9
+ # Load environment variables (from Hugging Face Secrets and .env if available)
10
  load_dotenv()
11
  DISCORD_TOKEN = os.getenv("DISCORD_TOKEN")
12
 
13
  if not DISCORD_TOKEN:
14
  raise ValueError("Discord bot token is missing. Set DISCORD_TOKEN in the environment variables.")
15
 
16
+ # Set model details
17
+ MODEL_REPO = "bartowski/agentica-org_DeepScaleR-1.5B-Preview-GGUF"
18
+ MODEL_FILENAME = "agentica-org_DeepScaleR-1.5B-Preview-Q8_0.gguf"
19
+ MODEL_PATH = f"./{MODEL_FILENAME}"
20
 
21
+ # If the model file doesn't exist locally, download it.
22
+ if not os.path.exists(MODEL_PATH):
23
+ print("Model file not found, downloading...")
24
+ MODEL_PATH = hf_hub_download(repo_id=MODEL_REPO, filename=MODEL_FILENAME)
25
+ print(f"Model downloaded to {MODEL_PATH}")
26
+
27
+ # Initialize the model using llama-cpp-python.
28
+ # Adjust n_threads if needed.
29
  llm = Llama(model_path=MODEL_PATH, n_threads=4)
30
 
31
+ # Define a function to generate a response.
32
  def generate_response(prompt):
 
 
33
  output = llm(prompt=prompt, max_tokens=200, temperature=0.7, top_p=0.9, echo=False)
34
  response = output["text"]
35
+ # Replace the internal model name with your bot's identity.
36
  response = response.replace("DeepScaleR", "Shiv Yantra AI")
37
  return response
38
 
39
+ # ---------------------------
40
  # Discord Bot Setup
41
+ # ---------------------------
42
  intents = discord.Intents.default()
43
+ intents.message_content = True # Enable access to message content
44
  client = discord.Client(intents=intents)
45
 
46
  @client.event
 
56
  user_input = message.content.strip()
57
  if user_input:
58
  try:
59
+ # Run generate_response in a separate thread so as not to block the Discord loop.
60
  ai_response = await asyncio.to_thread(generate_response, user_input)
61
  except Exception as e:
62
  print(f"Error during generation: {e}")
 
66
  def run_discord_bot():
67
  client.run(DISCORD_TOKEN)
68
 
69
+ # ---------------------------
70
  # Start the Discord Bot
71
+ # ---------------------------
72
  if __name__ == "__main__":
73
+ # Run the Discord bot on a separate thread.
74
  threading.Thread(target=run_discord_bot, daemon=True).start()
75
 
76
  # Keep the main thread alive.