Update cover.py
Browse files
cover.py
CHANGED
@@ -1,192 +1,161 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import os
|
2 |
from telegram import Update, InlineKeyboardButton, InlineKeyboardMarkup
|
3 |
-
from telegram.ext import Application, CommandHandler, CallbackQueryHandler, MessageHandler, filters
|
4 |
-
from
|
5 |
-
from
|
|
|
|
|
|
|
6 |
|
7 |
-
|
8 |
-
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
|
9 |
output_dir = os.path.join(BASE_DIR, 'song_output')
|
|
|
|
|
|
|
10 |
os.makedirs(output_dir, exist_ok=True)
|
11 |
|
12 |
-
|
|
|
13 |
async def start(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
|
|
23 |
async def button(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
[InlineKeyboardButton("Set Model URL", callback_data='set_model_url')],
|
54 |
-
[InlineKeyboardButton("Set Model Name", callback_data='set_download_model')],
|
55 |
-
[InlineKeyboardButton("Submit", callback_data='submit_download')],
|
56 |
-
[InlineKeyboardButton("Back", callback_data='back')]
|
57 |
-
]
|
58 |
-
reply_markup = InlineKeyboardMarkup(keyboard)
|
59 |
-
# Initialize storage for download parameters
|
60 |
-
context.user_data['download_data'] = {
|
61 |
-
'url': None,
|
62 |
-
'model': None
|
63 |
-
}
|
64 |
-
await query.edit_message_text(text="Please set your model download options:", reply_markup=reply_markup)
|
65 |
-
|
66 |
-
elif query.data == 'help':
|
67 |
-
help_text = (
|
68 |
-
"To generate a song:\n"
|
69 |
-
"1. Click 'Generate Song'.\n"
|
70 |
-
"2. Use the buttons to set each option:\n"
|
71 |
-
" • Song Link/Audio input\n"
|
72 |
-
" • Model Name\n"
|
73 |
-
" • Pitch (e.g., 1 for female, -1 for male)\n"
|
74 |
-
" • Toggle Keep Files if needed\n"
|
75 |
-
"3. Press 'Submit' to generate the song.\n\n"
|
76 |
-
"To download a model:\n"
|
77 |
-
"1. Click 'Download Model'.\n"
|
78 |
-
"2. Use the buttons to set Model URL and Model Name.\n"
|
79 |
-
"3. Press 'Submit' to download the model."
|
80 |
-
)
|
81 |
-
await query.edit_message_text(text=help_text)
|
82 |
-
|
83 |
-
elif query.data == 'back':
|
84 |
-
# Return to the main menu
|
85 |
-
keyboard = [
|
86 |
-
[InlineKeyboardButton("Generate Song", callback_data='generate')],
|
87 |
-
[InlineKeyboardButton("Download Model", callback_data='download_model')],
|
88 |
-
[InlineKeyboardButton("Help", callback_data='help')]
|
89 |
-
]
|
90 |
-
reply_markup = InlineKeyboardMarkup(keyboard)
|
91 |
-
await query.edit_message_text(text="Welcome back to AICoverGen! Choose an option:", reply_markup=reply_markup)
|
92 |
-
|
93 |
-
# Submission for song generation
|
94 |
-
elif query.data == 'submit_generation':
|
95 |
-
song_data = context.user_data.get('song_data', {})
|
96 |
-
input_val = song_data.get('input')
|
97 |
-
model_name = song_data.get('model')
|
98 |
-
pitch = song_data.get('pitch')
|
99 |
-
keep_files = song_data.get('keep_files', False)
|
100 |
-
if not input_val or not model_name or pitch is None:
|
101 |
-
await query.edit_message_text(text="Missing parameters! Ensure you set Song Link/Audio, Model Name, and Pitch.")
|
102 |
-
return
|
103 |
-
song_output = song_cover_pipeline(input_val, model_name, pitch, keep_files, is_webui=False)
|
104 |
-
if os.path.exists(song_output):
|
105 |
-
await query.edit_message_text(text="Song generated successfully. Sending audio...")
|
106 |
-
await context.bot.send_audio(chat_id=update.effective_chat.id, audio=open(song_output, 'rb'))
|
107 |
-
os.remove(song_output)
|
108 |
-
else:
|
109 |
-
await query.edit_message_text(text="An error occurred while generating the song.")
|
110 |
-
|
111 |
-
# Submission for model download
|
112 |
-
elif query.data == 'submit_download':
|
113 |
-
download_data = context.user_data.get('download_data', {})
|
114 |
-
model_url = download_data.get('url')
|
115 |
-
model_name = download_data.get('model')
|
116 |
-
if not model_url or not model_name:
|
117 |
-
await query.edit_message_text(text="Missing parameters! Ensure you set both Model URL and Model Name.")
|
118 |
-
return
|
119 |
-
try:
|
120 |
-
download_online_model(model_url, model_name)
|
121 |
-
await query.edit_message_text(text=f"Model '{model_name}' downloaded successfully from {model_url}!")
|
122 |
-
except Exception as e:
|
123 |
-
await query.edit_message_text(text=f"Failed to download the model. Error: {str(e)}")
|
124 |
-
|
125 |
-
# Handling parameter setting buttons for generation
|
126 |
-
elif query.data == 'set_input':
|
127 |
-
await query.edit_message_text(text="Please send the Song Link or upload an audio file.")
|
128 |
-
context.user_data['awaiting_input'] = 'input'
|
129 |
-
elif query.data == 'set_model':
|
130 |
-
await query.edit_message_text(text="Please send the Model Name.")
|
131 |
-
context.user_data['awaiting_input'] = 'model'
|
132 |
-
elif query.data == 'set_pitch':
|
133 |
-
await query.edit_message_text(text="Please send the Pitch (e.g., 1 for female, -1 for male).")
|
134 |
-
context.user_data['awaiting_input'] = 'pitch'
|
135 |
-
elif query.data == 'toggle_keep':
|
136 |
-
song_data = context.user_data.get('song_data', {})
|
137 |
-
current = song_data.get('keep_files', False)
|
138 |
-
song_data['keep_files'] = not current
|
139 |
-
context.user_data['song_data'] = song_data
|
140 |
-
await query.answer(text=f"Keep Files set to {song_data['keep_files']}", show_alert=True)
|
141 |
-
|
142 |
-
# Handling parameter setting buttons for download
|
143 |
-
elif query.data == 'set_model_url':
|
144 |
-
await query.edit_message_text(text="Please send the Model URL.")
|
145 |
-
context.user_data['awaiting_input'] = 'model_url'
|
146 |
-
elif query.data == 'set_download_model':
|
147 |
-
await query.edit_message_text(text="Please send the Model Name for download.")
|
148 |
-
context.user_data['awaiting_input'] = 'download_model'
|
149 |
-
|
150 |
-
# Message handler to capture text input after a parameter-setting prompt
|
151 |
async def handle_message(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
180 |
|
181 |
def main():
|
182 |
-
|
183 |
-
|
184 |
-
|
185 |
-
|
186 |
-
|
187 |
-
|
188 |
-
|
189 |
-
|
190 |
-
|
191 |
-
|
192 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
Change the bot to multi button option bot, like:
|
2 |
+
Inference Options:
|
3 |
+
|
4 |
+
1. song_link or audio input
|
5 |
+
|
6 |
+
|
7 |
+
2. model_name,
|
8 |
+
|
9 |
+
|
10 |
+
3. pitch,
|
11 |
+
|
12 |
+
|
13 |
+
4. keep_files
|
14 |
+
Download options:
|
15 |
+
|
16 |
+
|
17 |
+
5. Url
|
18 |
+
|
19 |
+
|
20 |
+
6. model name
|
21 |
+
|
22 |
+
|
23 |
+
|
24 |
import os
|
25 |
from telegram import Update, InlineKeyboardButton, InlineKeyboardMarkup
|
26 |
+
from telegram.ext import Application, CommandHandler, CallbackQueryHandler, MessageHandler, filters
|
27 |
+
from telegram.ext import ContextTypes
|
28 |
+
from main import song_cover_pipeline # Keeping this import from your original main.py
|
29 |
+
from webui import download_online_model # Import the download function
|
30 |
+
|
31 |
+
Define paths
|
32 |
|
33 |
+
BASE_DIR = os.path.dirname(os.path.abspath(file))
|
|
|
34 |
output_dir = os.path.join(BASE_DIR, 'song_output')
|
35 |
+
|
36 |
+
Ensure the output directory exists
|
37 |
+
|
38 |
os.makedirs(output_dir, exist_ok=True)
|
39 |
|
40 |
+
Start command
|
41 |
+
|
42 |
async def start(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
43 |
+
keyboard = [
|
44 |
+
[InlineKeyboardButton("Generate Song", callback_data='generate')],
|
45 |
+
[InlineKeyboardButton("Download Model", callback_data='download_model')],
|
46 |
+
[InlineKeyboardButton("Help", callback_data='help')]
|
47 |
+
]
|
48 |
+
reply_markup = InlineKeyboardMarkup(keyboard)
|
49 |
+
await update.message.reply_text('Welcome to AICoverGen! Choose an option below:', reply_markup=reply_markup)
|
50 |
+
|
51 |
+
Button handler
|
52 |
+
|
53 |
async def button(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
54 |
+
query = update.callback_query
|
55 |
+
await query.answer()
|
56 |
+
|
57 |
+
# Determine which option was selected
|
58 |
+
if query.data == 'generate':
|
59 |
+
# Store the user state as 'generate'
|
60 |
+
context.user_data['mode'] = 'generate'
|
61 |
+
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.")
|
62 |
+
|
63 |
+
elif query.data == 'download_model':
|
64 |
+
# Store the user state as 'download_model'
|
65 |
+
context.user_data['mode'] = 'download_model'
|
66 |
+
await query.edit_message_text(text="Please send the model name and URL in the format '<model_name> <url>'.")
|
67 |
+
|
68 |
+
elif query.data == 'help':
|
69 |
+
help_text = (
|
70 |
+
"To generate a song, follow these steps:\n"
|
71 |
+
"1. Click 'Generate Song'.\n"
|
72 |
+
"2. Send a message in the format '<model_name> <link> <pitch>' (e.g., 'model1 https://youtube.com/abc 2').\n"
|
73 |
+
"3. Wait for the bot to process and return the generated song.\n"
|
74 |
+
"Pitch: Use 1 for female voice, -1 for male voice.\n\n"
|
75 |
+
"To download a model:\n"
|
76 |
+
"1. Click 'Download Model'.\n"
|
77 |
+
"2. Send a message in the format '<model_name> <url>' to specify the model name and the download URL."
|
78 |
+
)
|
79 |
+
await query.edit_message_text(text=help_text)
|
80 |
+
|
81 |
+
Message handler
|
82 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
83 |
async def handle_message(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
84 |
+
# Check which mode the user is in based on previous button choice
|
85 |
+
mode = context.user_data.get('mode')
|
86 |
+
|
87 |
+
if mode == 'generate':
|
88 |
+
# Process song generation input
|
89 |
+
await generate_song(update, context)
|
90 |
+
elif mode == 'download_model':
|
91 |
+
# Process model download input
|
92 |
+
await download_model(update, context)
|
93 |
+
else:
|
94 |
+
# If no mode is selected, ask the user to choose an option
|
95 |
+
await update.message.reply_text("Please choose an option first by clicking 'Generate Song' or 'Download Model'.")
|
96 |
+
|
97 |
+
Generate song handler
|
98 |
+
|
99 |
+
async def generate_song(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
100 |
+
song_input = update.message.text
|
101 |
+
try:
|
102 |
+
model_name, song_link, pitch_str = song_input.split()
|
103 |
+
pitch = int(pitch_str)
|
104 |
+
except ValueError:
|
105 |
+
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').")
|
106 |
+
return
|
107 |
+
|
108 |
+
keep_files = False
|
109 |
+
is_webui = False
|
110 |
+
|
111 |
+
song_output = song_cover_pipeline(song_link, model_name, pitch, keep_files, is_webui)
|
112 |
+
|
113 |
+
if os.path.exists(song_output):
|
114 |
+
await update.message.reply_audio(audio=open(song_output, 'rb'))
|
115 |
+
os.remove(song_output)
|
116 |
+
else:
|
117 |
+
await update.message.reply_text(f"An error occurred while generating the song.")
|
118 |
+
|
119 |
+
Download model handler with custom name
|
120 |
+
|
121 |
+
async def download_model(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
122 |
+
model_input = update.message.text
|
123 |
+
try:
|
124 |
+
# Split the input into model name and URL
|
125 |
+
model_name, model_url = model_input.split()
|
126 |
+
except ValueError:
|
127 |
+
await update.message.reply_text(f"Please send a valid input in the format '<model_name> <url>' (e.g., 'model1 https://model.com/abc').")
|
128 |
+
return
|
129 |
+
|
130 |
+
if not model_url.startswith("http"):
|
131 |
+
await update.message.reply_text("Please send a valid URL.")
|
132 |
+
return
|
133 |
+
|
134 |
+
try:
|
135 |
+
# Call the function to download the model with a custom name
|
136 |
+
download_online_model(model_url, model_name)
|
137 |
+
await update.message.reply_text(f"Model '{model_name}' downloaded successfully from {model_url}!")
|
138 |
+
except Exception as e:
|
139 |
+
await update.message.reply_text(f"Failed to download the model. Error: {str(e)}")
|
140 |
+
|
141 |
+
Main function to run the bot
|
142 |
|
143 |
def main():
|
144 |
+
bot_token = "7722898432:AAEfj9s6ubY107SWiF6Uy1IKJFFmsiqY_BA"
|
145 |
+
|
146 |
+
if not bot_token:
|
147 |
+
raise ValueError("Bot token not found. Set the TELEGRAM_BOT_TOKEN environment variable.")
|
148 |
+
|
149 |
+
application = Application.builder().token(bot_token).build()
|
150 |
+
|
151 |
+
# Handlers
|
152 |
+
application.add_handler(CommandHandler("start", start))
|
153 |
+
application.add_handler(CallbackQueryHandler(button))
|
154 |
+
application.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, handle_message)) # Unified message handler
|
155 |
+
|
156 |
+
# Run the bot
|
157 |
+
application.run_polling()
|
158 |
+
|
159 |
+
if name == 'main':
|
160 |
+
main()
|
161 |
+
|