Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

KeyError after updating gradio 3.47 to 4.16 #7492

Closed
1 task done
Akin1995 opened this issue Feb 20, 2024 · 10 comments
Closed
1 task done

KeyError after updating gradio 3.47 to 4.16 #7492

Akin1995 opened this issue Feb 20, 2024 · 10 comments
Labels
bug Something isn't working needs repro Awaiting full reproduction

Comments

@Akin1995
Copy link

Describe the bug

I have updated gradio to 4.16 and now want to run my webapp with FastAPI but i get some error statements, what can i do ?

Have you searched existing issues? 🔎

  • I have searched and found no existing issues

Reproduction

import logging

import gradio
import uvicorn
from fastapi import FastAPI, Request, status
from fastapi.exceptions import RequestValidationError
from fastapi.responses import JSONResponse
from google.cloud.logging_v2 import Client
from starlette.middleware.cors import CORSMiddleware
from starlette.responses import RedirectResponse

from src import gui
from src.config import database, routers, env_settings, project_settings

project = project_settings.get_project_settings()
app = FastAPI(
    title=project.name,
    description=project.description,
    version=project.version,
    contact=project.authors[0]
)

app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

if env_settings.get_env_settings().debug:
    @app.exception_handler(RequestValidationError)
    async def validation_exception_handler(request: Request, exc: RequestValidationError):
        exc_str = f"{exc}".replace("\n", " ").replace("   ", " ")
        logging.warning(f"{request}: {exc_str}")
        content = {"status_code": 10422, "message": exc_str, "data": None}
        return JSONResponse(content=content, status_code=status.HTTP_422_UNPROCESSABLE_ENTITY)


@app.get("/")
async def home_page():
    return RedirectResponse("/gradio")


@app.on_event("startup")
async def app_init():
    """
    Setups logging, connects to database, initializes ODM and include routers in the src.
    """
    
    env = env_settings.get_env_settings()
    if env.use_local_logging:
        from src.config import local_logging
        local_logging.setup_local_logging()
    else:
        Client().setup_logging(log_level=logging.INFO)

    await database.connect_database()
    gradio.mount_gradio_app(app, gui.gradio_interface, path="/gradio")

    for router, prefix in routers.get_routers():
        app.include_router(router, prefix=prefix)

    # logging.info(f"{config.PROJECT_SETTINGS["name"]}: Finished initializing")


if __name__ == "__main__":  # for debugging locally
    uvicorn.run("main:app", host="127.0.0.1", port=8000, workers=4)



Screenshot

No response

Logs

INFO:     127.0.0.1:42894 - "POST /gradio/queue/join HTTP/1.1" 500 Internal Server Error
ERROR:    Exception in ASGI application
Traceback (most recent call last):
  File "/home/akin/miniconda3/envs/ml_toolkit/lib/python3.11/site-packages/uvicorn/protocols/http/httptools_impl.py", line 426, in run_asgi
    result = await app(  # type: ignore[func-returns-value]
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/akin/miniconda3/envs/ml_toolkit/lib/python3.11/site-packages/uvicorn/middleware/proxy_headers.py", line 84, in __call__
    return await self.app(scope, receive, send)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/akin/miniconda3/envs/ml_toolkit/lib/python3.11/site-packages/fastapi/applications.py", line 1054, in __call__
    await super().__call__(scope, receive, send)
  File "/home/akin/miniconda3/envs/ml_toolkit/lib/python3.11/site-packages/starlette/applications.py", line 123, in __call__
    await self.middleware_stack(scope, receive, send)
  File "/home/akin/miniconda3/envs/ml_toolkit/lib/python3.11/site-packages/starlette/middleware/errors.py", line 186, in __call__
    raise exc
  File "/home/akin/miniconda3/envs/ml_toolkit/lib/python3.11/site-packages/starlette/middleware/errors.py", line 164, in __call__
    await self.app(scope, receive, _send)
  File "/home/akin/miniconda3/envs/ml_toolkit/lib/python3.11/site-packages/starlette/middleware/cors.py", line 91, in __call__
    await self.simple_response(scope, receive, send, request_headers=headers)
  File "/home/akin/miniconda3/envs/ml_toolkit/lib/python3.11/site-packages/starlette/middleware/cors.py", line 146, in simple_response
    await self.app(scope, receive, send)
  File "/home/akin/miniconda3/envs/ml_toolkit/lib/python3.11/site-packages/starlette/middleware/exceptions.py", line 62, in __call__
    await wrap_app_handling_exceptions(self.app, conn)(scope, receive, send)
  File "/home/akin/miniconda3/envs/ml_toolkit/lib/python3.11/site-packages/starlette/_exception_handler.py", line 64, in wrapped_app
    raise exc
  File "/home/akin/miniconda3/envs/ml_toolkit/lib/python3.11/site-packages/starlette/_exception_handler.py", line 53, in wrapped_app
    await app(scope, receive, sender)
  File "/home/akin/miniconda3/envs/ml_toolkit/lib/python3.11/site-packages/starlette/routing.py", line 762, in __call__
    await self.middleware_stack(scope, receive, send)
  File "/home/akin/miniconda3/envs/ml_toolkit/lib/python3.11/site-packages/starlette/routing.py", line 782, in app
    await route.handle(scope, receive, send)
  File "/home/akin/miniconda3/envs/ml_toolkit/lib/python3.11/site-packages/starlette/routing.py", line 485, in handle
    await self.app(scope, receive, send)
  File "/home/akin/miniconda3/envs/ml_toolkit/lib/python3.11/site-packages/fastapi/applications.py", line 1054, in __call__
    await super().__call__(scope, receive, send)
  File "/home/akin/miniconda3/envs/ml_toolkit/lib/python3.11/site-packages/starlette/applications.py", line 123, in __call__
    await self.middleware_stack(scope, receive, send)
  File "/home/akin/miniconda3/envs/ml_toolkit/lib/python3.11/site-packages/starlette/middleware/errors.py", line 186, in __call__
    raise exc
  File "/home/akin/miniconda3/envs/ml_toolkit/lib/python3.11/site-packages/starlette/middleware/errors.py", line 164, in __call__
    await self.app(scope, receive, _send)
  File "/home/akin/miniconda3/envs/ml_toolkit/lib/python3.11/site-packages/starlette/middleware/cors.py", line 91, in __call__
    await self.simple_response(scope, receive, send, request_headers=headers)
  File "/home/akin/miniconda3/envs/ml_toolkit/lib/python3.11/site-packages/starlette/middleware/cors.py", line 146, in simple_response
    await self.app(scope, receive, send)
  File "/home/akin/miniconda3/envs/ml_toolkit/lib/python3.11/site-packages/starlette/middleware/exceptions.py", line 62, in __call__
    await wrap_app_handling_exceptions(self.app, conn)(scope, receive, send)
  File "/home/akin/miniconda3/envs/ml_toolkit/lib/python3.11/site-packages/starlette/_exception_handler.py", line 64, in wrapped_app
    raise exc
  File "/home/akin/miniconda3/envs/ml_toolkit/lib/python3.11/site-packages/starlette/_exception_handler.py", line 53, in wrapped_app
    await app(scope, receive, sender)
  File "/home/akin/miniconda3/envs/ml_toolkit/lib/python3.11/site-packages/starlette/routing.py", line 762, in __call__
    await self.middleware_stack(scope, receive, send)
  File "/home/akin/miniconda3/envs/ml_toolkit/lib/python3.11/site-packages/starlette/routing.py", line 782, in app
    await route.handle(scope, receive, send)
  File "/home/akin/miniconda3/envs/ml_toolkit/lib/python3.11/site-packages/starlette/routing.py", line 297, in handle
    await self.app(scope, receive, send)
  File "/home/akin/miniconda3/envs/ml_toolkit/lib/python3.11/site-packages/starlette/routing.py", line 77, in app
    await wrap_app_handling_exceptions(app, request)(scope, receive, send)
  File "/home/akin/miniconda3/envs/ml_toolkit/lib/python3.11/site-packages/starlette/_exception_handler.py", line 64, in wrapped_app
    raise exc
  File "/home/akin/miniconda3/envs/ml_toolkit/lib/python3.11/site-packages/starlette/_exception_handler.py", line 53, in wrapped_app
    await app(scope, receive, sender)
  File "/home/akin/miniconda3/envs/ml_toolkit/lib/python3.11/site-packages/starlette/routing.py", line 72, in app
    response = await func(request)
               ^^^^^^^^^^^^^^^^^^^
  File "/home/akin/miniconda3/envs/ml_toolkit/lib/python3.11/site-packages/fastapi/routing.py", line 299, in app
    raise e
  File "/home/akin/miniconda3/envs/ml_toolkit/lib/python3.11/site-packages/fastapi/routing.py", line 294, in app
    raw_response = await run_endpoint_function(
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/akin/miniconda3/envs/ml_toolkit/lib/python3.11/site-packages/fastapi/routing.py", line 191, in run_endpoint_function
    return await dependant.call(**values)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/akin/miniconda3/envs/ml_toolkit/lib/python3.11/site-packages/gradio/routes.py", line 696, in queue_join
    success, event_id = await blocks._queue.push(body, request, username)
                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/akin/miniconda3/envs/ml_toolkit/lib/python3.11/site-packages/gradio/queueing.py", line 217, in push
    event_queue = self.event_queue_per_concurrency_id[event.concurrency_id]
                  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^
KeyError: '139837161232544'

System Info

fastapi==0.109.*
pydantic==2.5.*
pydantic-settings==2.1.*
uvicorn[standard]==0.25.*
gradio==4.19.*

Severity

I can work around it

@Akin1995 Akin1995 added the bug Something isn't working label Feb 20, 2024
@pngwn
Copy link
Member

pngwn commented Feb 20, 2024

Do you have a minimal reproduction for this that we can run ourselves?

@pngwn pngwn added the needs repro Awaiting full reproduction label Feb 20, 2024
@Akin1995
Copy link
Author

Akin1995 commented Feb 20, 2024

css = """img {object-fit: fill !important};footer {visibility: hidden};"""

with gr.Blocks(
  css=css, 
  theme=gr.themes.Default(text_size=gr.themes.sizes.text_lg,
  font=gr.themes.GoogleFont("Monospace"))
) as gradio_interface:
    image_path = search_file_in_given_directory(
      os.getcwd(), 
      "Ella-Logo-transparent.png"
    )
    gr.Image(
      value=image_path, 
      width=150, 
      height=150, 
      interactive=False,
      show_label=False, 
      show_download_button=False, 
      show_share_button=False, 
      container=False
    )
    
    analytics_enabled = False
    
    gr.Markdown("MLToolkit demo, use any of the available tools!")

    suggested_output_styles = [
      "emotional", 
      "narrative", 
      "neutral", 
      "philosophical", 
      "sensational",
       "suspenseful", 
       "polite"
     ]

    with gr.Tab(Tools.STYLER):
        input_styler_type = gr.Dropdown(
          choices=suggested_output_styles,
          value="emotional" ,
          label="Design Output Style"
        )
        input_styler_text_ = gr.Textbox(lines=10, label="Input")
        button_style = gr.Button("Formated Text in Style")
        output_styler_text = gr.Textbox(lines=10, label="Output")

    button_style.click(
        rewrite_text_in_style,
        inputs=[input_styler_text_, input_styler_type],
        outputs=output_styler_text
    )

this is an example of a service calling gpt 4 in the background

@Akin1995
Copy link
Author

Can it be that the issue is because mounting takes place after the fastapi has already started because app_init() only is launched after uvicorn runs the fastapi application! So we cannot mount a gradio app to already running fast api?

@abidlabs abidlabs changed the title Error after updating gradio 3.47 to 4.16 KeyError after updating gradio 3.47 to 4.16 Feb 21, 2024
@Akin1995
Copy link
Author

already solved

@abidlabs
Copy link
Member

Hi @Akin1995 what was the solution?

@astromme
Copy link

astromme commented Mar 7, 2024

I'm also experiencing this, and have a similar setup. What was the resolution?

@microdou
Copy link

microdou commented Mar 22, 2024

@Akin1995 I had the exact same error message when trying to upgrade from 3.50.2 to v4. What's your workaround?

@EasonBz
Copy link

EasonBz commented Apr 9, 2024

I use
pip install gradio==4.8.0
successfully

@Archer6621
Copy link

Archer6621 commented May 2, 2024

I use pip install gradio==4.8.0 successfully

Using this version with the basic chat example, it just keeps hanging forever.... Clearly something is broken with FastAPI and Gradio interoperability.

A bit annoying that the OP did not share their solution, can this issue be reopened? It gives different errors on the latest version, see this issue (I believe it is related): #8074

@qgtf
Copy link

qgtf commented May 28, 2024

I use 我使用 pip install gradio==4.8.0点安装 gradio==4.8.0 successfully 成功

Great! it solved my issues;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working needs repro Awaiting full reproduction
Projects
None yet
Development

No branches or pull requests

8 participants