Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -5,32 +5,39 @@ import requests
|
|
5 |
import torch
|
6 |
import gradio as gr
|
7 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
|
|
8 |
|
9 |
-
# Load
|
|
|
10 |
DISCORD_TOKEN = os.getenv("DISCORD_TOKEN")
|
11 |
-
HF_TOKEN = os.getenv("HF_TOKEN") # Optional: only needed if
|
12 |
|
13 |
if not DISCORD_TOKEN:
|
14 |
raise ValueError("Discord bot token is missing. Set DISCORD_TOKEN in the environment variables.")
|
15 |
|
16 |
-
#
|
17 |
MODEL_NAME = "agentica-org/DeepScaleR-1.5B-Preview"
|
18 |
|
19 |
-
# Load the
|
|
|
20 |
if HF_TOKEN:
|
21 |
-
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME,
|
22 |
-
model = AutoModelForCausalLM.from_pretrained(
|
|
|
|
|
23 |
else:
|
24 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
25 |
-
model = AutoModelForCausalLM.from_pretrained(
|
|
|
|
|
26 |
|
27 |
-
# Define a function to generate
|
28 |
def generate_response(prompt):
|
29 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
30 |
inputs = tokenizer(prompt, return_tensors="pt", truncation=True, max_length=1024).to(device)
|
31 |
outputs = model.generate(**inputs, max_new_tokens=200, do_sample=True, top_p=0.9, temperature=0.7)
|
32 |
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
33 |
-
#
|
34 |
response = response.replace("DeepScaleR", "Shiv Yantra AI")
|
35 |
return response
|
36 |
|
@@ -49,10 +56,10 @@ def run_gradio():
|
|
49 |
# Discord Bot Setup
|
50 |
# ==========================
|
51 |
intents = discord.Intents.default()
|
52 |
-
intents.message_content = True #
|
53 |
client = discord.Client(intents=intents)
|
54 |
|
55 |
-
#
|
56 |
GRADIO_API_URL = "http://0.0.0.0:7860/run/predict"
|
57 |
|
58 |
@client.event
|
@@ -73,7 +80,7 @@ async def on_message(message):
|
|
73 |
response_json = r.json()
|
74 |
ai_response = response_json.get("data", ["Sorry, something went wrong."])[0]
|
75 |
except Exception as e:
|
76 |
-
ai_response = "Error
|
77 |
await message.channel.send(ai_response)
|
78 |
|
79 |
def run_discord_bot():
|
@@ -83,11 +90,11 @@ def run_discord_bot():
|
|
83 |
# Start Both Services
|
84 |
# ==========================
|
85 |
if __name__ == "__main__":
|
86 |
-
# Start Gradio in a
|
87 |
threading.Thread(target=run_gradio, daemon=True).start()
|
88 |
-
# Start Discord bot in
|
89 |
threading.Thread(target=run_discord_bot, daemon=True).start()
|
90 |
|
91 |
-
# Keep the main thread alive
|
92 |
while True:
|
93 |
pass
|
|
|
5 |
import torch
|
6 |
import gradio as gr
|
7 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
8 |
+
from dotenv import load_dotenv
|
9 |
|
10 |
+
# Load environment variables from Hugging Face Secrets (and .env if local)
|
11 |
+
load_dotenv()
|
12 |
DISCORD_TOKEN = os.getenv("DISCORD_TOKEN")
|
13 |
+
HF_TOKEN = os.getenv("HF_TOKEN") # Optional: only needed if model repo is private
|
14 |
|
15 |
if not DISCORD_TOKEN:
|
16 |
raise ValueError("Discord bot token is missing. Set DISCORD_TOKEN in the environment variables.")
|
17 |
|
18 |
+
# Set the model repository name. For DeepScaleR-1.5B-Preview, use:
|
19 |
MODEL_NAME = "agentica-org/DeepScaleR-1.5B-Preview"
|
20 |
|
21 |
+
# Load the tokenizer and model.
|
22 |
+
# Using token=HF_TOKEN instead of use_auth_token (per the new deprecation)
|
23 |
if HF_TOKEN:
|
24 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME, token=HF_TOKEN)
|
25 |
+
model = AutoModelForCausalLM.from_pretrained(
|
26 |
+
MODEL_NAME, token=HF_TOKEN, torch_dtype=torch.float16, device_map="auto"
|
27 |
+
)
|
28 |
else:
|
29 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
30 |
+
model = AutoModelForCausalLM.from_pretrained(
|
31 |
+
MODEL_NAME, torch_dtype=torch.float16, device_map="auto"
|
32 |
+
)
|
33 |
|
34 |
+
# Define a function to generate responses with the model
|
35 |
def generate_response(prompt):
|
36 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
37 |
inputs = tokenizer(prompt, return_tensors="pt", truncation=True, max_length=1024).to(device)
|
38 |
outputs = model.generate(**inputs, max_new_tokens=200, do_sample=True, top_p=0.9, temperature=0.7)
|
39 |
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
40 |
+
# Replace any instance of the internal model name with your bot's identity.
|
41 |
response = response.replace("DeepScaleR", "Shiv Yantra AI")
|
42 |
return response
|
43 |
|
|
|
56 |
# Discord Bot Setup
|
57 |
# ==========================
|
58 |
intents = discord.Intents.default()
|
59 |
+
intents.message_content = True # Needed to read message contents
|
60 |
client = discord.Client(intents=intents)
|
61 |
|
62 |
+
# Local endpoint for the Gradio API
|
63 |
GRADIO_API_URL = "http://0.0.0.0:7860/run/predict"
|
64 |
|
65 |
@client.event
|
|
|
80 |
response_json = r.json()
|
81 |
ai_response = response_json.get("data", ["Sorry, something went wrong."])[0]
|
82 |
except Exception as e:
|
83 |
+
ai_response = "Error communicating with the AI API."
|
84 |
await message.channel.send(ai_response)
|
85 |
|
86 |
def run_discord_bot():
|
|
|
90 |
# Start Both Services
|
91 |
# ==========================
|
92 |
if __name__ == "__main__":
|
93 |
+
# Start the Gradio interface in a daemon thread
|
94 |
threading.Thread(target=run_gradio, daemon=True).start()
|
95 |
+
# Start the Discord bot in a daemon thread
|
96 |
threading.Thread(target=run_discord_bot, daemon=True).start()
|
97 |
|
98 |
+
# Keep the main thread alive.
|
99 |
while True:
|
100 |
pass
|