Spaces:
Running
Running
File size: 4,789 Bytes
d45a618 |
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 124 125 126 127 128 129 130 131 132 133 |
#!/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
)
|