Spaces:
Running
Running
File size: 6,358 Bytes
63d8486 |
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 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 |
#!/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
)
|