MineisZarox commited on
Commit
aded39e
·
1 Parent(s): d7373ae
Files changed (1) hide show
  1. app.py +112 -135
app.py CHANGED
@@ -5,16 +5,122 @@ import threading
5
  import gradio as gr
6
  from io import BytesIO
7
  from fusion import Fusion
8
- from telethon.tl.types import InputFile
9
- from telethon import TelegramClient, events, Button
10
 
11
- from telethon.tl.functions.messages import SendMediaRequest
12
- from telethon.tl.types import InputMediaUploadedDocument
13
 
14
  API_ID = os.environ.get("API_ID")
15
  API_HASH = os.environ.get("API_HASH")
16
  BOT_TOKEN = os.environ.get("BOT_TOKEN")
17
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
  def process_audio(input_file,
19
  effect_8d, pan_boundary, jump_percentage, time_l_to_r, volume_multiplier,
20
  effect_slowed, speed_multiplier,
@@ -39,8 +145,8 @@ def process_audio(input_file,
39
  output_file = f"{input_file} {' + '.join(effects_str)} - {'By AudioFusion'}"
40
 
41
  # Save the processed sound and return the output file
42
- output = Fusion.saveSound(sound, output_file)
43
- return output
44
 
45
 
46
  before_text = """<div align="center">
@@ -137,135 +243,6 @@ with gr.Blocks(title="Audio Fusion") as iface:
137
  btnClear.add(components=output)
138
  btnRun.click(fn=process_audio, inputs=inputs, outputs=output, api_name="AudioFusion")
139
 
140
-
141
-
142
- client = TelegramClient('session_name', API_ID, API_HASH)
143
-
144
-
145
- async def send_audio(chat, audio):
146
- media = InputMediaUploadedDocument(
147
- file=await client.upload_file(audio),
148
- mime_type='audio/mpeg',
149
- attributes=[
150
- DocumentAttributeAudio(
151
- title='Audio Title', # Set the title of the audio
152
- )
153
- ]
154
- )
155
-
156
- # Send the audio file as music
157
- await client(SendMediaRequest(
158
- peer=chat,
159
- media=media,
160
- message='Optional caption for the audio'
161
- ))
162
-
163
- states = {}
164
-
165
- @client.on(events.NewMessage(pattern='/start'))
166
- async def start_handler(event):
167
- await event.reply("Welcome to AudioFusion Bot! Send me an audio file, and I'll apply effects for you.")
168
-
169
- buttons = [
170
- [Button.inline('Slowed', b'slowed'), Button.inline('8D', b'8d')],
171
- [Button.inline('Reverb', b'reverb'), Button.inline('Reverse', b'reverse')],
172
- [Button.inline('Volume', b'volume'), Button.inline('Speedup', b'speedup')],
173
- [Button.inline('Preview', b'preview')],
174
- [Button.inline('Send', b'send')],
175
- ]
176
-
177
- @client.on(events.NewMessage(pattern='/buttons'))
178
- async def buttons_handler(event):
179
- user_id = event.sender_id
180
-
181
- # Download the audio file and store it in the user's state
182
- reply_message = await event.get_reply_message()
183
- if not reply_message or not reply_message.file:
184
- await event.reply("Please reply to an audio file.")
185
- return
186
-
187
- audio_file = BytesIO()
188
- await event.client.download_media(reply_message, audio_file)
189
- audio_file.seek(0)
190
-
191
- # Store the audio file in the user's state
192
- states[user_id] = {'audio': audio_file}
193
-
194
- await client.send_file(event.chat_id, file="image.jpg", caption="Preview the current modification:", buttons=buttons)
195
-
196
-
197
- @client.on(events.CallbackQuery(pattern=b'(slowed|8d|reverb|reverse|trim|volume|speedup)'))
198
- async def audio_effect_handler(event):
199
- user_id = event.sender_id
200
- if user_id not in states or not states[user_id]:
201
- await event.answer("No audio file found. Please use /buttons command to upload an audio file.")
202
- return
203
-
204
- # Retrieve the audio file from the user's state
205
- audio_file = states[user_id]['audio']
206
-
207
- query = event.pattern_match.group(1).decode("UTF-8")
208
- sound = Fusion.from_file(audio_file, format="mp3")
209
- if query == 'slowed':
210
- modified_sound = await Fusion.effectSlowed(sound)
211
- elif query == 'speedup':
212
- modified_sound = await Fusion.effectSlowed(sound, 1.1)
213
- elif query == '8d':
214
- modified_sound = await Fusion.effect8D(sound)
215
- elif query == 'reverb':
216
- modified_sound = await Fusion.effectReverb(sound)
217
- elif query == 'reverse':
218
- modified_sound = sound.reverse()
219
- else:
220
- return await event.answer("INvalid for now...")
221
-
222
- audio_file = BytesIO()
223
- audio = modified_sound.export(audio_file, format="mp3")
224
- audio.seek(0)
225
- # Update the user's state with the modified sound
226
- states[user_id]['audio'] = audio
227
-
228
- await event.answer("Effect applied. Click /send to receive the modified audio file.", alert=True)
229
-
230
-
231
-
232
- @client.on(events.CallbackQuery(pattern=b'preview'))
233
- async def preview_handler(event):
234
- user_id = event.sender_id
235
- if user_id in states and states[user_id]:
236
- # Send the current modification for preview
237
- output_file_name = f"{user_id}_preview"
238
- output_file = await Fusion.saveSound(states[user_id]['audio'], output_file_name)
239
- await event.edit("`Uploading...`", buttons=buttons)
240
- # Edit the message and send the audio file in the edited message
241
- await event.edit(file=output_file, text="`Preview the current modification:`", buttons=buttons)
242
-
243
- # Clean up - remove the saved preview audio file
244
- os.remove(output_file)
245
- else:
246
- await event.answer("No modified audio file found. Please apply an effect first.", alert=True)
247
-
248
-
249
- @client.on(events.CallbackQuery(pattern=b'send'))
250
- async def send_handler(event):
251
- user_id = event.sender_id
252
- if user_id in states and states[user_id]:
253
- # Send the modified sound file
254
- # output_file_name = f"{user_id}_modified_audio"
255
- # output_file = await Fusion.saveSound(states[user_id]["audio"], output_file_name)
256
- # await event.reply(file=output_file)
257
-
258
- await client.send_file(event.chat_id, file=InputFile(states[user_id]["audio"], "Audio.mp3"))
259
-
260
- # Clean up - remove the user's state and the saved audio file
261
- del states[user_id]
262
- # os.remove(output_file)
263
- await event.delete()
264
- else:
265
- await event.answer("No modified audio file found. Please apply an effect first.")
266
-
267
-
268
-
269
  async def initiation():
270
  await client.send_message(-1001662130485, "**Hugging is Running.**", buttons=[(Button.url("Execal", "https://t.me/execal"),)],)
271
 
 
5
  import gradio as gr
6
  from io import BytesIO
7
  from fusion import Fusion
8
+ from telethon import TelegramClient, events, Button, types
 
9
 
 
 
10
 
11
  API_ID = os.environ.get("API_ID")
12
  API_HASH = os.environ.get("API_HASH")
13
  BOT_TOKEN = os.environ.get("BOT_TOKEN")
14
 
15
+
16
+ client = TelegramClient('session_name', API_ID, API_HASH)
17
+
18
+ states = {}
19
+
20
+ @client.on(events.NewMessage(pattern='/start'))
21
+ async def start_handler(event):
22
+ await event.reply("Welcome to AudioFusion Bot! Send me an audio file, and I'll apply effects for you.")
23
+
24
+ buttons = [
25
+ [Button.inline('Slowed', b'slowed'), Button.inline('8D', b'8d')],
26
+ [Button.inline('Reverb', b'reverb'), Button.inline('Reverse', b'reverse')],
27
+ [Button.inline('Volume', b'volume'), Button.inline('Speedup', b'speedup')],
28
+ [Button.inline('Preview', b'preview')],
29
+ [Button.inline('Send', b'send')],
30
+ ]
31
+
32
+ @client.on(events.NewMessage(pattern='/buttons'))
33
+ async def buttons_handler(event):
34
+ user_id = event.sender_id
35
+
36
+ # Download the audio file and store it in the user's state
37
+ reply_message = await event.get_reply_message()
38
+ if not reply_message or not reply_message.file:
39
+ await event.reply("Please reply to an audio file.")
40
+ return
41
+
42
+ audio_file = BytesIO()
43
+ await event.client.download_media(reply_message, audio_file)
44
+ audio_file.seek(0)
45
+
46
+ # Store the audio file in the user's state
47
+ states[user_id] = {'audio': audio_file}
48
+
49
+ await client.send_file(event.chat_id, file="image.jpg", caption="Preview the current modification:", buttons=buttons)
50
+
51
+
52
+ @client.on(events.CallbackQuery(pattern=b'(slowed|8d|reverb|reverse|trim|volume|speedup)'))
53
+ async def audio_effect_handler(event):
54
+ user_id = event.sender_id
55
+ if user_id not in states or not states[user_id]:
56
+ await event.answer("No audio file found. Please use /buttons command to upload an audio file.")
57
+ return
58
+
59
+ # Retrieve the audio file from the user's state
60
+ audio_file = states[user_id]['audio']
61
+
62
+ query = event.pattern_match.group(1).decode("UTF-8")
63
+ sound = Fusion.from_file(audio_file, format="mp3")
64
+ if query == 'slowed':
65
+ modified_sound = await Fusion.effectSlowed(sound)
66
+ elif query == 'speedup':
67
+ modified_sound = await Fusion.effectSlowed(sound, 1.1)
68
+ elif query == '8d':
69
+ modified_sound = await Fusion.effect8D(sound)
70
+ elif query == 'reverb':
71
+ modified_sound = await Fusion.effectReverb(sound)
72
+ elif query == 'reverse':
73
+ modified_sound = sound.reverse()
74
+ else:
75
+ return await event.answer("INvalid for now...")
76
+
77
+ audio_file = BytesIO()
78
+ audio = modified_sound.export(audio_file, format="mp3")
79
+ audio.seek(0)
80
+ # Update the user's state with the modified sound
81
+ states[user_id]['audio'] = audio
82
+
83
+ await event.answer("Effect applied. Click /send to receive the modified audio file.", alert=True)
84
+
85
+
86
+
87
+ @client.on(events.CallbackQuery(pattern=b'preview'))
88
+ async def preview_handler(event):
89
+ user_id = event.sender_id
90
+ if user_id in states and states[user_id]:
91
+ # Send the current modification for preview
92
+ output_file_name = f"{user_id}_preview"
93
+ output_file = await Fusion.saveSound(states[user_id]['audio'], output_file_name)
94
+ await event.edit("`Uploading...`", buttons=buttons)
95
+ # Edit the message and send the audio file in the edited message
96
+ await event.edit(file=output_file, text="`Preview the current modification:`", buttons=buttons)
97
+
98
+ # Clean up - remove the saved preview audio file
99
+ os.remove(output_file)
100
+ else:
101
+ await event.answer("No modified audio file found. Please apply an effect first.", alert=True)
102
+
103
+
104
+ @client.on(events.CallbackQuery(pattern=b'send'))
105
+ async def send_handler(event):
106
+ user_id = event.sender_id
107
+ if user_id in states and states[user_id]:
108
+ # Send the modified sound file
109
+ # output_file_name = f"{user_id}_modified_audio"
110
+ # output_file = await Fusion.saveSound(states[user_id]["audio"], output_file_name)
111
+ # await event.reply(file=output_file)
112
+
113
+ await client.send_file(event.chat_id, file=states[user_id]["audio"], attributes=types.DocumentAttributeFilename(file_name="Audio.mp3"))
114
+
115
+ # Clean up - remove the user's state and the saved audio file
116
+ del states[user_id]
117
+ # os.remove(output_file)
118
+ await event.delete()
119
+ else:
120
+ await event.answer("No modified audio file found. Please apply an effect first.")
121
+
122
+
123
+
124
  def process_audio(input_file,
125
  effect_8d, pan_boundary, jump_percentage, time_l_to_r, volume_multiplier,
126
  effect_slowed, speed_multiplier,
 
145
  output_file = f"{input_file} {' + '.join(effects_str)} - {'By AudioFusion'}"
146
 
147
  # Save the processed sound and return the output file
148
+ return Fusion.saveSound(sound, output_file)
149
+
150
 
151
 
152
  before_text = """<div align="center">
 
243
  btnClear.add(components=output)
244
  btnRun.click(fn=process_audio, inputs=inputs, outputs=output, api_name="AudioFusion")
245
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
246
  async def initiation():
247
  await client.send_message(-1001662130485, "**Hugging is Running.**", buttons=[(Button.url("Execal", "https://t.me/execal"),)],)
248