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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +131 -63
app.py CHANGED
@@ -1,5 +1,6 @@
1
  import streamlit as st
2
  import json
 
3
  from datetime import datetime
4
 
5
  # Page configuration
@@ -12,26 +13,37 @@ st.set_page_config(
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")
@@ -39,71 +51,127 @@ audio_id = st.text_input("Audio ID", value="e231****-****-****-****-****8cadc7dc
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")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import streamlit as st
2
  import json
3
+ import requests
4
  from datetime import datetime
5
 
6
  # Page configuration
 
13
  # Title
14
  st.title("🎵 Suno API Audio Generator")
15
 
16
+ # Load secrets from Hugging Face Space - using YOUR exact secret names
17
+ SUNO_API_KEY = st.secrets.get("SunoKey", "")
18
+ CALLBACK_URL = st.secrets.get("CallbackURL", "")
19
+
20
+ # Debug: Show what secrets were found
21
+ st.write("### 🔍 Debug Info")
22
+ st.write(f"Secrets found: {list(st.secrets.keys())}")
23
+ st.write(f"SunoKey loaded: {'✅ Yes' if SUNO_API_KEY else '❌ No'}")
24
+ st.write(f"CallbackURL loaded: {'✅ Yes' if CALLBACK_URL else '❌ No'}")
25
 
26
  # Show secret status
27
  if SUNO_API_KEY and CALLBACK_URL:
28
+ st.success("✅ Secrets loaded successfully from Hugging Face!")
29
+ st.info(f"Loaded **SunoKey** and **CallbackURL** secrets")
30
  else:
31
+ st.error("❌ Secrets not loaded correctly")
32
  st.info("""
33
+ **Make sure you've added these secrets in Hugging Face Space:**
34
  1. Go to **Settings** → **Repository secrets**
35
  2. Add:
36
+ - **Key:** `SunoKey` (exact name)
37
+ - **Value:** Your Suno API Bearer token
38
+
39
+ 3. Add:
40
+ - **Key:** `CallbackURL` (exact name)
41
+ - **Value:** Your callback URL
42
  """)
43
 
44
  # API Form
45
  st.markdown("---")
46
+ st.markdown("### 🎛️ API Parameters")
47
 
48
  task_id = st.text_input("Task ID", value="5c79****be8e")
49
  audio_id = st.text_input("Audio ID", value="e231****-****-****-****-****8cadc7dc")
 
51
  # Make API call button
52
  if st.button("🎵 Generate Audio", type="primary", use_container_width=True):
53
  if not SUNO_API_KEY or not CALLBACK_URL:
54
+ st.error("Please configure SunoKey and CallbackURL in Hugging Face Space settings")
55
  elif not task_id or not audio_id:
56
+ st.error("Please enter both Task ID and Audio ID")
57
  else:
58
+ with st.spinner("Making API call to Suno..."):
59
+ try:
60
+ # Prepare the API call using YOUR secrets
61
+ headers = {
62
+ "Authorization": f"Bearer {SUNO_API_KEY}",
63
+ "Content-Type": "application/json"
64
+ }
65
+
66
+ data = {
67
+ "taskId": task_id,
68
+ "audioId": audio_id,
69
+ "callBackUrl": CALLBACK_URL
70
+ }
71
+
72
+ # Show what we're sending
73
+ with st.expander("📡 Request Details", expanded=True):
74
+ st.code(f"""
75
  POST https://api.sunoapi.org/api/v1/wav/generate
76
 
77
  Headers:
78
+ {{
79
+ "Authorization": "Bearer {SUNO_API_KEY[:10]}...{SUNO_API_KEY[-10:] if len(SUNO_API_KEY) > 20 else '***'}",
80
+ "Content-Type": "application/json"
81
+ }}
82
 
83
  Body:
84
  {json.dumps(data, indent=2)}
85
+ """)
86
+
87
+ # Make the actual API call
88
+ response = requests.post(
89
+ "https://api.sunoapi.org/api/v1/wav/generate",
90
+ headers=headers,
91
+ json=data,
92
+ timeout=30
93
+ )
94
+
95
+ # Check response
96
+ if response.status_code == 200:
97
+ result = response.json()
98
+ st.success(f"✅ Success! Status: {response.status_code}")
99
+
100
+ # Display response
101
+ st.markdown("### 📋 API Response")
102
+ st.json(result)
103
+
104
+ # Download button
105
+ timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
106
+ st.download_button(
107
+ label="📥 Download JSON Response",
108
+ data=json.dumps(result, indent=2),
109
+ file_name=f"suno_response_{timestamp}.json",
110
+ mime="application/json"
111
+ )
112
+
113
+ # Show useful info
114
+ if "jobId" in result:
115
+ st.info(f"**Job ID:** `{result['jobId']}`")
116
+ if "message" in result:
117
+ st.info(f"**Message:** {result['message']}")
118
+
119
+ else:
120
+ st.error(f"❌ API Error: {response.status_code}")
121
+ try:
122
+ error_details = response.json()
123
+ st.json(error_details)
124
+ except:
125
+ st.text(f"Response: {response.text}")
126
+
127
+ except requests.exceptions.Timeout:
128
+ st.error("⏰ Request timed out after 30 seconds")
129
+ except requests.exceptions.ConnectionError:
130
+ st.error("🔌 Connection error - check your internet connection")
131
+ except requests.exceptions.RequestException as e:
132
+ st.error(f"⚠️ Request failed: {str(e)}")
133
+ except Exception as e:
134
+ st.error(f"❌ Unexpected error: {str(e)}")
135
 
136
+ # Secret info section
137
+ st.markdown("---")
138
+ with st.expander("🔐 Secret Information"):
139
  if SUNO_API_KEY:
140
+ # Show masked key
141
+ key_length = len(SUNO_API_KEY)
142
+ if key_length > 20:
143
+ masked = f"{SUNO_API_KEY[:10]}...{SUNO_API_KEY[-10:]}"
144
+ elif key_length > 8:
145
+ masked = f"{SUNO_API_KEY[:4]}...{SUNO_API_KEY[-4:]}"
146
+ else:
147
+ masked = "••••••••"
148
+
149
+ st.write(f"**SunoKey (API Key):** `{masked}`")
150
+ st.write(f"**Key Length:** {key_length} characters")
151
  else:
152
+ st.write("**SunoKey:** Not loaded")
153
 
154
  if CALLBACK_URL:
155
+ st.write(f"**CallbackURL:** `{CALLBACK_URL}`")
156
  else:
157
+ st.write("**CallbackURL:** Not loaded")
158
+
159
+ st.write("---")
160
+ st.write("**Note:** Secrets are securely loaded from Hugging Face Space settings.")
161
+
162
+ # Instructions
163
+ st.markdown("---")
164
+ st.markdown("### 📝 How to Use")
165
+ st.markdown("""
166
+ 1. **Configure Secrets** in Hugging Face Space settings:
167
+ - Add `SunoKey` with your API Bearer token
168
+ - Add `CallbackURL` with your callback endpoint
169
+
170
+ 2. **Enter API Parameters**:
171
+ - Task ID from your Suno account
172
+ - Audio ID from your Suno account
173
+
174
+ 3. **Click "Generate Audio"** to make the API call
175
+
176
+ 4. **View Response** and download the JSON result
177
+ """)