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
32.18k stars 2.41k forks source link

network timeout error #9264

Open taoxiaoxin1688-dev opened 1 week ago

taoxiaoxin1688-dev commented 1 week ago

Describe the bug

When using gradio on a server without a network environment, there will be network errors. See the log for specific error information. Every time I have to use the monkey patch to deal with this error, sometimes it doesn't work.

Have you searched existing issues? 🔎

Reproduction

import gradio as gr
import os

def process_pdf(file):
    if file is None:
        return "请上传PDF文件"

    file_size = os.path.getsize(file.name) / (1024 * 1024)  # Convert to MB
    if file_size > 5:
        return "文件大小超过5MB限制"

    # Here you would typically process the PDF file
    # For this example, we'll just return a success message
    return f"成功上传并处理了文件: {file.name}"

css = """
.gradio-container {
    max-width: 100% !important;
    padding: 0 !important;
}
#component-0 {
    height: 100vh;
    max-height: 100vh;
    overflow: hidden;
}
.left-sidebar {
    background-color: #f8f9fa;
    border-right: 1px solid #e9ecef;
    padding: 20px;
    height: 100vh;
    overflow-y: auto;
}
.main-content {
    height: 100vh;
    overflow-y: auto;
    padding: 20px;
}
.upload-area {
    border: 2px dashed #CCCCCC;
    border-radius: 10px;
    padding: 20px;
    margin-top: 20px;
}
#upload-button {
    background-color: #4285F4;
    color: white;
}
"""

with gr.Blocks(css=css) as demo:
    with gr.Row(equal_height=True):
        with gr.Column(scale=1, elem_classes="left-sidebar"):
            gr.Markdown("# Miner U")
            gr.Markdown("+ 提取任务")
            gr.Markdown("📄 PDF文档提取")

        with gr.Column(scale=4, elem_classes="main-content"):
            gr.Markdown("< 首页")
            gr.Markdown("# PDF文档提取")
            gr.Markdown("支持文本/扫描型 PDF 解析,识别各类版面元素并转换为多模态 Markdown 格式")

            with gr.Group(elem_classes="upload-area"):
                file_input = gr.File(label="点击上传文件", file_types=[".pdf"])
                with gr.Row():
                    upload_button = gr.Button("点击上传文件", elem_id="upload-button")
                output = gr.Textbox(label="处理结果", visible=False)

            gr.Markdown("请上传5M以内,不超过10页的PDF文档")

    upload_button.click(process_pdf, inputs=file_input, outputs=output)

if __name__ == "__main__":
    demo.launch()

Screenshot

image

Logs

(venv) root@notebook-1725358581-vcpfy-5f4bdb9847-8fvmd:/gwm-tmp/ai_tools/web_ui/mineru-server# python webui.py 
Running on local URL:  http://127.0.0.1:7860

To create a public link, set `share=True` in `launch()`.
Exception in thread Thread-3 (_do_normal_analytics_request):
Traceback (most recent call last):
  File "/gwm-tmp/ai_tools/web_ui/mineru-server/venv/lib/python3.10/site-packages/httpx/_transports/default.py", line 72, in map_httpcore_exceptions
    yield
  File "/gwm-tmp/ai_tools/web_ui/mineru-server/venv/lib/python3.10/site-packages/httpx/_transports/default.py", line 236, in handle_request
    resp = self._pool.handle_request(req)
  File "/gwm-tmp/ai_tools/web_ui/mineru-server/venv/lib/python3.10/site-packages/httpcore/_sync/connection_pool.py", line 216, in handle_request
    raise exc from None
  File "/gwm-tmp/ai_tools/web_ui/mineru-server/venv/lib/python3.10/site-packages/httpcore/_sync/connection_pool.py", line 196, in handle_request
    response = connection.handle_request(
  File "/gwm-tmp/ai_tools/web_ui/mineru-server/venv/lib/python3.10/site-packages/httpcore/_sync/connection.py", line 99, in handle_request
    raise exc
  File "/gwm-tmp/ai_tools/web_ui/mineru-server/venv/lib/python3.10/site-packages/httpcore/_sync/connection.py", line 76, in handle_request
    stream = self._connect(request)
  File "/gwm-tmp/ai_tools/web_ui/mineru-server/venv/lib/python3.10/site-packages/httpcore/_sync/connection.py", line 122, in _connect
    stream = self._network_backend.connect_tcp(**kwargs)
  File "/gwm-tmp/ai_tools/web_ui/mineru-server/venv/lib/python3.10/site-packages/httpcore/_backends/sync.py", line 205, in connect_tcp
    with map_exceptions(exc_map):
  File "/opt/conda/lib/python3.10/contextlib.py", line 153, in __exit__
    self.gen.throw(typ, value, traceback)
  File "/gwm-tmp/ai_tools/web_ui/mineru-server/venv/lib/python3.10/site-packages/httpcore/_exceptions.py", line 14, in map_exceptions
    raise to_exc(exc) from exc
httpcore.ConnectTimeout: timed out

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/opt/conda/lib/python3.10/threading.py", line 1016, in _bootstrap_inner
    self.run()
  File "/opt/conda/lib/python3.10/threading.py", line 953, in run
    self._target(*self._args, **self._kwargs)
  File "/gwm-tmp/ai_tools/web_ui/mineru-server/venv/lib/python3.10/site-packages/gradio/analytics.py", line 70, in _do_normal_analytics_request
    data["ip_address"] = get_local_ip_address()
  File "/gwm-tmp/ai_tools/web_ui/mineru-server/venv/lib/python3.10/site-packages/gradio/analytics.py", line 131, in get_local_ip_address
    ip_address = httpx.get(
  File "/gwm-tmp/ai_tools/web_ui/mineru-server/venv/lib/python3.10/site-packages/httpx/_api.py", line 210, in get
    return request(
  File "/gwm-tmp/ai_tools/web_ui/mineru-server/venv/lib/python3.10/site-packages/httpx/_api.py", line 118, in request
    return client.request(
  File "/gwm-tmp/ai_tools/web_ui/mineru-server/venv/lib/python3.10/site-packages/httpx/_client.py", line 837, in request
    return self.send(request, auth=auth, follow_redirects=follow_redirects)
  File "/gwm-tmp/ai_tools/web_ui/mineru-server/venv/lib/python3.10/site-packages/httpx/_client.py", line 926, in send
    response = self._send_handling_auth(
  File "/gwm-tmp/ai_tools/web_ui/mineru-server/venv/lib/python3.10/site-packages/httpx/_client.py", line 954, in _send_handling_auth
    response = self._send_handling_redirects(
  File "/gwm-tmp/ai_tools/web_ui/mineru-server/venv/lib/python3.10/site-packages/httpx/_client.py", line 991, in _send_handling_redirects
    response = self._send_single_request(request)
  File "/gwm-tmp/ai_tools/web_ui/mineru-server/venv/lib/python3.10/site-packages/httpx/_client.py", line 1027, in _send_single_request
    response = transport.handle_request(request)
  File "/gwm-tmp/ai_tools/web_ui/mineru-server/venv/lib/python3.10/site-packages/httpx/_transports/default.py", line 235, in handle_request
    with map_httpcore_exceptions():
  File "/opt/conda/lib/python3.10/contextlib.py", line 153, in __exit__
    self.gen.throw(typ, value, traceback)
  File "/gwm-tmp/ai_tools/web_ui/mineru-server/venv/lib/python3.10/site-packages/httpx/_transports/default.py", line 89, in map_httpcore_exceptions
    raise mapped_exc(message) from exc
httpx.ConnectTimeout: timed out
Exception in thread Thread-5 (_do_normal_analytics_request):
Traceback (most recent call last):
  File "/gwm-tmp/ai_tools/web_ui/mineru-server/venv/lib/python3.10/site-packages/httpx/_transports/default.py", line 72, in map_httpcore_exceptions
    yield
  File "/gwm-tmp/ai_tools/web_ui/mineru-server/venv/lib/python3.10/site-packages/httpx/_transports/default.py", line 236, in handle_request
    resp = self._pool.handle_request(req)
  File "/gwm-tmp/ai_tools/web_ui/mineru-server/venv/lib/python3.10/site-packages/httpcore/_sync/connection_pool.py", line 216, in handle_request
    raise exc from None
  File "/gwm-tmp/ai_tools/web_ui/mineru-server/venv/lib/python3.10/site-packages/httpcore/_sync/connection_pool.py", line 196, in handle_request
    response = connection.handle_request(
  File "/gwm-tmp/ai_tools/web_ui/mineru-server/venv/lib/python3.10/site-packages/httpcore/_sync/connection.py", line 99, in handle_request
    raise exc
  File "/gwm-tmp/ai_tools/web_ui/mineru-server/venv/lib/python3.10/site-packages/httpcore/_sync/connection.py", line 76, in handle_request
    stream = self._connect(request)
  File "/gwm-tmp/ai_tools/web_ui/mineru-server/venv/lib/python3.10/site-packages/httpcore/_sync/connection.py", line 122, in _connect
    stream = self._network_backend.connect_tcp(**kwargs)
  File "/gwm-tmp/ai_tools/web_ui/mineru-server/venv/lib/python3.10/site-packages/httpcore/_backends/sync.py", line 205, in connect_tcp
    with map_exceptions(exc_map):
  File "/opt/conda/lib/python3.10/contextlib.py", line 153, in __exit__
    self.gen.throw(typ, value, traceback)
  File "/gwm-tmp/ai_tools/web_ui/mineru-server/venv/lib/python3.10/site-packages/httpcore/_exceptions.py", line 14, in map_exceptions
    raise to_exc(exc) from exc
httpcore.ConnectTimeout: timed out

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/opt/conda/lib/python3.10/threading.py", line 1016, in _bootstrap_inner
    self.run()
  File "/opt/conda/lib/python3.10/threading.py", line 953, in run
    self._target(*self._args, **self._kwargs)
  File "/gwm-tmp/ai_tools/web_ui/mineru-server/venv/lib/python3.10/site-packages/gradio/analytics.py", line 70, in _do_normal_analytics_request
    data["ip_address"] = get_local_ip_address()
  File "/gwm-tmp/ai_tools/web_ui/mineru-server/venv/lib/python3.10/site-packages/gradio/analytics.py", line 131, in get_local_ip_address
    ip_address = httpx.get(
  File "/gwm-tmp/ai_tools/web_ui/mineru-server/venv/lib/python3.10/site-packages/httpx/_api.py", line 210, in get
    return request(
  File "/gwm-tmp/ai_tools/web_ui/mineru-server/venv/lib/python3.10/site-packages/httpx/_api.py", line 118, in request
    return client.request(
  File "/gwm-tmp/ai_tools/web_ui/mineru-server/venv/lib/python3.10/site-packages/httpx/_client.py", line 837, in request
    return self.send(request, auth=auth, follow_redirects=follow_redirects)
  File "/gwm-tmp/ai_tools/web_ui/mineru-server/venv/lib/python3.10/site-packages/httpx/_client.py", line 926, in send
    response = self._send_handling_auth(
  File "/gwm-tmp/ai_tools/web_ui/mineru-server/venv/lib/python3.10/site-packages/httpx/_client.py", line 954, in _send_handling_auth
    response = self._send_handling_redirects(
  File "/gwm-tmp/ai_tools/web_ui/mineru-server/venv/lib/python3.10/site-packages/httpx/_client.py", line 991, in _send_handling_redirects
    response = self._send_single_request(request)
  File "/gwm-tmp/ai_tools/web_ui/mineru-server/venv/lib/python3.10/site-packages/httpx/_client.py", line 1027, in _send_single_request
    response = transport.handle_request(request)
  File "/gwm-tmp/ai_tools/web_ui/mineru-server/venv/lib/python3.10/site-packages/httpx/_transports/default.py", line 235, in handle_request
    with map_httpcore_exceptions():
  File "/opt/conda/lib/python3.10/contextlib.py", line 153, in __exit__
    self.gen.throw(typ, value, traceback)
  File "/gwm-tmp/ai_tools/web_ui/mineru-server/venv/lib/python3.10/site-packages/httpx/_transports/default.py", line 89, in map_httpcore_exceptions
    raise mapped_exc(message) from exc
httpx.ConnectTimeout: timed out

System Info

gradio==4.42.0

Severity

I can work around it

C0untFloyd commented 1 week ago

Happening in my app too and it's quite severe. I can't put my finger on it but I suspect more issues coming from the latest fastapi. It seems fastapi's including httptools and dnspython now which the older versions did not. I seem to have solved this issues by forcing fastapi back to 0.103.0 but I'm not certain it's really gone.

callidex commented 1 week ago

What's the workaround, still getting this with 4.43?

taoxiaoxin1688-dev commented 1 week ago

` What's the workaround, still getting this with 4.43?

This is what I've done so far, adding this code at the very beginning when using gradio, it's much better and the program rarely gets network errors.

import socket
from gradio import analytics
def get_local_ip_address():
    try:
        s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        s.connect(("8.8.8.8", 80))
        ip_address = s.getsockname()[0]
        s.close()
    except:
        ip_address = "127.0.0.1"
    return ip_address

def _do_normal_analytics_request(data, topic):  # 添加 topic 参数
    try:
        data["ip_address"] = get_local_ip_address()
        # 如果你想完全禁用网络请求,可以在这里直接返回
        print(f"Analytics request for topic '{topic}' with data: {data}")
        return
    except Exception as e:
        print(f"Analytics request failed for topic '{topic}': {str(e)}")

# 应用猴子补丁
analytics.get_local_ip_address = get_local_ip_address
analytics._do_normal_analytics_request = _do_normal_analytics_request
C0untFloyd commented 1 week ago

But you can completely turn off gradio analytics and it still happens! os.environ['GRADIO_ANALYTICS_ENABLED'] = '0'

taoxiaoxin1688-dev commented 1 week ago

But you can completely turn off gradio analytics and it still happens! os.environ['GRADIO_ANALYTICS_ENABLED'] = '0'

Write about your program, this is so cool, I'll just disable it and it won't report an error

abidlabs commented 5 days ago

Just to confirm, everyone here is seeing these errors only on machines that do not have internet access?

C0untFloyd commented 5 days ago

Just to confirm, everyone here is seeing these errors only on machines that do not have internet access?

No, for me it's also happening on a machine with internet access. I can't really put my finger on it but disabling analytics doesn't do anything for me 😞 I have 2 machines I'm testing my app on and the most noticeable difference is performance. Since upgrading to 4.43 and forcing an older fastapi version the error doesn't happen anymore on the faster pc whereas it almost always happens on the slower one where events take a lot longer. I noticed 1 thing though. If the connection error happened and you just refresh the page, it won't ever happen again in that session. Perhaps that is helping? Btw. I'm using Firefox on both machines, eventually it's a browser issue? Also, I've noticed newer fastapi versions include httptools and dnstools...I don't know if there's a link. Thanks for trying to help!