Update app.py
Browse files
app.py
CHANGED
|
@@ -1,27 +1,60 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from gradio_client import Client, handle_file
|
| 3 |
import os
|
|
|
|
|
|
|
| 4 |
|
| 5 |
BACKEND_SPACE = "CleanSong-AI/Main-tool-backend-main"
|
| 6 |
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
if file_path is None:
|
| 9 |
return None
|
| 10 |
|
|
|
|
| 11 |
client = Client(BACKEND_SPACE)
|
| 12 |
-
|
| 13 |
result = client.predict(
|
| 14 |
-
handle_file(file_path),
|
| 15 |
api_name="/predict"
|
| 16 |
)
|
| 17 |
|
| 18 |
-
# Normalize HF return
|
| 19 |
if isinstance(result, (list, tuple)):
|
| 20 |
result = result[0]
|
| 21 |
-
|
| 22 |
if isinstance(result, dict) and "name" in result:
|
| 23 |
result = result["name"]
|
| 24 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
return result
|
| 26 |
|
| 27 |
# ------------------------------------------------------------
|
|
@@ -29,11 +62,14 @@ def clean_song(file_path):
|
|
| 29 |
# ------------------------------------------------------------
|
| 30 |
iface = gr.Interface(
|
| 31 |
fn=clean_song,
|
| 32 |
-
inputs=
|
|
|
|
|
|
|
|
|
|
| 33 |
outputs=gr.Audio(type="filepath", label="Cleaned Song"),
|
| 34 |
title="CleanSong AI - Prototype Frontend",
|
| 35 |
-
description="Upload a song and get a cleaned version."
|
| 36 |
)
|
| 37 |
|
| 38 |
if __name__ == "__main__":
|
| 39 |
-
iface.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from gradio_client import Client, handle_file
|
| 3 |
import os
|
| 4 |
+
import smtplib
|
| 5 |
+
from email.message import EmailMessage
|
| 6 |
|
| 7 |
BACKEND_SPACE = "CleanSong-AI/Main-tool-backend-main"
|
| 8 |
|
| 9 |
+
# --- Email sender (example with SMTP) ---
|
| 10 |
+
SMTP_SERVER = os.getenv("SMTP_SERVER") # e.g., smtp.gmail.com
|
| 11 |
+
SMTP_PORT = int(os.getenv("SMTP_PORT", 587))
|
| 12 |
+
SMTP_USER = os.getenv("SMTP_USER")
|
| 13 |
+
SMTP_PASS = os.getenv("SMTP_PASS")
|
| 14 |
+
|
| 15 |
+
def send_email(to_email, file_path):
|
| 16 |
+
msg = EmailMessage()
|
| 17 |
+
msg['Subject'] = "Your CleanSong AI track is ready!"
|
| 18 |
+
msg['From'] = SMTP_USER
|
| 19 |
+
msg['To'] = to_email
|
| 20 |
+
msg.set_content("Hi! Your cleaned track is ready. See the attachment!")
|
| 21 |
+
|
| 22 |
+
with open(file_path, 'rb') as f:
|
| 23 |
+
file_data = f.read()
|
| 24 |
+
file_name = os.path.basename(file_path)
|
| 25 |
+
|
| 26 |
+
msg.add_attachment(file_data, maintype='audio', subtype='mpeg', filename=file_name)
|
| 27 |
+
|
| 28 |
+
with smtplib.SMTP(SMTP_SERVER, SMTP_PORT) as server:
|
| 29 |
+
server.starttls()
|
| 30 |
+
server.login(SMTP_USER, SMTP_PASS)
|
| 31 |
+
server.send_message(msg)
|
| 32 |
+
|
| 33 |
+
# --- Main processing function ---
|
| 34 |
+
def clean_song(file_path, email=None):
|
| 35 |
if file_path is None:
|
| 36 |
return None
|
| 37 |
|
| 38 |
+
# Send file to backend
|
| 39 |
client = Client(BACKEND_SPACE)
|
|
|
|
| 40 |
result = client.predict(
|
| 41 |
+
handle_file(file_path),
|
| 42 |
api_name="/predict"
|
| 43 |
)
|
| 44 |
|
| 45 |
+
# Normalize HF return
|
| 46 |
if isinstance(result, (list, tuple)):
|
| 47 |
result = result[0]
|
|
|
|
| 48 |
if isinstance(result, dict) and "name" in result:
|
| 49 |
result = result["name"]
|
| 50 |
|
| 51 |
+
# Send email if provided
|
| 52 |
+
if email:
|
| 53 |
+
try:
|
| 54 |
+
send_email(email, result)
|
| 55 |
+
except Exception as e:
|
| 56 |
+
print("Failed to send email:", e)
|
| 57 |
+
|
| 58 |
return result
|
| 59 |
|
| 60 |
# ------------------------------------------------------------
|
|
|
|
| 62 |
# ------------------------------------------------------------
|
| 63 |
iface = gr.Interface(
|
| 64 |
fn=clean_song,
|
| 65 |
+
inputs=[
|
| 66 |
+
gr.Audio(type="filepath", label="Upload Song"),
|
| 67 |
+
gr.Textbox(label="Email (optional)", placeholder="Enter your email to get notified")
|
| 68 |
+
],
|
| 69 |
outputs=gr.Audio(type="filepath", label="Cleaned Song"),
|
| 70 |
title="CleanSong AI - Prototype Frontend",
|
| 71 |
+
description="Upload a song and get a cleaned version. Optionally enter your email to receive the cleaned track."
|
| 72 |
)
|
| 73 |
|
| 74 |
if __name__ == "__main__":
|
| 75 |
+
iface.launch()
|