Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -7,19 +7,19 @@ 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
|
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:
|
18 |
MODEL_REPO = "bartowski/agentica-org_DeepScaleR-1.5B-Preview-GGUF"
|
19 |
-
MODEL_FILENAME = "agentica-org_DeepScaleR-1.5B-Preview-
|
20 |
MODEL_PATH = f"./{MODEL_FILENAME}"
|
21 |
|
22 |
-
# Download the model file if it
|
23 |
if not os.path.exists(MODEL_PATH):
|
24 |
print("Model file not found locally. Downloading now...")
|
25 |
MODEL_PATH = hf_hub_download(repo_id=MODEL_REPO, filename=MODEL_FILENAME)
|
@@ -28,18 +28,18 @@ 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 available CPU cores.
|
32 |
print("Initializing model...")
|
33 |
llm = Llama(model_path=MODEL_PATH, n_threads=4)
|
34 |
print("Model initialization complete.")
|
35 |
|
36 |
-
# Define a function to generate
|
37 |
def generate_response(prompt):
|
38 |
try:
|
39 |
-
#
|
40 |
output = llm(prompt=prompt, max_tokens=200, temperature=0.7, top_p=0.9, echo=False)
|
41 |
response = output["text"]
|
42 |
-
#
|
43 |
response = response.replace("DeepScaleR", "Shiv Yantra AI")
|
44 |
return response
|
45 |
except Exception as e:
|
@@ -50,7 +50,7 @@ def generate_response(prompt):
|
|
50 |
# Discord Bot Setup
|
51 |
# ----------------------------
|
52 |
intents = discord.Intents.default()
|
53 |
-
intents.message_content = True #
|
54 |
client = discord.Client(intents=intents)
|
55 |
|
56 |
@client.event
|
@@ -59,13 +59,13 @@ async def on_ready():
|
|
59 |
|
60 |
@client.event
|
61 |
async def on_message(message):
|
62 |
-
#
|
63 |
if message.author == client.user:
|
64 |
return
|
65 |
user_input = message.content.strip()
|
66 |
if user_input:
|
67 |
try:
|
68 |
-
# Run the generate_response in a separate thread
|
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,5 +82,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 |
while True:
|
86 |
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 (recommended) 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}"
|
21 |
|
22 |
+
# Download the model file if it doesn't exist locally.
|
23 |
if not os.path.exists(MODEL_PATH):
|
24 |
print("Model file not found locally. Downloading now...")
|
25 |
MODEL_PATH = hf_hub_download(repo_id=MODEL_REPO, filename=MODEL_FILENAME)
|
|
|
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.")
|
35 |
|
36 |
+
# Define a function to generate responses using the model.
|
37 |
def generate_response(prompt):
|
38 |
try:
|
39 |
+
# Call the model: adjust max_tokens, temperature, and top_p as needed.
|
40 |
output = llm(prompt=prompt, max_tokens=200, temperature=0.7, top_p=0.9, echo=False)
|
41 |
response = output["text"]
|
42 |
+
# Replace any instance of the internal model name with your bot's identity.
|
43 |
response = response.replace("DeepScaleR", "Shiv Yantra AI")
|
44 |
return response
|
45 |
except Exception as e:
|
|
|
50 |
# Discord Bot Setup
|
51 |
# ----------------------------
|
52 |
intents = discord.Intents.default()
|
53 |
+
intents.message_content = True # Required to read message contents
|
54 |
client = discord.Client(intents=intents)
|
55 |
|
56 |
@client.event
|
|
|
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 the synchronous generate_response function in a separate thread.
|
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 |
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 to avoid busy-waiting.
|
86 |
while True:
|
87 |
time.sleep(60)
|