gradio: Increase `httpx` timeout to prevent requests from erroring out

Describe the bug

I’m developing a chatbot based on the RAG-QA.

I’m getting an Error on the Gradio front-end even though the backend works properly.

I’m uploading 1 page from Wikipedia using LangChain WikipediaLoader. Then creating a vector db using FAISS.

I’ve enabled logging and it looks as below:

2023-08-09 07:40:14 INFO [vector_store] Creating index...
2023-08-09 07:40:14 WARNING [langchain.text_splitter] Created a chunk of size 2518, which is longer than the specified 500
2023-08-09 07:40:14 WARNING [langchain.text_splitter] Created a chunk of size 793, which is longer than the specified 500
2023-08-09 07:40:14 WARNING [langchain.text_splitter] Created a chunk of size 2212, which is longer than the specified 500
2023-08-09 07:40:14 WARNING [langchain.text_splitter] Created a chunk of size 790, which is longer than the specified 500
Batches: 100%|██████████████████████████████████████████████████████████████████████████████████████████████████████████| 1/1 [00:03<00:00,  3.90s/it]
2023-08-09 07:40:18 INFO [faiss.loader] Loading faiss.
2023-08-09 07:40:18 INFO [faiss.loader] Successfully loaded faiss.
2023-08-09 07:40:18 INFO [vector_store] Index created successfully!
2023-08-09 07:40:18 INFO [common_utils] create_index took: 3.96 seconds
2023-08-09 07:40:18 INFO [vector_store] wikipedia data uploaded successfully!

In total it takes 3.96 seconds for the index creation.

I’m getting the Error on the Gradio front-end where I’m displaying the Status of the upload if it’s successful or not in the gr.Textbox()

Any help please?

Have you searched existing issues? 🔎

  • I have searched and found no existing issues

Reproduction

Create the upload button as below:

with gr.Blocks(title="Demo", theme=gr.themes.Soft()) as demo:
        with gr.Column(scale=1):
                upload_success = gr.Textbox(
                    label="Success of the Upload",
                    placeholder="Success status for the data upload would appear here",
                    interactive=False,
                    queue=False
                )
        
              page = gr.Textbox(
                  label="Enter Wikipedia page names",
                  placeholder="Type the Wikipedia page names here",
                  value="2022 FIFA World Cup"
              )
              
              b_page = gr.Button("Load Wikipedia Pages")
              b_page.click(
                  fn=load_wiki_pages,
                  inputs=page,
                  outputs=upload_success
              )


demo.queue().launch(share=True)

NOTE: I need to use demo.queue() because I’m developing a RAG-QA Chatbot. Inside chatbot, I’m streaming the response with:

for char in response:
      history[-1][1] += char
      time.sleep(0.03)
      yield history

Loading and creating the vector store as below:

from langchain.document_loaders import WikipediaLoader
from langchain.text_splitter import CharacterTextSplitter
from langchain.vectorstores import FAISS
from langchain.embeddings.openai import OpenAIEmbeddings

docs = WikipediaLoader(query=page, load_max_docs=1).load()

text_splitter = CharacterTextSplitter(
        chunk_size=500,
        chunk_overlap=100,
        separator="\n",
        )

documents = text_splitter.split_documents(docs)
        
# Vector store using Facebook AI Similarity Search
embeddings = OpenAIEmbeddings(openai_api_key=openai_api_key)
index = FAISS.from_documents(documents, embeddings)

Screenshot

Originally it looks as below: image

On uploading and creating the index from Wikipedia page: image

Logs

No response

System Info

gradio                        3.27.0
gradio_client                 0.3.0
langchain                     0.0.228
wikipedia                     1.4.0


### Severity

Blocking usage of gradio

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 1
  • Comments: 23 (6 by maintainers)

Most upvoted comments

@piwawa you can use this script to change the httpx timeout:

#!/bin/bash

# Get the path to the _config.py file
config_path=$(python -c "import httpx; import os; print(os.path.join(os.path.dirname(httpx.__file__), '_config.py'))")

# Check if the file exists
if [ ! -f "$config_path" ]; then
    echo "Error: $config_path does not exist."
    exit 1
fi

# Create a backup of the original file
cp "$config_path" "${config_path}.bak"

# Modify the file
sed -i "s/DEFAULT_TIMEOUT_CONFIG = Timeout(timeout=5.0)/DEFAULT_TIMEOUT_CONFIG = Timeout(timeout=60)/" "$config_path"

# Check if the modification was successful
if grep -q "DEFAULT_TIMEOUT_CONFIG = Timeout(timeout=60)" "$config_path"; then
    echo "Successfully modified $config_path"
    echo "A backup of the original file was created at ${config_path}.bak"
else
    echo "Failed to modify $config_path"
    echo "The original file remains unchanged"
fi

Hey, I am running a gradio instance within a docker container and query it via the API from the host machine.

This exact issue seems to be a problem with the Client class imported from gradio_client. By running a simple API call as showed in the simple tutorial, all my request time out after 5s.

from gradio_client import Client

client = Client("http://localhost:7860/")
result = client.predict(
		message="Hello!",
		api_name="/chat"
)
print(result)

I edited the same line in the _config.py file of httpx as suggested by @chintan-donda and it works. Posting here since this seems to be the exact same issue

Package Version
------- -------
Brotli  1.0.9
gradio  4.28.3
jupyter 1.0.0
pip     23.3.1
PyQt5   5.15.10
PySocks 1.7.1
sip     6.7.12
wheel   0.41.2

Thanks a lot!

Interesting thanks @chintan-donda so what did you need to do to fix this?

Manually change the value in this line to higher number, say 30. DEFAULT_TIMEOUT_CONFIG = Timeout(timeout=30.0)