gradio-app / gradio

Build and share delightful machine learning apps, all in Python. 🌟 Star to support our work!
http://www.gradio.app
Apache License 2.0
31.79k stars 2.37k forks source link

File "C:\Python311\Lib\site-packages\gradio\queueing.py", line 217, in push async with self.pending_message_lock: TypeError: 'NoneType' object does not support the asynchronous context manager protocol #7727

Closed sultan7577 closed 5 months ago

sultan7577 commented 5 months ago

Describe the bug

TypeError: 'NoneType' object does not support the asynchronous context manager protocol, is typically raised when you're trying to use an object as an asynchronous context manager (i.e., using it in an async with statement), but the object is None.

In your case, it seems like the pending_message_lock attribute of the self object (which appears to be an instance of a class from the gradio library) is None. This could be due to a variety of reasons, such as:

The pending_message_lock attribute not being properly initialized. The pending_message_lock attribute being accidentally overwritten somewhere in the code. An issue with the gradio library itself.

Hello eveyrone, hope you are doing well. I am relatively new to gradio and am not an expert in here, I am getting the above error for a long time, and even after trying out different things, am not able to solve it on my own. I would appreciate your help

Have you searched existing issues? 🔎

Reproduction

import gradio as gr
from models import User
from schemas import UserCreate
from flask import Flask, request, jsonify, render_template, redirect, url_for, flash, session
from flask_login import LoginManager, login_user, logout_user, login_required, current_user
from werkzeug.security import check_password_hash, generate_password_hash
from db import create_user, get_user_by_email, get_user_by_id, create_user_profile, create_pdf
from authlib.integrations.flask_client import OAuth
import speech_recognition as sr
from transformers import pipeline
import numpy as np

from flask_uploads import UploadSet, configure_uploads, DOCUMENTS

from generate import generate_qa_pairs
from qag import QuestionAnswerGenerator
from db import (
    get_random_question_answer_pair_from_db,
    get_question_answer_from_db,
    get_unique_sources_from_db,
)
from app_logging import setup_logging, get_logger

transcriber = pipeline("automatic-speech-recognition", model="openai/whisper-base.en")

@app.route('/gradio')
@login_required
def gradio():
    if 'gradio_interface' not in session:
        launch_gradio_interface()  # Launch the Gradio interface when the /gradio route is accessed
        session['gradio_interface'] = True
    return redirect('http://127.0.0.1:7860')  # Redirect to the Gradio interface

def launch_gradio_interface():
    def prof_pdf(message, history, sources, audio):
        # Convert audio to text
        if audio is not None:
            sr, y = audio
            y = y.astype(np.float32)
            y /= np.max(np.abs(y))

            message = transcriber({"sampling_rate": sr, "raw": y})["text"]
            yield f"Converted text from audio: {message}"
            return
        else: #m- if not blank submit, means user has inputed an answer for evaluvation.
            question = history[-1][1]
            db_document = get_question_answer_from_db(question)
            if db_document is None:
                yield "I cannot validate your answer."
                return
            else:
                context = db_document["context"]
                response = "**Grade:** "
                for token in qag.answer_evaluator.stream(
                    {"context": context, "question": question, "answer": message}
                ):
                    response += token
                    yield response
                if db_document["answer"].strip():
                    response += (
                        f"\n\n**Here is the correct answer:**\n\n{db_document['answer']}"
                    )
                yield response

    with gr.Blocks(theme="soft", title="Prof PDF") as demo:
        with gr.Row():
            with gr.Column(scale=2):
                gr.ChatInterface(
                    prof_pdf,
                    chatbot=gr.Chatbot(height=300, render=False),
                    textbox=gr.Textbox(
                        container=False,
                        scale=7,
                        render=False,
                    ),
                    additional_inputs=[
                        gr.Audio(label="Speak or upload audio"),
                        gr.Dropdown(
                            choices=get_unique_sources_from_db(),
                            label="Source",
                            multiselect=True,
                        ),
                    ],
                    description="Prompt Prof PDF to ask a question.",
                    retry_btn=None,
                    undo_btn=None,
                    clear_btn="Clear",
                )
            # upload pdf gradio interface
            with gr.Column(scale=1):
                result = gr.Markdown(label="Question Answer Pairs")
                max_chunks = gr.Number(
                    value=2,
                    label="Number of Pages",
                    minimum=1,
                    maximum=None,
                )
                max_question_answer_pairs = gr.Number(
                    value=5,
                    label="Number of Question Answer Pairs",
                    minimum=1,
                    maximum=None,
                )
                upload_button = gr.UploadButton(
                    "Click to Upload a File",
                    file_types=["pdf"],
                    file_count="single",
                )

            upload_button.upload(
                lambda file_obj, max_chunks, max_question_answer_pairs: generate_qa_pairs(
                    file_obj, max_chunks, max_question_answer_pairs, qag
                ),
                [upload_button, max_chunks, max_question_answer_pairs],
                result,
            )

    demo.launch(share=True)

@app.route('/login', methods=['GET', 'POST'])
def login():
    if request.method == 'POST':
        email = request.form.get('email')
        password = request.form.get('password')
        user = get_user_by_email(email)  # Get the user from the database

        if user and user.password == password:  # Check if the user exists and the password is correct
            login_user(user)
            if current_user.is_authenticated:  # Check if the user is logged in
                flash('Logged in successfully', 'success')
                return redirect(url_for('gradio'))  # Redirect to the /gradio route
            else:
                flash('Unsuccessful login. Please check username and password', 'danger')
        else:
            flash('Unsuccessful login. Please check username and password', 'danger')

    return render_template('login.html')

@app.route("/logout")
@login_required
def logout():
    logout_user()
    return redirect(url_for('home'))

@app.route('/dashboard')
@login_required
def dashboard():
    return render_template('dashboard.html')

@app.route('/change_password', methods=['GET', 'POST'])
@login_required
def change_password():
    if request.method == 'POST':
        old_password = request.form.get('old_password')
        new_password = request.form.get('new_password')
        confirm_password = request.form.get('confirm_password')

        if not check_password_hash(current_user.password, old_password):
            flash('Old password is incorrect', 'danger')
            return redirect(url_for('change_password'))

        if new_password != confirm_password:
            flash('New password and confirm password do not match', 'danger')
            return redirect(url_for('change_password'))

        current_user.password = generate_password_hash(new_password)
        flash('Password changed successfully', 'success')
        return redirect(url_for('dashboard'))

    return render_template('change_password.html')

if __name__ == "__main__":
    app.run(debug=True)

Screenshot

Screenshot 2024-03-18 165147

Logs

INFO:httpx:HTTP Request: POST https://api.gradio.app/gradio-error-analytics/ "HTTP/1.1 200 OK"
INFO:httpx:HTTP Request: POST https://api.gradio.app/gradio-launched-telemetry/ "HTTP/1.1 200 OK"
ERROR:    Exception in ASGI application
  + Exception Group Traceback (most recent call last):
  |   File "C:\Python311\Lib\site-packages\starlette\_utils.py", line 87, in collapse_excgroups
  |     yield
  |   File "C:\Python311\Lib\site-packages\starlette\middleware\base.py", line 190, in __call__
  |     async with anyio.create_task_group() as task_group:
  |   File "C:\Python311\Lib\site-packages\anyio\_backends\_asyncio.py", line 678, in __aexit__
  |     raise BaseExceptionGroup(
  | ExceptionGroup: unhandled errors in a TaskGroup (1 sub-exception)
  +-+---------------- 1 ----------------
    | Traceback (most recent call last):
    |   File "C:\Python311\Lib\site-packages\uvicorn\protocols\http\httptools_impl.py", line 412, in run_asgi       
    |     result = await app(  # type: ignore[func-returns-value]
    |              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |   File "C:\Python311\Lib\site-packages\uvicorn\middleware\proxy_headers.py", line 69, in __call__
    |     return await self.app(scope, receive, send)
    |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |   File "C:\Python311\Lib\site-packages\fastapi\applications.py", line 1054, in __call__
    |     await super().__call__(scope, receive, send)
    |   File "C:\Python311\Lib\site-packages\starlette\applications.py", line 123, in __call__
    |     await self.middleware_stack(scope, receive, send)
    |   File "C:\Python311\Lib\site-packages\starlette\middleware\errors.py", line 186, in __call__
    |     raise exc
    |   File "C:\Python311\Lib\site-packages\starlette\middleware\errors.py", line 164, in __call__
    |     await self.app(scope, receive, _send)
    |   File "C:\Python311\Lib\site-packages\starlette\middleware\base.py", line 189, in __call__
    |     with collapse_excgroups():
    |   File "C:\Python311\Lib\contextlib.py", line 155, in __exit__
    |     self.gen.throw(typ, value, traceback)
    |   File "C:\Python311\Lib\site-packages\starlette\_utils.py", line 93, in collapse_excgroups
    |     raise exc
    |   File "C:\Python311\Lib\site-packages\starlette\middleware\base.py", line 191, in __call__
    |     response = await self.dispatch_func(request, call_next)
    |                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |   File "C:\Python311\Lib\site-packages\gradio\route_utils.py", line 664, in dispatch
    |     response = await call_next(request)
    |                ^^^^^^^^^^^^^^^^^^^^^^^^
    |   File "C:\Python311\Lib\site-packages\starlette\middleware\base.py", line 165, in call_next
    |     raise app_exc
    |   File "C:\Python311\Lib\site-packages\starlette\middleware\base.py", line 151, in coro
    |     await self.app(scope, receive_or_disconnect, send_no_error)
    |   File "C:\Python311\Lib\site-packages\starlette\middleware\exceptions.py", line 62, in __call__
    |     await wrap_app_handling_exceptions(self.app, conn)(scope, receive, send)
    |   File "C:\Python311\Lib\site-packages\starlette\_exception_handler.py", line 64, in wrapped_app
    |     raise exc
    |   File "C:\Python311\Lib\site-packages\starlette\_exception_handler.py", line 53, in wrapped_app
    |     await app(scope, receive, sender)
    |   File "C:\Python311\Lib\site-packages\starlette\routing.py", line 758, in __call__
    |     await self.middleware_stack(scope, receive, send)
    |   File "C:\Python311\Lib\site-packages\starlette\routing.py", line 778, in app
    |     await route.handle(scope, receive, send)
    |   File "C:\Python311\Lib\site-packages\starlette\routing.py", line 299, in handle
    |     await self.app(scope, receive, send)
    |   File "C:\Python311\Lib\site-packages\starlette\routing.py", line 79, in app
    |     await wrap_app_handling_exceptions(app, request)(scope, receive, send)
    |   File "C:\Python311\Lib\site-packages\starlette\_exception_handler.py", line 64, in wrapped_app
    |     raise exc
    |   File "C:\Python311\Lib\site-packages\starlette\_exception_handler.py", line 53, in wrapped_app
    |     await app(scope, receive, sender)
    |   File "C:\Python311\Lib\site-packages\starlette\routing.py", line 74, in app
    |     response = await func(request)
    |                ^^^^^^^^^^^^^^^^^^^
    |   File "C:\Python311\Lib\site-packages\fastapi\routing.py", line 278, in app
    |     raw_response = await run_endpoint_function(
    |                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |   File "C:\Python311\Lib\site-packages\fastapi\routing.py", line 191, in run_endpoint_function
    |     return await dependant.call(**values)
    |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |   File "C:\Python311\Lib\site-packages\gradio\routes.py", line 662, in queue_join
    |     return await queue_join_helper(body, request, username)
    |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |   File "C:\Python311\Lib\site-packages\gradio\routes.py", line 680, in queue_join_helper
    |     success, event_id = await blocks._queue.push(
    |                         ^^^^^^^^^^^^^^^^^^^^^^^^^
    |   File "C:\Python311\Lib\site-packages\gradio\queueing.py", line 217, in push
    |     async with self.pending_message_lock:
    | TypeError: 'NoneType' object does not support the asynchronous context manager protocol
    +------------------------------------

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Python311\Lib\site-packages\uvicorn\protocols\http\httptools_impl.py", line 412, in run_asgi
    result = await app(  # type: ignore[func-returns-value]
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Python311\Lib\site-packages\uvicorn\middleware\proxy_headers.py", line 69, in __call__
    return await self.app(scope, receive, send)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Python311\Lib\site-packages\fastapi\applications.py", line 1054, in __call__
    await super().__call__(scope, receive, send)
  File "C:\Python311\Lib\site-packages\starlette\applications.py", line 123, in __call__
    await self.middleware_stack(scope, receive, send)
  File "C:\Python311\Lib\site-packages\starlette\middleware\errors.py", line 186, in __call__
    raise exc
  File "C:\Python311\Lib\site-packages\starlette\middleware\errors.py", line 164, in __call__
    await self.app(scope, receive, _send)
  File "C:\Python311\Lib\site-packages\starlette\middleware\base.py", line 189, in __call__
    with collapse_excgroups():
  File "C:\Python311\Lib\contextlib.py", line 155, in __exit__
    self.gen.throw(typ, value, traceback)
  File "C:\Python311\Lib\site-packages\starlette\_utils.py", line 93, in collapse_excgroups
    raise exc
  File "C:\Python311\Lib\site-packages\starlette\middleware\base.py", line 191, in __call__
    response = await self.dispatch_func(request, call_next)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Python311\Lib\site-packages\gradio\route_utils.py", line 664, in dispatch
    response = await call_next(request)
               ^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Python311\Lib\site-packages\starlette\middleware\base.py", line 165, in call_next
    raise app_exc
  File "C:\Python311\Lib\site-packages\starlette\middleware\base.py", line 151, in coro
    await self.app(scope, receive_or_disconnect, send_no_error)
  File "C:\Python311\Lib\site-packages\starlette\middleware\exceptions.py", line 62, in __call__
    await wrap_app_handling_exceptions(self.app, conn)(scope, receive, send)
  File "C:\Python311\Lib\site-packages\starlette\_exception_handler.py", line 64, in wrapped_app
    raise exc
  File "C:\Python311\Lib\site-packages\starlette\_exception_handler.py", line 53, in wrapped_app
    await app(scope, receive, sender)
  File "C:\Python311\Lib\site-packages\starlette\routing.py", line 758, in __call__
    await self.middleware_stack(scope, receive, send)
  File "C:\Python311\Lib\site-packages\starlette\routing.py", line 778, in app
    await route.handle(scope, receive, send)
  File "C:\Python311\Lib\site-packages\starlette\routing.py", line 299, in handle
    await self.app(scope, receive, send)
  File "C:\Python311\Lib\site-packages\starlette\routing.py", line 79, in app
    await wrap_app_handling_exceptions(app, request)(scope, receive, send)
  File "C:\Python311\Lib\site-packages\starlette\_exception_handler.py", line 64, in wrapped_app
    raise exc
  File "C:\Python311\Lib\site-packages\starlette\_exception_handler.py", line 53, in wrapped_app
    await app(scope, receive, sender)
  File "C:\Python311\Lib\site-packages\starlette\routing.py", line 74, in app
    response = await func(request)
               ^^^^^^^^^^^^^^^^^^^
  File "C:\Python311\Lib\site-packages\fastapi\routing.py", line 278, in app
    raw_response = await run_endpoint_function(
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Python311\Lib\site-packages\fastapi\routing.py", line 191, in run_endpoint_function
    return await dependant.call(**values)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Python311\Lib\site-packages\gradio\routes.py", line 662, in queue_join
    return await queue_join_helper(body, request, username)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Python311\Lib\site-packages\gradio\routes.py", line 680, in queue_join_helper
    success, event_id = await blocks._queue.push(
                        ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Python311\Lib\site-packages\gradio\queueing.py", line 217, in push
    async with self.pending_message_lock:
TypeError: 'NoneType' object does not support the asynchronous context manager protocol

System Info

pip show gradio
Name: gradio
Version: 4.21.0
Summary: Python library for easily interacting with trained machine learning models
Home-page:
Author:
Author-email: Abubakar Abid <gradio-team@huggingface.co>, Ali Abid <gradio-team@huggingface.co>, Ali Abdalla <gradio-team@huggingface.co>, Dawood Khan <gradio-team@huggingface.co>, Ahsen Khaliq <gradio-team@huggingface.co>, Pete Allen <gradio-team@huggingface.co>, Ömer Faruk Özdemir <gradio-team@huggingface.co>, Freddy A Boulton <gradio-team@huggingface.co>, Hannah Blair <gradio-team@huggingface.co>
License:
Location: C:\Python311\Lib\site-packages
Requires: aiofiles, altair, fastapi, ffmpy, gradio-client, httpx, huggingface-hub, importlib-resources, jinja2, markupsafe, matplotlib, numpy, orjson, packaging, pandas, pillow, pydantic, pydub, python-multipart, pyyaml, ruff, semantic-version, tomlkit, typer, typing-extensions, uvicorn
Required-by:

Severity

Blocking usage of gradio

abidlabs commented 5 months ago

Hi @sultan7577 can you please provide a minimal code example that we can use to reproduce the issue above? See: https://stackoverflow.com/help/minimal-reproducible-example

In the meantime, just looking through the code, it seems like you are trying to mount a gradio application within a larger fastapi application. If you are doing that, you need to follow a specific pattern, please see the docs: https://www.gradio.app/docs/mount_gradio_app https://www.gradio.app/guides/sharing-your-app#mounting-within-another-fast-api-app

sultan7577 commented 5 months ago

Pardon me for asking but since im new to this, can you clarify what do you mean by minimal reproducible code example?

abidlabs commented 5 months ago

Hi @sultan7577 a small, self-contained example which has the same bug that you're experiencing. It should not include external dependencies whenever possible. See https://stackoverflow.com/help/minimal-reproducible-example

freddyaboulton commented 5 months ago

This error happens when you try to launch gradio outside the main thread. Since you're not using FastAPI, I think you'll have to start the gradio app and your flask app in separate processes.

sultan7577 commented 5 months ago

This error happens when you try to launch gradio outside the main thread. Since you're not using FastAPI, I think you'll have to start the gradio app and your flask app in separate processes.

Thank you very much for the review, i will try to work on this.

sultan7577 commented 5 months ago

Hi @sultan7577 a small, self-contained example which has the same bug that you're experiencing. It should not include external dependencies whenever possible. See https://stackoverflow.com/help/minimal-reproducible-example

Thank you for your review, i will try to figure it out as you guys mentioned.

abidlabs commented 5 months ago

Closing for now, thanks folks!