Spaces:
Configuration error
Configuration error
File size: 1,485 Bytes
4f90c96 | 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 | 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)
|