import os import requests import gradio as gr import spaces API_URL = os.environ.get("ALGOTRENDY_API_URL", "") @spaces.GPU(duration=10) def no_op_gpu_function(): # ZeroGPU requires at least one decorated function pass def proxy_request(query, endpoint_path): """Send a request to the AlgoTrendy backend via proxy.""" if not API_URL: # Mock response if no backend configured return f"Mock response from proxy.\nQuery: '{query}'\nPath: '{endpoint_path}'\n(Set ALGOTRENDY_API_URL secret to connect to a real backend)" url = f"{API_URL.rstrip('/')}/{endpoint_path.lstrip('/')}" try: response = requests.post(url, json={"query": query}, timeout=10) try: return response.json() except Exception: return response.text except Exception as e: return f"Error communicating with backend: {e}" with gr.Blocks(title="AlgoTrendy API Proxy") as demo: gr.Markdown("# AlgoTrendy API Proxy") gr.Markdown("This Space acts as a public proxy to the private AlgoTrendy backend.") with gr.Row(): endpoint = gr.Textbox(label="Endpoint Path", value="api/v1/query") query = gr.Textbox(label="Query/Payload", value="hello") submit_btn = gr.Button("Send Request") output = gr.Textbox(label="Response", lines=5) submit_btn.click(fn=proxy_request, inputs=[query, endpoint], outputs=output) if __name__ == "__main__": demo.launch(mcp_server=True)