Spaces:
Running
Running
#!/usr/bin/env python3 | |
""" | |
Minimal A1D MCP Server with Gradio - Ultra-simple version to avoid all compatibility issues | |
""" | |
import gradio as gr | |
import os | |
from utils import A1DAPIClient, prepare_request_data, format_response_with_preview | |
from config import TOOLS_CONFIG | |
def get_api_client(): | |
"""Get API client with current API key""" | |
api_key = os.getenv("A1D_API_KEY") | |
if not api_key: | |
raise ValueError("A1D_API_KEY environment variable is required") | |
return A1DAPIClient(api_key) | |
def process_remove_bg(image_url): | |
"""Remove background from images using AI""" | |
try: | |
if not image_url or not image_url.strip(): | |
return "β Error: Please provide an image URL" | |
client = get_api_client() | |
data = prepare_request_data("remove_bg", image_url=image_url) | |
response = client.make_request_with_result( | |
TOOLS_CONFIG["remove_bg"]["api_endpoint"], | |
data, | |
timeout=120 | |
) | |
message, media_url = format_response_with_preview(response, "remove_bg") | |
return f"{message}\n\nResult URL: {media_url}" if media_url else message | |
except Exception as e: | |
return f"β Error: {str(e)}" | |
def process_upscale(image_url, scale): | |
"""Upscale images using AI""" | |
try: | |
if not image_url or not image_url.strip(): | |
return "β Error: Please provide an image URL" | |
client = get_api_client() | |
data = prepare_request_data("image_upscaler", image_url=image_url, scale=int(scale)) | |
response = client.make_request_with_result( | |
TOOLS_CONFIG["image_upscaler"]["api_endpoint"], | |
data, | |
timeout=120 | |
) | |
message, media_url = format_response_with_preview(response, "image_upscaler") | |
return f"{message}\n\nResult URL: {media_url}" if media_url else message | |
except Exception as e: | |
return f"β Error: {str(e)}" | |
def process_generate(prompt): | |
"""Generate images using AI from text prompts""" | |
try: | |
if not prompt or not prompt.strip(): | |
return "β Error: Please provide a text prompt" | |
client = get_api_client() | |
data = prepare_request_data("image_generator", prompt=prompt) | |
response = client.make_request_with_result( | |
TOOLS_CONFIG["image_generator"]["api_endpoint"], | |
data, | |
timeout=120 | |
) | |
message, media_url = format_response_with_preview(response, "image_generator") | |
return f"{message}\n\nResult URL: {media_url}" if media_url else message | |
except Exception as e: | |
return f"β Error: {str(e)}" | |
# Create the minimal Gradio interface | |
def create_minimal_interface(): | |
"""Create minimal interface with basic components only""" | |
# Background Removal Interface | |
bg_interface = gr.Interface( | |
fn=process_remove_bg, | |
inputs=gr.Textbox(label="Image URL", placeholder="https://example.com/image.jpg"), | |
outputs=gr.Textbox(label="Result"), | |
title="π Background Removal", | |
description="Remove background from images using AI" | |
) | |
# Image Upscaler Interface | |
upscale_interface = gr.Interface( | |
fn=process_upscale, | |
inputs=[ | |
gr.Textbox(label="Image URL", placeholder="https://example.com/image.jpg"), | |
gr.Radio(choices=["2", "4", "8", "16"], value="2", label="Scale Factor") | |
], | |
outputs=gr.Textbox(label="Result"), | |
title="π Image Upscaler", | |
description="Upscale images using AI" | |
) | |
# Image Generator Interface | |
gen_interface = gr.Interface( | |
fn=process_generate, | |
inputs=gr.Textbox(label="Text Prompt", placeholder="A beautiful sunset over mountains...", lines=3), | |
outputs=gr.Textbox(label="Result"), | |
title="π¨ Image Generator", | |
description="Generate images using AI from text prompts" | |
) | |
# Create tabbed interface | |
demo = gr.TabbedInterface( | |
[bg_interface, upscale_interface, gen_interface], | |
["Background Removal", "Image Upscaler", "Image Generator"], | |
title="π€ A1D MCP Server - Universal AI Tools" | |
) | |
return demo | |
if __name__ == "__main__": | |
# Check for API key | |
if not os.getenv("A1D_API_KEY"): | |
print("β Error: A1D_API_KEY environment variable is required") | |
print("Please set your API key in the Space settings") | |
exit(1) | |
print("π Starting A1D MCP Server (Minimal Version)...") | |
print("β API key found") | |
print("π Server will be available at: http://0.0.0.0:7860") | |
# Create and launch the minimal app | |
demo = create_minimal_interface() | |
demo.launch( | |
server_name="0.0.0.0", | |
server_port=7860, | |
share=False | |
) | |