Spaces:
Running
Running
| #!/usr/bin/env python3 | |
| """ | |
| Simple A1D MCP Server with Gradio - Minimal version to avoid type inference 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 remove_bg_simple(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", None | |
| 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 message, media_url | |
| except Exception as e: | |
| return f"β Error: {str(e)}", None | |
| def image_upscaler_simple(image_url, scale): | |
| """Upscale images using AI""" | |
| try: | |
| if not image_url or not image_url.strip(): | |
| return "β Error: Please provide an image URL", None | |
| client = get_api_client() | |
| data = prepare_request_data("image_upscaler", image_url=image_url, scale=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 message, media_url | |
| except Exception as e: | |
| return f"β Error: {str(e)}", None | |
| def image_generator_simple(prompt): | |
| """Generate images using AI from text prompts""" | |
| try: | |
| if not prompt or not prompt.strip(): | |
| return "β Error: Please provide a text prompt", None | |
| 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 message, media_url | |
| except Exception as e: | |
| return f"β Error: {str(e)}", None | |
| def create_simple_app(): | |
| """Create a simple Gradio app""" | |
| with gr.Blocks(title="A1D MCP Server - Universal AI Tools") as demo: | |
| gr.Markdown("# π€ A1D MCP Server - Universal AI Tools") | |
| gr.Markdown("Powerful AI image and video processing tools for any MCP-compatible client.") | |
| with gr.Tab("π Background Removal"): | |
| with gr.Row(): | |
| with gr.Column(): | |
| bg_input = gr.Textbox( | |
| label="Image URL", | |
| placeholder="https://example.com/image.jpg" | |
| ) | |
| bg_button = gr.Button("Remove Background", variant="primary") | |
| with gr.Column(): | |
| bg_output = gr.Textbox(label="Result") | |
| bg_image = gr.Image(label="Preview") | |
| bg_button.click( | |
| fn=remove_bg_simple, | |
| inputs=[bg_input], | |
| outputs=[bg_output, bg_image] | |
| ) | |
| with gr.Tab("π Image Upscaler"): | |
| with gr.Row(): | |
| with gr.Column(): | |
| up_input = gr.Textbox( | |
| label="Image URL", | |
| placeholder="https://example.com/image.jpg" | |
| ) | |
| up_scale = gr.Dropdown( | |
| choices=[2, 4, 8, 16], | |
| value=2, | |
| label="Scale Factor" | |
| ) | |
| up_button = gr.Button("Upscale Image", variant="primary") | |
| with gr.Column(): | |
| up_output = gr.Textbox(label="Result") | |
| up_image = gr.Image(label="Preview") | |
| up_button.click( | |
| fn=image_upscaler_simple, | |
| inputs=[up_input, up_scale], | |
| outputs=[up_output, up_image] | |
| ) | |
| with gr.Tab("π¨ Image Generator"): | |
| with gr.Row(): | |
| with gr.Column(): | |
| gen_input = gr.Textbox( | |
| label="Text Prompt", | |
| placeholder="A beautiful sunset over mountains...", | |
| lines=3 | |
| ) | |
| gen_button = gr.Button("Generate Image", variant="primary") | |
| with gr.Column(): | |
| gen_output = gr.Textbox(label="Result") | |
| gen_image = gr.Image(label="Preview") | |
| gen_button.click( | |
| fn=image_generator_simple, | |
| inputs=[gen_input], | |
| outputs=[gen_output, gen_image] | |
| ) | |
| gr.Markdown(""" | |
| ## π§ MCP Client Configuration | |
| Add this to your Claude Desktop configuration: | |
| ```json | |
| { | |
| "mcpServers": { | |
| "a1d-hosted": { | |
| "command": "npx", | |
| "args": [ | |
| "mcp-remote@latest", | |
| "https://aigchacker-a1d-mcp-server.hf.space/gradio_api/mcp/sse", | |
| "--header", | |
| "API_KEY:${MCP_API_KEY}" | |
| ], | |
| "env": { | |
| "MCP_API_KEY": "your_a1d_api_key_here" | |
| } | |
| } | |
| } | |
| } | |
| ``` | |
| """) | |
| 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 (Simple Version)...") | |
| print("β API key found") | |
| # Create and launch the app | |
| demo = create_simple_app() | |
| demo.launch( | |
| server_name="0.0.0.0", | |
| server_port=7860, | |
| share=False | |
| ) | |