Update cover.py
Browse files
cover.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
|
| 2 |
|
| 3 |
|
| 4 |
|
|
@@ -10,130 +10,125 @@ from main import song_cover_pipeline # Keeping this import from your original m
|
|
| 10 |
from webui import download_online_model # Import the download function
|
| 11 |
|
| 12 |
|
| 13 |
-
|
| 14 |
-
BASE_DIR = os.path.dirname(os.path.abspath(
|
| 15 |
output_dir = os.path.join(BASE_DIR, 'song_output')
|
| 16 |
|
| 17 |
-
Ensure the output directory exists
|
| 18 |
-
|
| 19 |
os.makedirs(output_dir, exist_ok=True)
|
| 20 |
|
| 21 |
-
|
| 22 |
-
|
| 23 |
async def start(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
| 24 |
-
keyboard = [
|
| 25 |
-
[InlineKeyboardButton("Generate Song", callback_data='generate')],
|
| 26 |
-
[InlineKeyboardButton("Download Model", callback_data='download_model')],
|
| 27 |
-
[InlineKeyboardButton("Help", callback_data='help')]
|
| 28 |
-
]
|
| 29 |
-
reply_markup = InlineKeyboardMarkup(keyboard)
|
| 30 |
-
await update.message.reply_text('Welcome to AICoverGen! Choose an option below:', reply_markup=reply_markup)
|
| 31 |
-
|
| 32 |
-
|
| 33 |
async def button(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
| 34 |
-
query = update.callback_query
|
| 35 |
-
await query.answer()
|
| 36 |
-
|
| 37 |
-
# Determine which option was selected
|
| 38 |
-
if query.data == 'generate':
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
elif query.data == 'download_model':
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
elif query.data == 'help':
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
async def handle_message(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
| 64 |
-
# Check which mode the user is in based on previous button choice
|
| 65 |
-
mode = context.user_data.get('mode')
|
| 66 |
-
|
| 67 |
-
if mode == 'generate':
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
elif mode == 'download_model':
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
else:
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
async def generate_song(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
| 80 |
-
song_input = update.message.text
|
| 81 |
-
try:
|
| 82 |
-
model_name, song_link, pitch_str = song_input.split()
|
| 83 |
-
pitch = int(pitch_str)
|
| 84 |
-
except ValueError:
|
| 85 |
-
await update.message.reply_text(f"Please send a valid input in the format '<model_name> <link> <pitch>' (e.g., 'model1 https://youtube.com/abc 2').")
|
| 86 |
-
return
|
| 87 |
-
|
| 88 |
-
keep_files = False
|
| 89 |
-
is_webui = False
|
| 90 |
-
|
| 91 |
-
song_output = song_cover_pipeline(song_link, model_name, pitch, keep_files, is_webui)
|
| 92 |
-
|
| 93 |
-
if os.path.exists(song_output):
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
else:
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
async def download_model(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
| 101 |
-
model_input = update.message.text
|
| 102 |
-
try:
|
| 103 |
-
# Split the input into model name and URL
|
| 104 |
-
model_name, model_url = model_input.split()
|
| 105 |
-
except ValueError:
|
| 106 |
-
await update.message.reply_text(f"Please send a valid input in the format '<model_name> <url>' (e.g., 'model1 https://model.com/abc').")
|
| 107 |
-
return
|
| 108 |
-
|
| 109 |
-
if not model_url.startswith("http"):
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
try:
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
except Exception as e:
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
def main():
|
| 122 |
-
bot_token =
|
| 123 |
-
|
| 124 |
-
if not bot_token:
|
| 125 |
-
raise ValueError("Bot token not found. Set the TELEGRAM_BOT_TOKEN environment variable.")
|
| 126 |
|
| 127 |
-
|
|
|
|
| 128 |
|
| 129 |
-
|
| 130 |
-
application.add_handler(CommandHandler("start", start))
|
| 131 |
-
application.add_handler(CallbackQueryHandler(button))
|
| 132 |
-
application.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, handle_message)) # Unified message handler
|
| 133 |
|
| 134 |
-
#
|
| 135 |
-
application.
|
|
|
|
|
|
|
| 136 |
|
| 137 |
-
|
| 138 |
-
|
| 139 |
|
|
|
|
|
|
|
|
|
| 1 |
+
TELEGRAM_BOT_TOKEN="7722898432:AAEfj9s6ubY107SWiF6Uy1IKJFFmsiqY_BA"
|
| 2 |
|
| 3 |
|
| 4 |
|
|
|
|
| 10 |
from webui import download_online_model # Import the download function
|
| 11 |
|
| 12 |
|
| 13 |
+
# Define paths
|
| 14 |
+
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
|
| 15 |
output_dir = os.path.join(BASE_DIR, 'song_output')
|
| 16 |
|
| 17 |
+
# Ensure the output directory exists
|
|
|
|
| 18 |
os.makedirs(output_dir, exist_ok=True)
|
| 19 |
|
| 20 |
+
# Start command
|
|
|
|
| 21 |
async def start(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
| 22 |
+
keyboard = [
|
| 23 |
+
[InlineKeyboardButton("Generate Song", callback_data='generate')],
|
| 24 |
+
[InlineKeyboardButton("Download Model", callback_data='download_model')],
|
| 25 |
+
[InlineKeyboardButton("Help", callback_data='help')]
|
| 26 |
+
]
|
| 27 |
+
reply_markup = InlineKeyboardMarkup(keyboard)
|
| 28 |
+
await update.message.reply_text('Welcome to AICoverGen! Choose an option below:', reply_markup=reply_markup)
|
| 29 |
+
|
| 30 |
+
# Button handler
|
| 31 |
async def button(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
| 32 |
+
query = update.callback_query
|
| 33 |
+
await query.answer()
|
| 34 |
+
|
| 35 |
+
# Determine which option was selected
|
| 36 |
+
if query.data == 'generate':
|
| 37 |
+
# Store the user state as 'generate'
|
| 38 |
+
context.user_data['mode'] = 'generate'
|
| 39 |
+
await query.edit_message_text(text="Please send the model name, YouTube link, and pitch (e.g., '<model_name> <link> <pitch>')\nNote: pitch 1 for female and pitch -1 for male.")
|
| 40 |
+
|
| 41 |
+
elif query.data == 'download_model':
|
| 42 |
+
# Store the user state as 'download_model'
|
| 43 |
+
context.user_data['mode'] = 'download_model'
|
| 44 |
+
await query.edit_message_text(text="Please send the model name and URL in the format '<model_name> <url>'.")
|
| 45 |
+
|
| 46 |
+
elif query.data == 'help':
|
| 47 |
+
help_text = (
|
| 48 |
+
"To generate a song, follow these steps:\n"
|
| 49 |
+
"1. Click 'Generate Song'.\n"
|
| 50 |
+
"2. Send a message in the format '<model_name> <link> <pitch>' (e.g., 'model1 https://youtube.com/abc 2').\n"
|
| 51 |
+
"3. Wait for the bot to process and return the generated song.\n"
|
| 52 |
+
"Pitch: Use 1 for female voice, -1 for male voice.\n\n"
|
| 53 |
+
"To download a model:\n"
|
| 54 |
+
"1. Click 'Download Model'.\n"
|
| 55 |
+
"2. Send a message in the format '<model_name> <url>' to specify the model name and the download URL."
|
| 56 |
+
)
|
| 57 |
+
await query.edit_message_text(text=help_text)
|
| 58 |
+
|
| 59 |
+
# Message handler
|
|
|
|
| 60 |
async def handle_message(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
| 61 |
+
# Check which mode the user is in based on previous button choice
|
| 62 |
+
mode = context.user_data.get('mode')
|
| 63 |
+
|
| 64 |
+
if mode == 'generate':
|
| 65 |
+
# Process song generation input
|
| 66 |
+
await generate_song(update, context)
|
| 67 |
+
elif mode == 'download_model':
|
| 68 |
+
# Process model download input
|
| 69 |
+
await download_model(update, context)
|
| 70 |
+
else:
|
| 71 |
+
# If no mode is selected, ask the user to choose an option
|
| 72 |
+
await update.message.reply_text("Please choose an option first by clicking 'Generate Song' or 'Download Model'.")
|
| 73 |
+
|
| 74 |
+
# Generate song handler
|
|
|
|
| 75 |
async def generate_song(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
| 76 |
+
song_input = update.message.text
|
| 77 |
+
try:
|
| 78 |
+
model_name, song_link, pitch_str = song_input.split()
|
| 79 |
+
pitch = int(pitch_str)
|
| 80 |
+
except ValueError:
|
| 81 |
+
await update.message.reply_text(f"Please send a valid input in the format '<model_name> <link> <pitch>' (e.g., 'model1 https://youtube.com/abc 2').")
|
| 82 |
+
return
|
| 83 |
+
|
| 84 |
+
keep_files = False
|
| 85 |
+
is_webui = False
|
| 86 |
+
|
| 87 |
+
song_output = song_cover_pipeline(song_link, model_name, pitch, keep_files, is_webui)
|
| 88 |
+
|
| 89 |
+
if os.path.exists(song_output):
|
| 90 |
+
await update.message.reply_audio(audio=open(song_output, 'rb'))
|
| 91 |
+
os.remove(song_output)
|
| 92 |
+
else:
|
| 93 |
+
await update.message.reply_text(f"An error occurred while generating the song.")
|
| 94 |
+
|
| 95 |
+
# Download model handler with custom name
|
| 96 |
async def download_model(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
| 97 |
+
model_input = update.message.text
|
| 98 |
+
try:
|
| 99 |
+
# Split the input into model name and URL
|
| 100 |
+
model_name, model_url = model_input.split()
|
| 101 |
+
except ValueError:
|
| 102 |
+
await update.message.reply_text(f"Please send a valid input in the format '<model_name> <url>' (e.g., 'model1 https://model.com/abc').")
|
| 103 |
+
return
|
| 104 |
+
|
| 105 |
+
if not model_url.startswith("http"):
|
| 106 |
+
await update.message.reply_text("Please send a valid URL.")
|
| 107 |
+
return
|
| 108 |
+
|
| 109 |
+
try:
|
| 110 |
+
# Call the function to download the model with a custom name
|
| 111 |
+
download_online_model(model_url, model_name)
|
| 112 |
+
await update.message.reply_text(f"Model '{model_name}' downloaded successfully from {model_url}!")
|
| 113 |
+
except Exception as e:
|
| 114 |
+
await update.message.reply_text(f"Failed to download the model. Error: {str(e)}")
|
| 115 |
+
|
| 116 |
+
# Main function to run the bot
|
| 117 |
def main():
|
| 118 |
+
bot_token = TELEGRAM_BOT_TOKEN
|
|
|
|
|
|
|
|
|
|
| 119 |
|
| 120 |
+
if not bot_token:
|
| 121 |
+
raise ValueError("Bot token not found. Set the TELEGRAM_BOT_TOKEN environment variable.")
|
| 122 |
|
| 123 |
+
application = Application.builder().token(bot_token).build()
|
|
|
|
|
|
|
|
|
|
| 124 |
|
| 125 |
+
# Handlers
|
| 126 |
+
application.add_handler(CommandHandler("start", start))
|
| 127 |
+
application.add_handler(CallbackQueryHandler(button))
|
| 128 |
+
application.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, handle_message)) # Unified message handler
|
| 129 |
|
| 130 |
+
# Run the bot
|
| 131 |
+
application.run_polling()
|
| 132 |
|
| 133 |
+
if __name__ == '__main__':
|
| 134 |
+
main()
|