Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -7,14 +7,14 @@ from dotenv import load_dotenv
|
|
7 |
from huggingface_hub import hf_hub_download
|
8 |
from llama_cpp import Llama
|
9 |
|
10 |
-
# Load environment variables from Hugging Face Secrets and .env
|
11 |
load_dotenv()
|
12 |
DISCORD_TOKEN = os.getenv("DISCORD_TOKEN")
|
13 |
|
14 |
if not DISCORD_TOKEN:
|
15 |
raise ValueError("Discord bot token is missing. Set DISCORD_TOKEN in the environment variables.")
|
16 |
|
17 |
-
# Model details: Using the Q6_K_L variant
|
18 |
MODEL_REPO = "bartowski/agentica-org_DeepScaleR-1.5B-Preview-GGUF"
|
19 |
MODEL_FILENAME = "agentica-org_DeepScaleR-1.5B-Preview-Q6_K_L.gguf"
|
20 |
MODEL_PATH = f"./{MODEL_FILENAME}"
|
@@ -28,7 +28,6 @@ else:
|
|
28 |
print(f"Model found locally at {MODEL_PATH}")
|
29 |
|
30 |
# Initialize the model using llama-cpp-python.
|
31 |
-
# Adjust n_threads based on your available CPU cores.
|
32 |
print("Initializing model...")
|
33 |
llm = Llama(model_path=MODEL_PATH, n_threads=4)
|
34 |
print("Model initialization complete.")
|
@@ -36,10 +35,10 @@ print("Model initialization complete.")
|
|
36 |
# Define a function to generate responses using the model.
|
37 |
def generate_response(prompt):
|
38 |
try:
|
39 |
-
#
|
40 |
-
output = llm(prompt=prompt, max_tokens=
|
41 |
response = output["text"]
|
42 |
-
#
|
43 |
response = response.replace("DeepScaleR", "Shiv Yantra AI")
|
44 |
return response
|
45 |
except Exception as e:
|
@@ -50,7 +49,7 @@ def generate_response(prompt):
|
|
50 |
# Discord Bot Setup
|
51 |
# ----------------------------
|
52 |
intents = discord.Intents.default()
|
53 |
-
intents.message_content = True # Required to read message
|
54 |
client = discord.Client(intents=intents)
|
55 |
|
56 |
@client.event
|
@@ -59,13 +58,12 @@ async def on_ready():
|
|
59 |
|
60 |
@client.event
|
61 |
async def on_message(message):
|
62 |
-
# Avoid replying to itself.
|
63 |
if message.author == client.user:
|
64 |
-
return
|
65 |
user_input = message.content.strip()
|
66 |
if user_input:
|
67 |
try:
|
68 |
-
# Run
|
69 |
ai_response = await asyncio.to_thread(generate_response, user_input)
|
70 |
except Exception as e:
|
71 |
print(f"Error during generation in on_message: {e}")
|
@@ -82,6 +80,6 @@ if __name__ == "__main__":
|
|
82 |
print("Starting Discord bot...")
|
83 |
threading.Thread(target=run_discord_bot, daemon=True).start()
|
84 |
print("Discord bot started. Keeping main thread alive.")
|
85 |
-
# Use a sleep loop
|
86 |
while True:
|
87 |
time.sleep(60)
|
|
|
7 |
from huggingface_hub import hf_hub_download
|
8 |
from llama_cpp import Llama
|
9 |
|
10 |
+
# Load environment variables (from Hugging Face Secrets and .env if available)
|
11 |
load_dotenv()
|
12 |
DISCORD_TOKEN = os.getenv("DISCORD_TOKEN")
|
13 |
|
14 |
if not DISCORD_TOKEN:
|
15 |
raise ValueError("Discord bot token is missing. Set DISCORD_TOKEN in the environment variables.")
|
16 |
|
17 |
+
# Model details: Using the Q6_K_L variant from bartowski’s GGUF collection.
|
18 |
MODEL_REPO = "bartowski/agentica-org_DeepScaleR-1.5B-Preview-GGUF"
|
19 |
MODEL_FILENAME = "agentica-org_DeepScaleR-1.5B-Preview-Q6_K_L.gguf"
|
20 |
MODEL_PATH = f"./{MODEL_FILENAME}"
|
|
|
28 |
print(f"Model found locally at {MODEL_PATH}")
|
29 |
|
30 |
# Initialize the model using llama-cpp-python.
|
|
|
31 |
print("Initializing model...")
|
32 |
llm = Llama(model_path=MODEL_PATH, n_threads=4)
|
33 |
print("Model initialization complete.")
|
|
|
35 |
# Define a function to generate responses using the model.
|
36 |
def generate_response(prompt):
|
37 |
try:
|
38 |
+
# Reduce max_tokens to 50 to speed up generation.
|
39 |
+
output = llm(prompt=prompt, max_tokens=50, temperature=0.7, top_p=0.9, echo=False)
|
40 |
response = output["text"]
|
41 |
+
# Enforce bot identity: replace internal model name with "Shiv Yantra AI"
|
42 |
response = response.replace("DeepScaleR", "Shiv Yantra AI")
|
43 |
return response
|
44 |
except Exception as e:
|
|
|
49 |
# Discord Bot Setup
|
50 |
# ----------------------------
|
51 |
intents = discord.Intents.default()
|
52 |
+
intents.message_content = True # Required to read message content
|
53 |
client = discord.Client(intents=intents)
|
54 |
|
55 |
@client.event
|
|
|
58 |
|
59 |
@client.event
|
60 |
async def on_message(message):
|
|
|
61 |
if message.author == client.user:
|
62 |
+
return # Skip messages from the bot itself
|
63 |
user_input = message.content.strip()
|
64 |
if user_input:
|
65 |
try:
|
66 |
+
# Run generate_response in a separate thread to avoid blocking.
|
67 |
ai_response = await asyncio.to_thread(generate_response, user_input)
|
68 |
except Exception as e:
|
69 |
print(f"Error during generation in on_message: {e}")
|
|
|
80 |
print("Starting Discord bot...")
|
81 |
threading.Thread(target=run_discord_bot, daemon=True).start()
|
82 |
print("Discord bot started. Keeping main thread alive.")
|
83 |
+
# Use a sleep loop instead of busy-waiting.
|
84 |
while True:
|
85 |
time.sleep(60)
|