Hardik5456 commited on
Commit
1c63c09
·
verified ·
1 Parent(s): 6239d55

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -18
app.py CHANGED
@@ -1,23 +1,24 @@
1
  import os
2
  import threading
 
3
  import discord
4
  import torch
5
  from transformers import AutoModelForCausalLM, AutoTokenizer
6
  from dotenv import load_dotenv
7
 
8
- # Load environment variables from Hugging Face Secrets and .env (if available)
9
  load_dotenv()
10
  DISCORD_TOKEN = os.getenv("DISCORD_TOKEN")
11
- HF_TOKEN = os.getenv("HF_TOKEN") # Optional: only needed if your model repository is private
12
 
13
  if not DISCORD_TOKEN:
14
  raise ValueError("Discord bot token is missing. Set DISCORD_TOKEN in the environment variables.")
15
 
16
- # Specify the model repository name. For DeepScaleR-1.5B-Preview use the official repo:
 
17
  MODEL_NAME = "agentica-org/DeepScaleR-1.5B-Preview"
18
 
19
  # Load the tokenizer and model.
20
- # If HF_TOKEN is provided, use it for authentication.
21
  if HF_TOKEN:
22
  tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME, token=HF_TOKEN)
23
  model = AutoModelForCausalLM.from_pretrained(
@@ -29,21 +30,27 @@ else:
29
  MODEL_NAME, torch_dtype=torch.float16, device_map="auto"
30
  )
31
 
32
- # Define a function to generate responses using the model.
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
- # Replace any instance of "DeepScaleR" with "Shiv Yantra AI" to enforce the bot's identity.
39
  response = response.replace("DeepScaleR", "Shiv Yantra AI")
40
  return response
41
 
42
- # ==========================
43
  # Discord Bot Setup
44
- # ==========================
45
  intents = discord.Intents.default()
46
- intents.message_content = True # Required for reading message content
47
  client = discord.Client(intents=intents)
48
 
49
  @client.event
@@ -59,23 +66,37 @@ async def on_message(message):
59
  user_input = message.content.strip()
60
  if user_input:
61
  try:
62
- # Directly call the local function instead of making an HTTP request.
63
- ai_response = generate_response(user_input)
64
  except Exception as e:
65
- print(f"Error generating response: {e}")
66
  ai_response = "Error processing your request."
67
  await message.channel.send(ai_response)
68
 
69
  def run_discord_bot():
70
  client.run(DISCORD_TOKEN)
71
 
72
- # ==========================
73
- # Start the Discord Bot
74
- # ==========================
 
 
 
 
 
 
 
 
 
 
 
 
75
  if __name__ == "__main__":
76
- # Run the Discord bot on a separate thread.
 
 
77
  threading.Thread(target=run_discord_bot, daemon=True).start()
78
-
79
  # Keep the main thread alive.
80
  while True:
81
  pass
 
1
  import os
2
  import threading
3
+ import asyncio
4
  import discord
5
  import torch
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 needed if your model repo is private
13
 
14
  if not DISCORD_TOKEN:
15
  raise ValueError("Discord bot token is missing. Set DISCORD_TOKEN in the environment variables.")
16
 
17
+ # Specify the model repository name.
18
+ # For DeepScaleR-1.5B-Preview, we use the official repository:
19
  MODEL_NAME = "agentica-org/DeepScaleR-1.5B-Preview"
20
 
21
  # Load the tokenizer and model.
 
22
  if HF_TOKEN:
23
  tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME, token=HF_TOKEN)
24
  model = AutoModelForCausalLM.from_pretrained(
 
30
  MODEL_NAME, torch_dtype=torch.float16, device_map="auto"
31
  )
32
 
33
+ # Define a function to generate AI responses.
34
  def generate_response(prompt):
35
  device = "cuda" if torch.cuda.is_available() else "cpu"
36
  inputs = tokenizer(prompt, return_tensors="pt", truncation=True, max_length=1024).to(device)
37
+ outputs = model.generate(
38
+ **inputs,
39
+ max_new_tokens=200,
40
+ do_sample=True,
41
+ top_p=0.9,
42
+ temperature=0.7
43
+ )
44
  response = tokenizer.decode(outputs[0], skip_special_tokens=True)
45
+ # Replace any instance of the internal model name with the bot's identity.
46
  response = response.replace("DeepScaleR", "Shiv Yantra AI")
47
  return response
48
 
49
+ # --------------------------
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
 
66
  user_input = message.content.strip()
67
  if user_input:
68
  try:
69
+ # Run the synchronous generate_response function in a separate thread.
70
+ ai_response = await asyncio.to_thread(generate_response, user_input)
71
  except Exception as e:
72
+ print(f"Error during generation: {e}")
73
  ai_response = "Error processing your request."
74
  await message.channel.send(ai_response)
75
 
76
  def run_discord_bot():
77
  client.run(DISCORD_TOKEN)
78
 
79
+ # --------------------------
80
+ # (Optional) Gradio Interface Setup
81
+ # --------------------------
82
+ # If you want a web UI (you can disable this if not needed)
83
+ import gradio as gr
84
+ def gradio_api(input_text):
85
+ return generate_response(input_text)
86
+ iface = gr.Interface(fn=gradio_api, inputs="text", outputs="text", title="Shiv Yantra AI")
87
+
88
+ def run_gradio():
89
+ iface.launch(server_name="0.0.0.0", server_port=7860, share=False)
90
+
91
+ # --------------------------
92
+ # Start Services Concurrently
93
+ # --------------------------
94
  if __name__ == "__main__":
95
+ # Optionally, start the Gradio interface in a daemon thread.
96
+ threading.Thread(target=run_gradio, daemon=True).start()
97
+ # Start the Discord bot in a separate thread.
98
  threading.Thread(target=run_discord_bot, daemon=True).start()
99
+
100
  # Keep the main thread alive.
101
  while True:
102
  pass