Skip to content

Commit

Permalink
Call mounted gradio app via api (#4435)
Browse files Browse the repository at this point in the history
* Fix bug

* Add changelog
  • Loading branch information
freddyaboulton committed Jun 6, 2023
1 parent 1c48b3e commit bc6a1fa
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 8 deletions.
1 change: 1 addition & 0 deletions client/python/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
## Bug Fixes:
- Fixes parameter names not showing underscores by [@abidlabs](https://github.com/abidlabs) in [PR 4230](https://github.com/gradio-app/gradio/pull/4230)
- Fixes issue in which state was not handled correctly if `serialize=False` by [@abidlabs](https://github.com/abidlabs) in [PR 4230](https://github.com/gradio-app/gradio/pull/4230)
- Fixed bug where mounted apps could not be called via the client by [@freddyaboulton](https://github.com/freddyaboulton) in [PR 4435](https://github.com/gradio-app/gradio/pull/4435)

## Breaking Changes:

Expand Down
2 changes: 1 addition & 1 deletion client/python/gradio_client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def __init__(
self.space_id = None

if src.startswith("http://") or src.startswith("https://"):
_src = src
_src = src if src.endswith("/") else src + "/"
else:
_src = self._space_name_to_src(src)
if _src is None:
Expand Down
14 changes: 7 additions & 7 deletions client/python/gradio_client/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@
from huggingface_hub import SpaceStage
from websockets.legacy.protocol import WebSocketCommonProtocol

API_URL = "/api/predict/"
WS_URL = "/queue/join"
UPLOAD_URL = "/upload"
CONFIG_URL = "/config"
API_INFO_URL = "/info"
RAW_API_INFO_URL = "/info?serialize=False"
API_URL = "api/predict/"
WS_URL = "queue/join"
UPLOAD_URL = "upload"
CONFIG_URL = "config"
API_INFO_URL = "info"
RAW_API_INFO_URL = "info?serialize=False"
SPACE_FETCHER_URL = "https://gradio-space-api-fetcher-v2.hf.space/api"
RESET_URL = "/reset"
RESET_URL = "reset"
SPACE_URL = "https://hf.space/{}"

STATE_COMPONENT = "state"
Expand Down
34 changes: 34 additions & 0 deletions client/python/test/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@

import gradio as gr
import pytest
import uvicorn
from fastapi import FastAPI
from gradio.networking import Server
from huggingface_hub.utils import RepositoryNotFoundError

from gradio_client import Client
Expand Down Expand Up @@ -319,6 +322,37 @@ def test_state_without_serialize(self, stateful_chatbot):
ret = client.predict(message, initial_history, api_name="/submit")
assert ret == ("", [["", None], ["Hello", "I love you"]])

def test_can_call_mounted_app_via_api(self):
def greet(name):
return "Hello " + name + "!"

gradio_app = gr.Interface(
fn=greet,
inputs=gr.Textbox(lines=2, placeholder="Name Here..."),
outputs="text",
)

app = FastAPI()
app = gr.mount_gradio_app(app, gradio_app, path="/test/gradio")
config = uvicorn.Config(
app=app,
port=8000,
log_level="info",
)
server = Server(config=config)
# Using the gradio Server class to not have
# to implement code again to run uvicorn in a separate thread
# However, that means we need to set this flag to prevent
# run_in_thread_from_blocking
server.started = True
try:
server.run_in_thread()
time.sleep(1)
client = Client("http://127.0.0.1:8000/test/gradio/")
assert client.predict("freddy") == "Hello freddy!"
finally:
server.thread.join(timeout=1)


class TestStatusUpdates:
@patch("gradio_client.client.Endpoint.make_end_to_end_fn")
Expand Down

0 comments on commit bc6a1fa

Please sign in to comment.