Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,25 +1,23 @@
|
|
1 |
import os
|
2 |
import threading
|
3 |
import discord
|
4 |
-
import requests
|
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
|
11 |
load_dotenv()
|
12 |
DISCORD_TOKEN = os.getenv("DISCORD_TOKEN")
|
13 |
-
HF_TOKEN = os.getenv("HF_TOKEN") # Optional: only
|
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
|
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(
|
@@ -31,70 +29,65 @@ else:
|
|
31 |
MODEL_NAME, torch_dtype=torch.float16, device_map="auto"
|
32 |
)
|
33 |
|
34 |
-
#
|
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 |
-
#
|
41 |
response = response.replace("DeepScaleR", "Shiv Yantra AI")
|
42 |
return response
|
43 |
|
44 |
# ==========================
|
45 |
-
# Gradio
|
46 |
# ==========================
|
47 |
def gradio_api(input_text):
|
48 |
return generate_response(input_text)
|
49 |
|
50 |
-
iface = gr.Interface(fn=gradio_api, inputs="text", outputs="text")
|
51 |
|
52 |
def run_gradio():
|
53 |
iface.launch(server_name="0.0.0.0", server_port=7860, share=False)
|
54 |
|
55 |
# ==========================
|
56 |
-
# Discord Bot Setup
|
57 |
# ==========================
|
58 |
intents = discord.Intents.default()
|
59 |
-
intents.message_content = True #
|
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
|
66 |
async def on_ready():
|
67 |
print(f"Logged in as {client.user}")
|
68 |
|
69 |
@client.event
|
70 |
async def on_message(message):
|
|
|
71 |
if message.author == client.user:
|
72 |
-
return
|
73 |
|
74 |
user_input = message.content.strip()
|
75 |
if user_input:
|
76 |
try:
|
77 |
-
|
78 |
-
|
79 |
-
r.raise_for_status()
|
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
|
84 |
await message.channel.send(ai_response)
|
85 |
|
86 |
def run_discord_bot():
|
87 |
client.run(DISCORD_TOKEN)
|
88 |
|
89 |
# ==========================
|
90 |
-
# Start Both Services
|
91 |
# ==========================
|
92 |
if __name__ == "__main__":
|
93 |
-
# Start the Gradio interface in a
|
94 |
threading.Thread(target=run_gradio, daemon=True).start()
|
95 |
-
# Start the Discord bot in a
|
96 |
threading.Thread(target=run_discord_bot, daemon=True).start()
|
97 |
|
98 |
-
# Keep the main thread alive
|
99 |
while True:
|
100 |
pass
|
|
|
1 |
import os
|
2 |
import threading
|
3 |
import discord
|
|
|
4 |
import torch
|
5 |
import gradio as gr
|
6 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
7 |
from dotenv import load_dotenv
|
8 |
|
9 |
+
# Load environment variables (from Hugging Face Secrets and .env if available)
|
10 |
load_dotenv()
|
11 |
DISCORD_TOKEN = os.getenv("DISCORD_TOKEN")
|
12 |
+
HF_TOKEN = os.getenv("HF_TOKEN") # Optional: only if needed for private models
|
13 |
|
14 |
if not DISCORD_TOKEN:
|
15 |
raise ValueError("Discord bot token is missing. Set DISCORD_TOKEN in the environment variables.")
|
16 |
|
17 |
+
# Set the model repository name (public model)
|
18 |
MODEL_NAME = "agentica-org/DeepScaleR-1.5B-Preview"
|
19 |
|
20 |
+
# Load the tokenizer and model. Use token=HF_TOKEN if provided.
|
|
|
21 |
if HF_TOKEN:
|
22 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME, token=HF_TOKEN)
|
23 |
model = AutoModelForCausalLM.from_pretrained(
|
|
|
29 |
MODEL_NAME, torch_dtype=torch.float16, device_map="auto"
|
30 |
)
|
31 |
|
32 |
+
# Function to generate AI responses
|
33 |
def generate_response(prompt):
|
34 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
35 |
inputs = tokenizer(prompt, return_tensors="pt", truncation=True, max_length=1024).to(device)
|
36 |
outputs = model.generate(**inputs, max_new_tokens=200, do_sample=True, top_p=0.9, temperature=0.7)
|
37 |
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
38 |
+
# Ensure the bot always identifies as "Shiv Yantra AI"
|
39 |
response = response.replace("DeepScaleR", "Shiv Yantra AI")
|
40 |
return response
|
41 |
|
42 |
# ==========================
|
43 |
+
# Gradio Interface (optional UI)
|
44 |
# ==========================
|
45 |
def gradio_api(input_text):
|
46 |
return generate_response(input_text)
|
47 |
|
48 |
+
iface = gr.Interface(fn=gradio_api, inputs="text", outputs="text", title="Shiv Yantra AI")
|
49 |
|
50 |
def run_gradio():
|
51 |
iface.launch(server_name="0.0.0.0", server_port=7860, share=False)
|
52 |
|
53 |
# ==========================
|
54 |
+
# Discord Bot Setup (Directly uses local generate_response)
|
55 |
# ==========================
|
56 |
intents = discord.Intents.default()
|
57 |
+
intents.message_content = True # Required for reading message content
|
58 |
client = discord.Client(intents=intents)
|
59 |
|
|
|
|
|
|
|
60 |
@client.event
|
61 |
async def on_ready():
|
62 |
print(f"Logged in as {client.user}")
|
63 |
|
64 |
@client.event
|
65 |
async def on_message(message):
|
66 |
+
# Avoid replying to itself
|
67 |
if message.author == client.user:
|
68 |
+
return
|
69 |
|
70 |
user_input = message.content.strip()
|
71 |
if user_input:
|
72 |
try:
|
73 |
+
# Directly call the local generate_response function
|
74 |
+
ai_response = generate_response(user_input)
|
|
|
|
|
|
|
75 |
except Exception as e:
|
76 |
+
ai_response = "Error processing your request."
|
77 |
await message.channel.send(ai_response)
|
78 |
|
79 |
def run_discord_bot():
|
80 |
client.run(DISCORD_TOKEN)
|
81 |
|
82 |
# ==========================
|
83 |
+
# Start Both Services Concurrently
|
84 |
# ==========================
|
85 |
if __name__ == "__main__":
|
86 |
+
# Start the Gradio interface in a separate thread (optional UI)
|
87 |
threading.Thread(target=run_gradio, daemon=True).start()
|
88 |
+
# Start the Discord bot in a separate thread
|
89 |
threading.Thread(target=run_discord_bot, daemon=True).start()
|
90 |
|
91 |
+
# Keep the main thread alive indefinitely
|
92 |
while True:
|
93 |
pass
|