Call mounted gradio app via api (#4435)

* Fix bug

* Add changelog
This commit is contained in:
Freddy Boulton 2023-06-07 00:40:56 +09:00 committed by GitHub
parent 1c48b3e857
commit bc6a1fad1c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 43 additions and 8 deletions

View File

@ -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:

View File

@ -85,7 +85,7 @@ class Client:
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:

View File

@ -23,14 +23,14 @@ import requests
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"

View File

@ -11,6 +11,9 @@ from unittest.mock import MagicMock, patch
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
@ -319,6 +322,37 @@ class TestClientPredictions:
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")