Skip to content

Instantly share code, notes, and snippets.

@gengen1988
Created April 17, 2024 10:58
Show Gist options
  • Save gengen1988/f040a5c8004fea57eb651eb209455362 to your computer and use it in GitHub Desktop.
Save gengen1988/f040a5c8004fea57eb651eb209455362 to your computer and use it in GitHub Desktop.
quick expose gradio python api to http
import asyncio
from typing import Annotated
from fastapi import Body, FastAPI
from gradio_client import Client
app = FastAPI()
@app.post("/gradio_proxy/{api_name}")
async def proxy(api_name: str, parameters: Annotated[dict, Body()]):
client = Client("http://127.0.0.1:7860")
job = client.submit(**parameters, api_name=f"/{api_name}")
loop = asyncio.get_running_loop()
result = await loop.run_in_executor(None, job.result)
return result
fastapi
uvicorn[standard]
gradio_client
#!/bin/bash
uvicorn main:app --reload
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment