Spaces:
Running
Running
adding the removed code for song push to wordpress
#3
by
mubashiralli
- opened
app.py
CHANGED
@@ -13,6 +13,14 @@ from io import BytesIO
|
|
13 |
import soundfile as sf
|
14 |
from datetime import datetime
|
15 |
from tempfile import NamedTemporaryFile
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
|
17 |
# Try to get API_URL from environment variables, if not found set to a default value
|
18 |
try:
|
@@ -28,6 +36,84 @@ except KeyError:
|
|
28 |
st.error("BEARER_TOKEN environment variable is not set.")
|
29 |
st.stop()
|
30 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
headers = {
|
32 |
"Authorization": f"Bearer {BEARER_TOKEN}",
|
33 |
"Content-Type": "application/json"
|
@@ -126,9 +212,13 @@ if st.button("Generate Audio") and genre and energy_level and description and te
|
|
126 |
st_state.audio_pydub_sample_rate = audio_pydub.frame_rate
|
127 |
st_state.audio = np.array(output[0]).astype(np.float32)
|
128 |
sample_rate = eval(response.content)[0]['sample_rate']
|
|
|
129 |
st_state.audio_sample_rate = sample_rate
|
130 |
st_state.augmented_audio_pydub = st_state.audio_pydub
|
131 |
st.audio(st_state.audio_pydub.export().read())
|
|
|
|
|
|
|
132 |
|
133 |
# Post-processing options
|
134 |
st.header("Post-processing Options")
|
|
|
13 |
import soundfile as sf
|
14 |
from datetime import datetime
|
15 |
from tempfile import NamedTemporaryFile
|
16 |
+
from wordpress_xmlrpc import Client
|
17 |
+
from wordpress_xmlrpc.methods import media
|
18 |
+
from xmlrpc.client import Binary
|
19 |
+
from woocommerce import API
|
20 |
+
from wordpress_xmlrpc.compat import xmlrpc_client
|
21 |
+
from io import BytesIO
|
22 |
+
import soundfile as sf
|
23 |
+
import uuid
|
24 |
|
25 |
# Try to get API_URL from environment variables, if not found set to a default value
|
26 |
try:
|
|
|
36 |
st.error("BEARER_TOKEN environment variable is not set.")
|
37 |
st.stop()
|
38 |
|
39 |
+
def save_to_wordpress(audio, sample_rate):
|
40 |
+
# Define your WordPress site URL and authentication credentials
|
41 |
+
wordpress_url = 'https://songlabai.com/xmlrpc.php'
|
42 |
+
woocommerce_url = 'https://songlabai.com'
|
43 |
+
consumer_key = 'ck_93d516ba12289a6fd0eced56bbc0b05ecbf98735'
|
44 |
+
consumer_secret = 'cs_9d5eb716d631db408a4c47796b5d18b0313d8559'
|
45 |
+
username = 'admin_h2ibbgql'
|
46 |
+
password = 'um^VdaNK0H8Vw2*KNJlYABkh'
|
47 |
+
|
48 |
+
# Authenticate with WordPress XML-RPC API
|
49 |
+
wav_bytes = BytesIO()
|
50 |
+
sf.write(wav_bytes, audio, samplerate=sample_rate, format='WAV')
|
51 |
+
title = f"generated_audio_{datetime.now().timestamp()}.wav"
|
52 |
+
file_data = {
|
53 |
+
'name': title,
|
54 |
+
'type': 'audio/x-wav', # Change the MIME type according to your file type
|
55 |
+
'bits': xmlrpc_client.Binary(wav_bytes.getvalue()),
|
56 |
+
}
|
57 |
+
wp_client = Client(wordpress_url, username, password)
|
58 |
+
for _ in range(4):
|
59 |
+
try:
|
60 |
+
# Upload the file to WordPress Media Library
|
61 |
+
|
62 |
+
media_response = wp_client.call(media.UploadFile(file_data))
|
63 |
+
|
64 |
+
# Handle the response
|
65 |
+
if media_response:
|
66 |
+
print("File successfully uploaded to WordPress with attachment ID:", media_response)
|
67 |
+
|
68 |
+
# Create product data for WooCommerce
|
69 |
+
product_data = {
|
70 |
+
'status':'pending',
|
71 |
+
'name': title,
|
72 |
+
'type': 'simple',
|
73 |
+
'regular_price': '1.00', # Set the price as needed
|
74 |
+
'sku': str(uuid.uuid4()),
|
75 |
+
'downloadable': True,
|
76 |
+
'download_limit': -1,
|
77 |
+
'download_expiry': -1,
|
78 |
+
}
|
79 |
+
|
80 |
+
# Authenticate with WooCommerce API
|
81 |
+
wc_api = API(
|
82 |
+
url=woocommerce_url,
|
83 |
+
consumer_key=consumer_key,
|
84 |
+
consumer_secret=consumer_secret,
|
85 |
+
version="wc/v3"
|
86 |
+
)
|
87 |
+
|
88 |
+
# Create the product
|
89 |
+
response = wc_api.post("products", product_data)
|
90 |
+
|
91 |
+
# Handle the response
|
92 |
+
if response.status_code == 201:
|
93 |
+
print("Product successfully created in WooCommerce:", response.json())
|
94 |
+
# Update product to add downloadable file URL
|
95 |
+
product_update_data = {
|
96 |
+
'downloads': [{
|
97 |
+
'name': media_response['title'],
|
98 |
+
'file': media_response['link']
|
99 |
+
}]
|
100 |
+
}
|
101 |
+
product_id = response.json().get('id')
|
102 |
+
response = wc_api.put(f"products/{product_id}", product_update_data)
|
103 |
+
|
104 |
+
if response.status_code == 200:
|
105 |
+
print("Downloadable file URL added to product:", response.json())
|
106 |
+
return response.json()['permalink'], response.json()['permalink'].split('p=')[-1]
|
107 |
+
else:
|
108 |
+
print("Error adding downloadable file URL to product:", response.text)
|
109 |
+
else:
|
110 |
+
print("Error creating product in WooCommerce:", response.text)
|
111 |
+
else:
|
112 |
+
print("Error uploading file to WordPress.")
|
113 |
+
break
|
114 |
+
except Exception as e:
|
115 |
+
print("Error:", e)
|
116 |
+
|
117 |
headers = {
|
118 |
"Authorization": f"Bearer {BEARER_TOKEN}",
|
119 |
"Content-Type": "application/json"
|
|
|
212 |
st_state.audio_pydub_sample_rate = audio_pydub.frame_rate
|
213 |
st_state.audio = np.array(output[0]).astype(np.float32)
|
214 |
sample_rate = eval(response.content)[0]['sample_rate']
|
215 |
+
perm_link, product_code = save_to_wordpress(np.array(output[0]).astype(np.float32), sample_rate)
|
216 |
st_state.audio_sample_rate = sample_rate
|
217 |
st_state.augmented_audio_pydub = st_state.audio_pydub
|
218 |
st.audio(st_state.audio_pydub.export().read())
|
219 |
+
st.write(f"To download use the following product code: {product_code}\nTo Publish, please contact the admin by sending the following link: {perm_link}")
|
220 |
+
st.link_button("Contact Us", "https://songlabai.com/subcribe/")
|
221 |
+
st.link_button("Download Song", "https://songlabai.com/download-music/")
|
222 |
|
223 |
# Post-processing options
|
224 |
st.header("Post-processing Options")
|