Update app3.py
Browse files
app3.py
CHANGED
|
@@ -1,311 +1,82 @@
|
|
| 1 |
#!/usr/bin/env python3
|
| 2 |
"""
|
| 3 |
-
Discord Bot -
|
| 4 |
-
|
| 5 |
"""
|
| 6 |
|
| 7 |
import discord
|
| 8 |
from discord.ext import commands
|
| 9 |
-
import
|
| 10 |
-
import
|
| 11 |
-
import csv
|
| 12 |
-
import io
|
| 13 |
-
import tempfile
|
| 14 |
-
import random
|
| 15 |
|
| 16 |
# ============================================================
|
| 17 |
# CONFIGURATION
|
| 18 |
# ============================================================
|
| 19 |
TOKEN = "MTQ2ODk2MDIzMjg4MDQ3MjIwNg.GkZcck.IotGRnly_kDwVwUkK9MZ_KXaD6TYI-4lGFKepI"
|
| 20 |
|
| 21 |
-
# Channel name where CSV is uploaded
|
| 22 |
-
CSV_CHANNEL_NAME = "general" # #general channel
|
| 23 |
-
|
| 24 |
-
# Voice styles
|
| 25 |
-
VOICE_STYLES = {
|
| 26 |
-
"1": {"name": "Professional Female", "voice": "en-US-JennyNeural"},
|
| 27 |
-
"2": {"name": "Professional Male", "voice": "en-US-GuyNeural"},
|
| 28 |
-
"3": {"name": "British Female", "voice": "en-GB-SoniaNeural"},
|
| 29 |
-
"4": {"name": "Deep Male", "voice": "en-US-ChristopherNeural"},
|
| 30 |
-
"5": {"name": "Friendly Female", "voice": "en-US-AnaNeural"},
|
| 31 |
-
}
|
| 32 |
-
|
| 33 |
-
# Store prompts
|
| 34 |
-
csv_prompts = []
|
| 35 |
-
|
| 36 |
# ============================================================
|
| 37 |
-
# BOT SETUP
|
| 38 |
# ============================================================
|
| 39 |
intents = discord.Intents.default()
|
| 40 |
-
intents.message_content = True
|
| 41 |
-
# intents.attachments = True # β REMOVED - This doesn't exist in older versions
|
| 42 |
|
| 43 |
bot = commands.Bot(command_prefix="!", intents=intents)
|
| 44 |
|
| 45 |
# ============================================================
|
| 46 |
-
#
|
| 47 |
-
# ============================================================
|
| 48 |
-
async def load_csv_from_general():
|
| 49 |
-
"""Read CSV file from #general channel"""
|
| 50 |
-
global csv_prompts
|
| 51 |
-
|
| 52 |
-
# Find #general channel
|
| 53 |
-
channel = None
|
| 54 |
-
for c in bot.get_all_channels():
|
| 55 |
-
if c.name == CSV_CHANNEL_NAME and isinstance(c, discord.TextChannel):
|
| 56 |
-
channel = c
|
| 57 |
-
break
|
| 58 |
-
|
| 59 |
-
if channel is None:
|
| 60 |
-
return False, f"β #{CSV_CHANNEL_NAME} channel not found!"
|
| 61 |
-
|
| 62 |
-
print(f"π Searching in #{channel.name}...")
|
| 63 |
-
|
| 64 |
-
# Find message with CSV attachment
|
| 65 |
-
async for message in channel.history(limit=200):
|
| 66 |
-
if message.attachments:
|
| 67 |
-
for attachment in message.attachments:
|
| 68 |
-
if attachment.filename.endswith('.csv'):
|
| 69 |
-
csv_content = await attachment.read()
|
| 70 |
-
print(f"π Found CSV: {attachment.filename}")
|
| 71 |
-
|
| 72 |
-
# Parse CSV
|
| 73 |
-
csv_text = csv_content.decode('utf-8')
|
| 74 |
-
csv_reader = csv.DictReader(io.StringIO(csv_text))
|
| 75 |
-
|
| 76 |
-
csv_prompts = []
|
| 77 |
-
for row in csv_reader:
|
| 78 |
-
prompt = row.get('prompt', row.get('prompt_text', row.get('text', '')))
|
| 79 |
-
if prompt:
|
| 80 |
-
csv_prompts.append(prompt)
|
| 81 |
-
|
| 82 |
-
return True, f"β
Loaded {len(csv_prompts)} prompts from #{channel.name}"
|
| 83 |
-
|
| 84 |
-
return False, "β No CSV file found in #general channel!"
|
| 85 |
-
|
| 86 |
-
# ============================================================
|
| 87 |
-
# VOICEOVER GENERATION
|
| 88 |
-
# ============================================================
|
| 89 |
-
def generate_natural_voiceover(raw_prompt):
|
| 90 |
-
"""Convert technical prompt to natural voiceover"""
|
| 91 |
-
|
| 92 |
-
intros = ["Watch how", "See how", "Discover how", "Learn how", "Ever wondered how"]
|
| 93 |
-
outros = ["That's engineering in action!", "Simple mechanics, powerful results!", "That's how industry gets things done!"]
|
| 94 |
-
|
| 95 |
-
if " in " in raw_prompt:
|
| 96 |
-
parts = raw_prompt.split(" in ", 1)
|
| 97 |
-
mechanism_action = parts[0]
|
| 98 |
-
industry = parts[1].split(" -")[0] if " -" in parts[1] else parts[1]
|
| 99 |
-
else:
|
| 100 |
-
mechanism_action = raw_prompt
|
| 101 |
-
industry = "industrial setting"
|
| 102 |
-
|
| 103 |
-
words = mechanism_action.split()
|
| 104 |
-
mechanism = words[0] if words else "mechanism"
|
| 105 |
-
action = " ".join(words[1:]) if len(words) > 1 else "operates"
|
| 106 |
-
|
| 107 |
-
intro = random.choice(intros)
|
| 108 |
-
outro = random.choice(outros)
|
| 109 |
-
article = "an" if mechanism[0].lower() in 'aeiou' else "a"
|
| 110 |
-
|
| 111 |
-
return f"{intro} {article} {mechanism} {action} inside {article} {industry}. {outro}"
|
| 112 |
-
|
| 113 |
-
async def text_to_speech(text, voice_id, output_path):
|
| 114 |
-
"""Convert text to speech using Edge TTS"""
|
| 115 |
-
communicate = edge_tts.Communicate(text, voice_id)
|
| 116 |
-
await communicate.save(output_path)
|
| 117 |
-
return output_path
|
| 118 |
-
|
| 119 |
-
# ============================================================
|
| 120 |
-
# DISCORD COMMANDS
|
| 121 |
# ============================================================
|
| 122 |
|
| 123 |
@bot.event
|
| 124 |
async def on_ready():
|
| 125 |
print("=" * 50)
|
| 126 |
print(f"β
Bot is ready!")
|
| 127 |
-
print(f"π€
|
| 128 |
-
print(f"
|
| 129 |
-
|
| 130 |
-
# Load CSV on startup
|
| 131 |
-
status, msg = await load_csv_from_general()
|
| 132 |
-
print(msg)
|
| 133 |
print("=" * 50)
|
| 134 |
|
| 135 |
-
@bot.command(name="
|
| 136 |
-
async def
|
| 137 |
-
"""
|
| 138 |
-
|
| 139 |
-
await ctx.send(
|
| 140 |
-
|
| 141 |
-
@bot.command(name="list")
|
| 142 |
-
async def list_prompts(ctx, page: int = 1):
|
| 143 |
-
"""List prompts from CSV
|
| 144 |
-
Example: !list 2
|
| 145 |
-
"""
|
| 146 |
-
if not csv_prompts:
|
| 147 |
-
await ctx.send("β No prompts loaded! Use `!reload` first.")
|
| 148 |
-
return
|
| 149 |
-
|
| 150 |
-
per_page = 10
|
| 151 |
-
total_pages = (len(csv_prompts) + per_page - 1) // per_page
|
| 152 |
-
|
| 153 |
-
if page < 1 or page > total_pages:
|
| 154 |
-
await ctx.send(f"β Page {page} not found. Total pages: {total_pages}")
|
| 155 |
-
return
|
| 156 |
-
|
| 157 |
-
start = (page - 1) * per_page
|
| 158 |
-
end = min(start + per_page, len(csv_prompts))
|
| 159 |
-
|
| 160 |
-
response = f"**π Prompts (Page {page}/{total_pages})** - Total: {len(csv_prompts)}\n\n"
|
| 161 |
-
for i in range(start, end):
|
| 162 |
-
response += f"`{i+1}. {csv_prompts[i][:70]}...`\n"
|
| 163 |
-
|
| 164 |
-
await ctx.send(response)
|
| 165 |
-
|
| 166 |
-
@bot.command(name="count")
|
| 167 |
-
async def count_prompts(ctx):
|
| 168 |
-
"""Get total number of prompts"""
|
| 169 |
-
if not csv_prompts:
|
| 170 |
-
await ctx.send("β No prompts loaded! Use `!reload` first.")
|
| 171 |
-
else:
|
| 172 |
-
await ctx.send(f"π Total prompts in CSV: **{len(csv_prompts)}**")
|
| 173 |
-
|
| 174 |
-
@bot.command(name="generate")
|
| 175 |
-
async def generate_prompt(ctx, prompt_id: int):
|
| 176 |
-
"""Generate voiceover for a specific prompt
|
| 177 |
-
Example: !generate 5
|
| 178 |
-
"""
|
| 179 |
-
if not csv_prompts:
|
| 180 |
-
await ctx.send("β No prompts loaded! Use `!reload` first.")
|
| 181 |
-
return
|
| 182 |
-
|
| 183 |
-
if prompt_id < 1 or prompt_id > len(csv_prompts):
|
| 184 |
-
await ctx.send(f"β Prompt ID must be between 1 and {len(csv_prompts)}")
|
| 185 |
-
return
|
| 186 |
-
|
| 187 |
-
prompt_text = csv_prompts[prompt_id - 1]
|
| 188 |
-
|
| 189 |
-
await ctx.send(f"π€ **Generating voiceover for Prompt #{prompt_id}**...\nπ `{prompt_text[:100]}...`")
|
| 190 |
-
|
| 191 |
-
voiceover_text = generate_natural_voiceover(prompt_text)
|
| 192 |
-
|
| 193 |
-
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as tmp:
|
| 194 |
-
await text_to_speech(voiceover_text, "en-US-JennyNeural", tmp.name)
|
| 195 |
-
await ctx.send(file=discord.File(tmp.name, filename=f"prompt_{prompt_id}.mp3"))
|
| 196 |
|
| 197 |
-
@bot.command(name="
|
| 198 |
-
async def
|
| 199 |
-
"""
|
| 200 |
-
|
| 201 |
-
Styles: 1=Female, 2=Male, 3=British, 4=Deep, 5=Friendly
|
| 202 |
-
"""
|
| 203 |
-
if not csv_prompts:
|
| 204 |
-
await ctx.send("β No prompts loaded! Use `!reload` first.")
|
| 205 |
-
return
|
| 206 |
-
|
| 207 |
-
if prompt_id < 1 or prompt_id > len(csv_prompts):
|
| 208 |
-
await ctx.send(f"β Prompt ID must be between 1 and {len(csv_prompts)}")
|
| 209 |
-
return
|
| 210 |
-
|
| 211 |
-
voice = VOICE_STYLES.get(voice_style, VOICE_STYLES["1"])
|
| 212 |
-
prompt_text = csv_prompts[prompt_id - 1]
|
| 213 |
-
|
| 214 |
-
await ctx.send(f"π€ Generating with **{voice['name']}** for Prompt #{prompt_id}...")
|
| 215 |
-
|
| 216 |
-
voiceover_text = generate_natural_voiceover(prompt_text)
|
| 217 |
-
|
| 218 |
-
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as tmp:
|
| 219 |
-
await text_to_speech(voiceover_text, voice["voice"], tmp.name)
|
| 220 |
-
await ctx.send(file=discord.File(tmp.name, filename=f"prompt_{prompt_id}.mp3"))
|
| 221 |
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
"""
|
| 227 |
-
if not csv_prompts:
|
| 228 |
-
await ctx.send("β No prompts loaded! Use `!reload` first.")
|
| 229 |
-
return
|
| 230 |
-
|
| 231 |
-
if end_id is None:
|
| 232 |
-
end_id = len(csv_prompts)
|
| 233 |
-
|
| 234 |
-
if start_id < 1 or end_id > len(csv_prompts) or start_id > end_id:
|
| 235 |
-
await ctx.send(f"β Invalid range. Use 1 to {len(csv_prompts)}")
|
| 236 |
-
return
|
| 237 |
-
|
| 238 |
-
voice = VOICE_STYLES.get(voice_style, VOICE_STYLES["1"])
|
| 239 |
-
total = end_id - start_id + 1
|
| 240 |
-
|
| 241 |
-
await ctx.send(f"π€ Generating {total} voiceovers (IDs {start_id}-{end_id}) with **{voice['name']}**...")
|
| 242 |
-
|
| 243 |
-
for idx in range(start_id, end_id + 1):
|
| 244 |
-
prompt_text = csv_prompts[idx - 1]
|
| 245 |
-
voiceover_text = generate_natural_voiceover(prompt_text)
|
| 246 |
-
|
| 247 |
-
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as tmp:
|
| 248 |
-
await text_to_speech(voiceover_text, voice["voice"], tmp.name)
|
| 249 |
-
await ctx.send(file=discord.File(tmp.name, filename=f"prompt_{idx}.mp3"))
|
| 250 |
-
|
| 251 |
-
await asyncio.sleep(1)
|
| 252 |
-
|
| 253 |
-
await ctx.send(f"β
Completed! Generated {total} voiceovers!")
|
| 254 |
|
| 255 |
-
@
|
| 256 |
-
|
| 257 |
-
"
|
| 258 |
-
if not csv_prompts:
|
| 259 |
-
await ctx.send("β No prompts loaded! Use `!reload` first.")
|
| 260 |
-
return
|
| 261 |
-
|
| 262 |
-
prompt_id = random.randint(1, len(csv_prompts))
|
| 263 |
-
prompt_text = csv_prompts[prompt_id - 1]
|
| 264 |
-
|
| 265 |
-
await ctx.send(f"π² **Random Prompt #{prompt_id}**: `{prompt_text[:100]}...`")
|
| 266 |
-
|
| 267 |
-
voiceover_text = generate_natural_voiceover(prompt_text)
|
| 268 |
-
|
| 269 |
-
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as tmp:
|
| 270 |
-
await text_to_speech(voiceover_text, "en-US-JennyNeural", tmp.name)
|
| 271 |
-
await ctx.send(file=discord.File(tmp.name, filename=f"random_{prompt_id}.mp3"))
|
| 272 |
|
| 273 |
-
|
| 274 |
-
|
| 275 |
-
|
| 276 |
-
embed = discord.Embed(
|
| 277 |
-
title="π€ Voiceover Generator Bot",
|
| 278 |
-
description="Generates voiceovers from CSV in #general",
|
| 279 |
-
color=discord.Color.blue()
|
| 280 |
-
)
|
| 281 |
-
embed.add_field(name="CSV Channel", value="#general", inline=False)
|
| 282 |
-
embed.add_field(name="Prompts Loaded", value=len(csv_prompts) if csv_prompts else 0, inline=False)
|
| 283 |
-
embed.add_field(name="Voice Styles", value="\n".join([f"{k}. {v['name']}" for k, v in VOICE_STYLES.items()]), inline=False)
|
| 284 |
-
embed.add_field(name="Commands", value="""`!reload` - Reload CSV
|
| 285 |
-
`!list` - List prompts
|
| 286 |
-
`!count` - Count prompts
|
| 287 |
-
`!generate <id>` - Generate voiceover
|
| 288 |
-
`!voice <id> <style>` - Generate with style
|
| 289 |
-
`!batch <start> <end>` - Batch generate
|
| 290 |
-
`!random` - Random prompt
|
| 291 |
-
`!info` - This menu""", inline=False)
|
| 292 |
-
|
| 293 |
-
await ctx.send(embed=embed)
|
| 294 |
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
await ctx.send(f"π Pong! Latency: {round(bot.latency * 1000)}ms")
|
| 299 |
|
| 300 |
# ============================================================
|
| 301 |
-
# RUN BOT
|
| 302 |
# ============================================================
|
| 303 |
if __name__ == "__main__":
|
| 304 |
print("=" * 50)
|
| 305 |
-
print("π€ Discord
|
| 306 |
print("=" * 50)
|
| 307 |
-
print("π Reading CSV from #general channel")
|
| 308 |
print("π Starting bot...")
|
|
|
|
| 309 |
print("=" * 50)
|
| 310 |
|
| 311 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
#!/usr/bin/env python3
|
| 2 |
"""
|
| 3 |
+
Simple Discord Bot - Ping Command Only
|
| 4 |
+
Works on Hugging Face Spaces with port 7860
|
| 5 |
"""
|
| 6 |
|
| 7 |
import discord
|
| 8 |
from discord.ext import commands
|
| 9 |
+
import threading
|
| 10 |
+
from flask import Flask
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
# ============================================================
|
| 13 |
# CONFIGURATION
|
| 14 |
# ============================================================
|
| 15 |
TOKEN = "MTQ2ODk2MDIzMjg4MDQ3MjIwNg.GkZcck.IotGRnly_kDwVwUkK9MZ_KXaD6TYI-4lGFKepI"
|
| 16 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
# ============================================================
|
| 18 |
+
# BOT SETUP
|
| 19 |
# ============================================================
|
| 20 |
intents = discord.Intents.default()
|
| 21 |
+
intents.message_content = True
|
|
|
|
| 22 |
|
| 23 |
bot = commands.Bot(command_prefix="!", intents=intents)
|
| 24 |
|
| 25 |
# ============================================================
|
| 26 |
+
# COMMANDS
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
# ============================================================
|
| 28 |
|
| 29 |
@bot.event
|
| 30 |
async def on_ready():
|
| 31 |
print("=" * 50)
|
| 32 |
print(f"β
Bot is ready!")
|
| 33 |
+
print(f"π€ Bot name: {bot.user.name}")
|
| 34 |
+
print(f"π Bot ID: {bot.user.id}")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
print("=" * 50)
|
| 36 |
|
| 37 |
+
@bot.command(name="ping")
|
| 38 |
+
async def ping(ctx):
|
| 39 |
+
"""Check if bot is alive"""
|
| 40 |
+
latency = round(bot.latency * 1000)
|
| 41 |
+
await ctx.send(f"π Pong! Latency: {latency}ms")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
+
@bot.command(name="hello")
|
| 44 |
+
async def hello(ctx):
|
| 45 |
+
"""Say hello"""
|
| 46 |
+
await ctx.send(f"π Hello {ctx.author.mention}!")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
+
# ============================================================
|
| 49 |
+
# FLASK SERVER FOR PORT 7860 (Hugging Face Spaces)
|
| 50 |
+
# ============================================================
|
| 51 |
+
app = Flask(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
+
@app.route('/')
|
| 54 |
+
def home():
|
| 55 |
+
return "π€ Discord Bot is Running!"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
+
def run_bot():
|
| 58 |
+
"""Run Discord bot"""
|
| 59 |
+
bot.run(TOKEN)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
|
| 61 |
+
def run_flask():
|
| 62 |
+
"""Run Flask server on port 7860"""
|
| 63 |
+
app.run(host='0.0.0.0', port=7860)
|
|
|
|
| 64 |
|
| 65 |
# ============================================================
|
| 66 |
+
# RUN BOT AND FLASK TOGETHER
|
| 67 |
# ============================================================
|
| 68 |
if __name__ == "__main__":
|
| 69 |
print("=" * 50)
|
| 70 |
+
print("π€ Simple Discord Bot")
|
| 71 |
print("=" * 50)
|
|
|
|
| 72 |
print("π Starting bot...")
|
| 73 |
+
print("π Flask server on port 7860")
|
| 74 |
print("=" * 50)
|
| 75 |
|
| 76 |
+
# Run Flask in separate thread
|
| 77 |
+
flask_thread = threading.Thread(target=run_flask)
|
| 78 |
+
flask_thread.daemon = True
|
| 79 |
+
flask_thread.start()
|
| 80 |
+
|
| 81 |
+
# Run Discord bot
|
| 82 |
+
run_bot()
|