Upload_Cover / lap.py
haraberget's picture
Upload lap.py
3c2bfd0 verified
import requests
import json
import time
import re
import gradio as gr
# ==========================================
# Helper: Make URLs clickable in Markdown/HTML
# ==========================================
def make_clickable_links(text):
"""Convert URLs in a string to HTML clickable links."""
if not isinstance(text, str):
return text
url_pattern = r'(https?://[^\s]+)'
return re.sub(url_pattern, r'<a href="\1" target="_blank" rel="noopener noreferrer">\1</a>', text)
# ==========================================
# API configuration (adjust to your real endpoint)
# ==========================================
API_BASE_URL = "https://your-api-endpoint.com" # <-- CHANGE THIS
API_KEY = "YOUR_API_KEY" # <-- CHANGE IF NEEDED
headers = {
"Content-Type": "application/json",
# "Authorization": f"Bearer {API_KEY}" # Uncomment if needed
}
# ==========================================
# Function 1: Generate a cover (starts a task)
# ==========================================
def generate_cover(prompt, style, title, callback_url):
"""Call the API to start generating a cover."""
payload = {
"prompt": prompt,
"style": style,
"title": title,
"callBackUrl": callback_url,
# other default parameters from your original JSON
"audioWeight": 0.65,
"customMode": True,
"instrumental": False,
"model": "V5_5",
"negativeTags": "Distorted, Low Quality",
"styleWeight": 0.65,
"vocalGender": "none",
"weirdnessConstraint": 0.65
}
try:
response = requests.post(f"{API_BASE_URL}/generate", json=payload, headers=headers)
response.raise_for_status()
result = response.json()
# Format the JSON and make links clickable
response_json = json.dumps(result, indent=2)
clickable_response = make_clickable_links(response_json)
return f"""✅ **Generation Started!**
**Full Response:**
```json
{clickable_response}
```"""
except Exception as e:
return f"❌ Error: {str(e)}"
# ==========================================
# Function 2: Get task info (check status)
# ==========================================
def get_task_info(task_id):
"""Retrieve task information by task_id."""
try:
response = requests.get(f"{API_BASE_URL}/task/{task_id}", headers=headers)
response.raise_for_status()
data = response.json()
# Format the raw JSON and make links clickable
clickable_raw_json = make_clickable_links(json.dumps(data, indent=2))
output = f"## Task Status: {data.get('status', 'UNKNOWN')}\n"
output += f"\n## Raw Response\n```json\n{clickable_raw_json}\n```"
return output
except Exception as e:
return f"❌ Error: {str(e)}"
# ==========================================
# Gradio Interface
# ==========================================
with gr.Blocks(title="Cover Upload & Generation") as demo:
gr.Markdown("# 🎵 Cover Song Generator")
gr.Markdown("Generate AI covers with clickable links in the JSON output.")
with gr.Tab("Generate Cover"):
prompt_text = gr.Textbox(label="Prompt", lines=5, placeholder="Enter your song prompt...")
style_text = gr.Textbox(label="Style", placeholder="e.g., folk, acoustic ballad, slow thoughtful")
title_text = gr.Textbox(label="Title", placeholder="Song title")
callback_url_text = gr.Textbox(label="Callback URL", placeholder="https://1hit.no/cover/cb.php")
generate_btn = gr.Button("Generate")
output_gen = gr.Markdown(label="Response")
generate_btn.click(
fn=generate_cover,
inputs=[prompt_text, style_text, title_text, callback_url_text],
outputs=output_gen
)
with gr.Tab("Check Task Status"):
task_id_input = gr.Textbox(label="Task ID", placeholder="e.g., 629f9fe62409f2b997266f7f7325e759")
status_btn = gr.Button("Get Status")
output_status = gr.Markdown(label="Task Info")
status_btn.click(
fn=get_task_info,
inputs=task_id_input,
outputs=output_status
)
gr.Markdown("---\n💡 **Note:** All URLs in the JSON responses are now clickable. Just click on any `https://` link to open it in a new tab.")
# ==========================================
# Launch the app
# ==========================================
if __name__ == "__main__":
demo.launch()