NeoPy commited on
Commit
9e1334d
·
verified ·
1 Parent(s): 3421cfe

Update cover.py

Browse files
Files changed (1) hide show
  1. cover.py +150 -181
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, ContextTypes
4
- from main import song_cover_pipeline # Import your song generation pipeline
5
- from webui import download_online_model # Import the model download function
 
 
 
6
 
7
- # Define paths
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
- # /start command handler: show main menu
 
13
  async def start(update: Update, context: ContextTypes.DEFAULT_TYPE):
14
- keyboard = [
15
- [InlineKeyboardButton("Generate Song", callback_data='generate')],
16
- [InlineKeyboardButton("Download Model", callback_data='download_model')],
17
- [InlineKeyboardButton("Help", callback_data='help')]
18
- ]
19
- reply_markup = InlineKeyboardMarkup(keyboard)
20
- await update.message.reply_text('Welcome to AICoverGen! Choose an option below:', reply_markup=reply_markup)
21
-
22
- # CallbackQuery handler for inline buttons
 
23
  async def button(update: Update, context: ContextTypes.DEFAULT_TYPE):
24
- query = update.callback_query
25
- await query.answer()
26
-
27
- # Main menu selections
28
- if query.data == 'generate':
29
- context.user_data['mode'] = 'generate'
30
- # Prepare an inline keyboard for song generation options
31
- keyboard = [
32
- [InlineKeyboardButton("Set Song Link/Audio", callback_data='set_input')],
33
- [InlineKeyboardButton("Set Model Name", callback_data='set_model')],
34
- [InlineKeyboardButton("Set Pitch", callback_data='set_pitch')],
35
- [InlineKeyboardButton("Toggle Keep Files", callback_data='toggle_keep')],
36
- [InlineKeyboardButton("Submit", callback_data='submit_generation')],
37
- [InlineKeyboardButton("Back", callback_data='back')]
38
- ]
39
- reply_markup = InlineKeyboardMarkup(keyboard)
40
- # Initialize storage for generation parameters
41
- context.user_data['song_data'] = {
42
- 'input': None,
43
- 'model': None,
44
- 'pitch': None,
45
- 'keep_files': False
46
- }
47
- await query.edit_message_text(text="Please set your song generation options:", reply_markup=reply_markup)
48
-
49
- elif query.data == 'download_model':
50
- context.user_data['mode'] = 'download_model'
51
- # Prepare an inline keyboard for model download options
52
- keyboard = [
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
- mode = context.user_data.get('mode')
153
- if 'awaiting_input' in context.user_data:
154
- key = context.user_data.pop('awaiting_input')
155
- if mode == 'generate':
156
- song_data = context.user_data.get('song_data', {})
157
- if key == 'input':
158
- # Here you could also add logic to handle audio files if needed
159
- song_data['input'] = update.message.text
160
- elif key == 'model':
161
- song_data['model'] = update.message.text
162
- elif key == 'pitch':
163
- try:
164
- song_data['pitch'] = int(update.message.text)
165
- except ValueError:
166
- await update.message.reply_text("Invalid pitch. Please send a valid integer (e.g., 1 or -1).")
167
- return
168
- context.user_data['song_data'] = song_data
169
- await update.message.reply_text(f"Set {key} to {update.message.text}.")
170
- elif mode == 'download_model':
171
- download_data = context.user_data.get('download_data', {})
172
- if key == 'model_url':
173
- download_data['url'] = update.message.text
174
- elif key == 'download_model':
175
- download_data['model'] = update.message.text
176
- context.user_data['download_data'] = download_data
177
- await update.message.reply_text(f"Set {key} to {update.message.text}.")
178
- else:
179
- await update.message.reply_text("Please choose an option first by clicking one of the buttons.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
180
 
181
  def main():
182
- bot_token = "7722898432:"+"AAEfj9s6ubY107SWiF6Uy1IKJFFmsiqY_BA"
183
- if not bot_token:
184
- raise ValueError("Bot token not found. Set the TELEGRAM_BOT_TOKEN environment variable.")
185
- application = Application.builder().token(bot_token).build()
186
- application.add_handler(CommandHandler("start", start))
187
- application.add_handler(CallbackQueryHandler(button))
188
- application.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, handle_message))
189
- application.run_polling()
190
-
191
- if __name__ == '__main__':
192
- main()
 
 
 
 
 
 
 
 
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
+