File size: 4,486 Bytes
3c2bfd0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
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()