MySafeCode commited on
Commit
361fa27
·
verified ·
1 Parent(s): 16dd2cd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +88 -253
app.py CHANGED
@@ -1,274 +1,109 @@
1
  import streamlit as st
2
- import requests
3
  import json
4
- import os
5
  from datetime import datetime
6
 
7
  # Page configuration
8
  st.set_page_config(
9
- page_title="🎵 Suno AI Audio Generator",
10
  page_icon="🎵",
11
- layout="wide",
12
- initial_sidebar_state="expanded"
13
  )
14
 
15
- # Custom CSS
16
- st.markdown("""
17
- <style>
18
- .stApp {
19
- background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
20
- }
21
- .api-card {
22
- background: rgba(255, 255, 255, 0.95);
23
- backdrop-filter: blur(10px);
24
- border: 1px solid rgba(255, 255, 255, 0.2);
25
- box-shadow: 0 8px 32px 0 rgba(31, 38, 135, 0.37);
26
- }
27
- .secret-mask {
28
- font-family: 'Monaco', 'Menlo', 'Ubuntu Mono', monospace;
29
- background: #f3f4f6;
30
- padding: 2px 6px;
31
- border-radius: 4px;
32
- font-size: 12px;
33
- color: #374151;
34
- }
35
- .success-badge {
36
- background: #d1fae5;
37
- color: #065f46;
38
- border-radius: 9999px;
39
- padding: 4px 12px;
40
- display: inline-flex;
41
- align-items: center;
42
- font-size: 12px;
43
- margin: 4px;
44
- }
45
- .error-badge {
46
- background: #fee2e2;
47
- color: #7c2d1a;
48
- border-radius: 9999px;
49
- padding: 4px 12px;
50
- display: inline-flex;
51
- align-items: center;
52
- font-size: 12px;
53
- margin: 4px;
54
- }
55
- </style>
56
- """, unsafe_allow_html=True)
57
 
58
- # Main App
59
- def main():
60
- # Header
61
- st.title("🎵 Suno AI Audio Generator")
62
- st.markdown("Generate audio using Suno API with secure secret management")
63
-
64
- # Initialize session state
65
- if 'secrets_loaded' not in st.session_state:
66
- st.session_state.secrets_loaded = False
67
- st.session_state.suno_api_key = None
68
- st.session_state.callback_url = None
69
- st.session_state.last_api_response = None
70
-
71
- # Sidebar for configuration
72
- with st.sidebar:
73
- st.header("⚙️ Configuration")
74
- st.markdown("---")
75
-
76
- # Secret Loading Section
77
- st.subheader("🔐 Load Secrets")
78
-
79
- # Load secrets from environment variables
80
- suno_api_key = st.secrets.get("SUNO_API_KEY", "").strip() or os.environ.get("SUNO_API_KEY", "")
81
- callback_url = st.secrets.get("CALLBACK_URL", "").strip() or os.environ.get("CALLBACK_URL", "")
82
-
83
- # Show loaded secrets status
84
- if suno_api_key and callback_url:
85
- st.success("Secrets loaded!")
86
-
87
- # Display masked secrets
88
- with st.expander("🔐 Loaded Secrets", expanded=True):
89
- st.markdown(f"""
90
- **API Key Status:** ✅ Loaded
91
- **Callback URL:** ✅ Loaded
92
-
93
- **Usage:** API calls will automatically use these secrets.
94
- """)
95
-
96
- st.session_state.suno_api_key = suno_api_key
97
- st.session_state.callback_url = callback_url
98
- st.session_state.secrets_loaded = True
99
- else:
100
- st.warning("Secrets not configured")
101
- st.markdown("""
102
- **To configure:**
103
- 1. Set **SUNO_API_KEY** in `.streamlit/secrets.toml`
104
- 2. Set **CALLBACK_URL** in `.streamlit/secrets.toml`
105
- """)
106
-
107
- st.markdown("---")
108
-
109
- # Manual input for testing
110
- st.subheader("🔧 Manual Override (Testing)")
111
- manual_api_key = st.text_input("Enter API Key:", type="password")
112
- manual_callback_url = st.text_input("Enter Callback URL:", value=callback_url if callback_url else "")
113
-
114
- if st.button("🔧 Use Manual Credentials"):
115
- if manual_api_key and manual_callback_url:
116
- st.session_state.suno_api_key = manual_api_key
117
- st.session_state.callback_url = manual_callback_url
118
- st.session_state.secrets_loaded = True
119
- st.success("Using manual credentials")
120
- st.rerun()
121
-
122
- # Main content area
123
- col1, col2 = st[3, 1]
124
-
125
- with col1:
126
- st.markdown("### 🎛️ API Parameters")
127
-
128
- # Task ID input
129
- task_id = st.text_input(
130
- "Task ID",
131
- value="5c79****be8e",
132
- help="Enter your Suno API task ID"
133
- )
134
-
135
- # Audio ID input
136
- audio_id = st.text_input(
137
- "Audio ID",
138
- value="e231****-****-****-****-****8cadc7dc",
139
- help="Enter your Suno API audio ID"
140
- )
141
-
142
- # API Endpoint selection
143
- endpoint = st.selectbox(
144
- "API Endpoint",
145
- options=["/api/v1/wav/generate", "/api/v1/audio/sync", "/api/v1/audio/status"],
146
- help="Select the Suno API endpoint to use"
147
- )
148
-
149
- # Advanced options
150
- with st.expander("🔧 Advanced Options"):
151
- timeout = st.number_input("Timeout (seconds)", min_value=5, max_value=60, value=30, step=5)
152
- retry_count = st.slider("Retry Count", 0, 3, 1)
153
- show_debug = st.checkbox("Show Debug Info")
154
-
155
- with col2:
156
- st.markdown("### 📊 Quick Actions")
157
- if st.button("🚀 Test API Connection"):
158
- test_api_connection()
159
-
160
- if st.button("🧪 Test with Sample Data"):
161
- test_with_sample_data()
162
-
163
- if st.button("📋 Copy Configuration"):
164
- copy_configuration()
165
-
166
- # API Call Section
167
- st.markdown("---")
168
- st.markdown("### 🚀 Make API Call")
169
-
170
- # Make API call button
171
- if st.button("🎵 Generate Audio", key="generate_audio", type="primary", use_container_width=True):
172
- make_api_call(task_id, audio_id, st.secrets.get("SUNO_API_KEY", manual_api_key if manual_api_key else suno_api_key), callback_url if callback_url else manual_callback_url)
173
-
174
- # Results section
175
- st.markdown("---")
176
- if 'last_api_response' in st.session_state:
177
- st.markdown("### 📋 Last API Response")
178
- st.json(st.session_state.last_api_response)
179
-
180
- # Download JSON button
181
- st.download_button(
182
- label="📥 Download Response JSON",
183
- data=json.dumps(st.session_state.last_api_response, indent=2),
184
- file_name=f"suno_response_{datetime.now().strftime('%Y%m%d_%H%M%S')}.json",
185
- mime="application/json"
186
- )
187
 
188
- # Test API Connection
189
- def test_api_connection():
190
- with st.spinner("Testing API connection..."):
191
- # Implement API connection test
192
- try:
193
- test_response = {"status": "connected"}
194
- st.success("✅ API connection successful!")
195
- st.json(test_response)
196
- except Exception as e:
197
- st.error(f"❌ API connection failed: {str(e)}")
 
 
198
 
199
- # Test with Sample Data
200
- def test_with_sample_data():
201
- sample_data = {
202
- "taskId": "5c79****be8e",
203
- "audioId": "e231****-****-****-****-****8cadc7dc",
204
- "callBackUrl": st.session_state.callback_url
205
- }
206
-
207
- st.info("🧪 Testing with sample data...")
208
- st.json(sample_data)
209
 
210
- # Copy Configuration
211
- def copy_configuration():
212
- config = {
213
- "api_key": st.session_state.suno_api_key,
214
- "callback_url": st.session_state.callback_url,
215
- "task_id": st.session_state.get("task_id", ""),
216
- "audio_id": st.session_state.get("audio_id", "")
217
- }
218
-
219
- st.code(json.dumps(config, indent=2), language="json")
220
- st.success("📋 Configuration copied to clipboard!")
221
 
222
- # Make API Call Function
223
- def make_api_call(task_id, audio_id, api_key, callback_url):
224
- with st.spinner("🎵 Generating audio..."):
225
- # Prepare the API call
226
- headers = {
227
- "Authorization": f"Bearer {api_key}",
228
- "Content-Type": "application/json"
229
- }
230
-
231
- data = {
232
- "taskId": task_id,
233
- "audioId": audio_id,
234
- "callBackUrl": callback_url
235
- }
236
-
237
- # Show request details
238
- with st.expander("📡 Request Details", expanded=False):
239
- st.markdown(f"""
240
- **Endpoint:** `https://api.sunoapi.org/api/v1/wav/generate`
241
- **Headers:** ```python
242
- {json.dumps(headers, indent=2)}
243
- ```
244
- **Body:** ```json
245
- {json.dumps(data, indent=2)}
246
- ```
247
- """)
248
-
249
- # Make the API call
250
- try:
251
- #response = requests.post("https://api.sunoapi.org/api/v1/wav/generate", headers=headers, json=data)
252
- #st.session_state.last_api_response = response.json()
253
 
254
- # For demo purposes, return a mock response
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
255
  mock_response = {
256
  "status": "success",
257
- "jobId": "generated_123456789",
258
- "estimatedCompletion": "2026-01-17T20:45:00Z",
259
- "message": "Audio generation started successfully"
260
  }
261
 
262
- st.session_state.last_api_response = mock_response
263
- st.success("✅ Audio generation started successfully!")
264
 
265
- # Show job info
266
- st.info(f"**Job ID:** {mock_response['jobId']}")
267
- st.info(f"**Estimated Completion:** {mock_response['estimatedCompletion']}")
268
-
269
- except Exception as e:
270
- st.error(f"❌ API call failed: {str(e)}")
 
271
 
272
- # Run the main function
273
- if __name__ == "__main__":
274
- main()
 
 
 
 
 
 
 
 
 
 
1
  import streamlit as st
 
2
  import json
 
3
  from datetime import datetime
4
 
5
  # Page configuration
6
  st.set_page_config(
7
+ page_title="Suno API Generator",
8
  page_icon="🎵",
9
+ layout="centered"
 
10
  )
11
 
12
+ # Title
13
+ st.title("🎵 Suno API Audio Generator")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
 
15
+ # Load secrets from Hugging Face Space
16
+ SUNO_API_KEY = st.secrets.get("SUNO_API_KEY", "")
17
+ CALLBACK_URL = st.secrets.get("CALLBACK_URL", "")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
 
19
+ # Show secret status
20
+ if SUNO_API_KEY and CALLBACK_URL:
21
+ st.success(" Secrets loaded from Hugging Face!")
22
+ else:
23
+ st.error("❌ Secrets not configured in Hugging Face Space")
24
+ st.info("""
25
+ Add these secrets in your Hugging Face Space settings:
26
+ 1. Go to **Settings** → **Repository secrets**
27
+ 2. Add:
28
+ - `SUNO_API_KEY`: Your Suno API Bearer token
29
+ - `CALLBACK_URL`: Your callback URL
30
+ """)
31
 
32
+ # API Form
33
+ st.markdown("---")
34
+ st.markdown("### API Parameters")
 
 
 
 
 
 
 
35
 
36
+ task_id = st.text_input("Task ID", value="5c79****be8e")
37
+ audio_id = st.text_input("Audio ID", value="e231****-****-****-****-****8cadc7dc")
 
 
 
 
 
 
 
 
 
38
 
39
+ # Make API call button
40
+ if st.button("🎵 Generate Audio", type="primary", use_container_width=True):
41
+ if not SUNO_API_KEY or not CALLBACK_URL:
42
+ st.error("Please configure secrets in Hugging Face Space settings")
43
+ elif not task_id or not audio_id:
44
+ st.error("Please enter Task ID and Audio ID")
45
+ else:
46
+ with st.spinner("Making API call..."):
47
+ # Prepare the API call
48
+ headers = {
49
+ "Authorization": f"Bearer {SUNO_API_KEY}",
50
+ "Content-Type": "application/json"
51
+ }
52
+
53
+ data = {
54
+ "taskId": task_id,
55
+ "audioId": audio_id,
56
+ "callBackUrl": CALLBACK_URL
57
+ }
 
 
 
 
 
 
 
 
 
 
 
 
58
 
59
+ # Show request info
60
+ with st.expander("📡 Request Details"):
61
+ st.code(f"""
62
+ POST https://api.sunoapi.org/api/v1/wav/generate
63
+
64
+ Headers:
65
+ {json.dumps(headers, indent=2)}
66
+
67
+ Body:
68
+ {json.dumps(data, indent=2)}
69
+ """)
70
+
71
+ # Note: In production, you would make the actual API call:
72
+ # import requests
73
+ # response = requests.post(
74
+ # "https://api.sunoapi.org/api/v1/wav/generate",
75
+ # headers=headers,
76
+ # json=data
77
+ # )
78
+
79
+ # For now, show a mock response
80
  mock_response = {
81
  "status": "success",
82
+ "jobId": f"job_{datetime.now().strftime('%Y%m%d%H%M%S')}",
83
+ "message": "Audio generation started",
84
+ "estimatedCompletion": "2026-01-18T10:30:00Z"
85
  }
86
 
87
+ st.success("✅ API call successful!")
88
+ st.json(mock_response)
89
 
90
+ # Download button
91
+ st.download_button(
92
+ label="📥 Download Response",
93
+ data=json.dumps(mock_response, indent=2),
94
+ file_name=f"response_{datetime.now().strftime('%Y%m%d_%H%M%S')}.json",
95
+ mime="application/json"
96
+ )
97
 
98
+ # Secret info (hidden by default)
99
+ with st.expander("🔐 Secret Info"):
100
+ if SUNO_API_KEY:
101
+ masked_key = f"{SUNO_API_KEY[:4]}...{SUNO_API_KEY[-4:]}" if len(SUNO_API_KEY) > 8 else "••••••••"
102
+ st.write(f"**API Key:** `{masked_key}`")
103
+ else:
104
+ st.write("**API Key:** Not set")
105
+
106
+ if CALLBACK_URL:
107
+ st.write(f"**Callback URL:** `{CALLBACK_URL}`")
108
+ else:
109
+ st.write("**Callback URL:** Not set")