# 504 Gateway Timeout with http request

**URL:** <https://discuss.huggingface.co/t/504-gateway-timeout-with-http-request/24018>\
**Category:** Spaces\
**Created:** [October 5, 2022, 11:27am UTC](https://discuss.huggingface.co/t/504-gateway-timeout-with-http-request/24018 "2022-10-05T11:27:03Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Skyrr](https://avatars.discourse-cdn.com/v4/letter/s/97f17d/32.png) [@Skyrr](https://discuss.huggingface.co/u/Skyrr)\
**Post date:** [October 5, 2022, 11:27am UTC](https://discuss.huggingface.co/t/504-gateway-timeout-with-http-request/24018/1 "2022-10-05T11:27:03Z")

</div>

I created space using Gradio, because I really need the API functionality. In app.py I am simply loading OpenAI’s whisper and getting predictions from audio. But always, when the request takes longer than a minute I get 504. No matter what I do. I tried setting enable\_queue param (but that’s enabled by default), using the queue method, setting timeouts on the client, changing clients (from flutter http to python requests) and using fastapi in gradio space. Nothing worked so far. The error is from nginx so I think there is some set timeout, that I cannot change. MAYBE I could use set command to change the /etc/… nginx conf from simple subprocces command, but I am not sure that that would work and I also think it should work by default or be easily changable. I would really appreciate some response since I’ve been struggling with this for a few days now.

---

<div class="post-metadata">

**Author:** ![chris-rannou](https://avatars.discourse-cdn.com/v4/letter/c/7feea3/32.png) [@chris-rannou](https://discuss.huggingface.co/u/chris-rannou)\
**Post date:** [October 6, 2022, 7:46am UTC](https://discuss.huggingface.co/t/504-gateway-timeout-with-http-request/24018/2 "2022-10-06T07:46:56Z")

</div>

Hello @Skyrr,  
Could you provide a link to the failed space please ?

---

<div class="post-metadata">

**Author:** ![Skyrr](https://avatars.discourse-cdn.com/v4/letter/s/97f17d/32.png) [@Skyrr](https://discuss.huggingface.co/u/Skyrr)\
**Post date:** [October 6, 2022, 8:43am UTC](https://discuss.huggingface.co/t/504-gateway-timeout-with-http-request/24018/3 "2022-10-06T08:43:07Z")

</div>

Yes of course. Sorry I forgot that. Here: [OpenAIWhisperLargePredictions - a Hugging Face Space by Skyrr](https://huggingface.co/spaces/Skyrr/OpenAIWhisperLargePredictions)

Right now I am using FastAPI and the API address is:  
[https://hf.space/embed/Skyrr/OpenAIWhisperLargePredictions/notes](https://hf.space/embed/Skyrr/OpenAIWhisperLargePredictions/notes)

Thank you!

---

<div class="post-metadata">

**Author:** ![chris-rannou](https://avatars.discourse-cdn.com/v4/letter/c/7feea3/32.png) [@chris-rannou](https://discuss.huggingface.co/u/chris-rannou)\
**Post date:** [October 6, 2022, 10:10am UTC](https://discuss.huggingface.co/t/504-gateway-timeout-with-http-request/24018/4 "2022-10-06T10:10:17Z")

</div>

The API seems to reply for me.  
Though your endpoint seems to require a body and you defined a `GET` method for `/notes`

---

<div class="post-metadata">

**Author:** ![chris-rannou](https://avatars.discourse-cdn.com/v4/letter/c/7feea3/32.png) [@chris-rannou](https://discuss.huggingface.co/u/chris-rannou)\
**Post date:** [October 6, 2022, 10:11am UTC](https://discuss.huggingface.co/t/504-gateway-timeout-with-http-request/24018/5 "2022-10-06T10:11:00Z")

</div>

Changing `@app.get("/notes")` to `@app.post("/notes")` should allow you to post to this endpoint

---

<div class="post-metadata">

**Author:** ![Skyrr](https://avatars.discourse-cdn.com/v4/letter/s/97f17d/32.png) [@Skyrr](https://discuss.huggingface.co/u/Skyrr)\
**Post date:** [October 6, 2022, 10:19am UTC](https://discuss.huggingface.co/t/504-gateway-timeout-with-http-request/24018/6 "2022-10-06T10:19:31Z")

</div>

So it replies for you even when it takes longer than a minute?

---

<div class="post-metadata">

**Author:** ![Skyrr](https://avatars.discourse-cdn.com/v4/letter/s/97f17d/32.png) [@Skyrr](https://discuss.huggingface.co/u/Skyrr)\
**Post date:** [October 6, 2022, 10:20am UTC](https://discuss.huggingface.co/t/504-gateway-timeout-with-http-request/24018/7 "2022-10-06T10:20:17Z")

</div>

I used post with gradio’s default API. The problem was still there. But I can try post with FastAPI.

---

<div class="post-metadata">

**Author:** ![Skyrr](https://avatars.discourse-cdn.com/v4/letter/s/97f17d/32.png) [@Skyrr](https://discuss.huggingface.co/u/Skyrr)\
**Post date:** [October 6, 2022, 10:29am UTC](https://discuss.huggingface.co/t/504-gateway-timeout-with-http-request/24018/8 "2022-10-06T10:29:21Z")

</div>

Tried that, still getting the same error. Gateway timeout after a minute.

---

<div class="post-metadata">

**Author:** ![chris-rannou](https://avatars.discourse-cdn.com/v4/letter/c/7feea3/32.png) [@chris-rannou](https://discuss.huggingface.co/u/chris-rannou)\
**Post date:** [October 6, 2022, 10:36am UTC](https://discuss.huggingface.co/t/504-gateway-timeout-with-http-request/24018/9 "2022-10-06T10:36:28Z")

</div>

Could you provide a sample body so that I can try the requests on my side.  
It seems your call triggers a 500 on your api, might be linked

---

<div class="post-metadata">

**Author:** ![Skyrr](https://avatars.discourse-cdn.com/v4/letter/s/97f17d/32.png) [@Skyrr](https://discuss.huggingface.co/u/Skyrr)\
**Post date:** [October 6, 2022, 10:54am UTC](https://discuss.huggingface.co/t/504-gateway-timeout-with-http-request/24018/10 "2022-10-06T10:54:49Z")

</div>

Sure thing.  
Flutter:

```auto
final url = Uri.parse('https://hf.space/embed/Skyrr/OpenAIWhisperLargePredictions/+/api/predict');
    final response = await http.post(url,
        headers: {
          'Content-Type': 'application/json',
          'Accept': 'application/json',
        },
        body: jsonEncode(<String, List<dynamic>>{
        "data": [
        {"name": fileName, "data": readAudioFile(path)}
        ]
        }),encoding: Encoding.getByName('utf-8'))
    .timeout(const Duration(seconds: 60 * 60));

String readAudioFile(String path) {
  final file = io.File(path);
  List<int> bytes = file.readAsBytesSync();
  String base64String = base64Encode(bytes);
  final fileString = 'data:audio/wav;base64,$base64String';
  return fileString;
}

```

Python requests:

```auto
import requests
import base64

def main():
    url = "https://hf.space/embed/Skyrr/OpenAIWhisperLargePredictions/notes"
    headers = {
        "Content-Type": "application/json",
        "Encoding": "utf-8",
        "Accept": "application/json",
        }

    
    data = {"audio": base64decode(r"C:\Users\skirl\Downloads\1664830934891.wav")}
    
    response = requests.post(url, headers=headers, json=data,timeout=3600) 
    
    print(response.text)

def base64decode(path):
    return base64.b64encode(open(path, "rb").read()).decode("utf-8")

if __name__ == " __main__":
    main()

```

Please note that I didn’t yet update the url in flutter.

---

<div class="post-metadata">

**Author:** ![chris-rannou](https://avatars.discourse-cdn.com/v4/letter/c/7feea3/32.png) [@chris-rannou](https://discuss.huggingface.co/u/chris-rannou)\
**Post date:** [October 6, 2022, 11:14am UTC](https://discuss.huggingface.co/t/504-gateway-timeout-with-http-request/24018/11 "2022-10-06T11:14:07Z")

</div>

So there is indeed a timeout after 1 min.  
Within gradio when using the UI the call is not done directly but a websocket is open and the call down internally, the result is then sent to the websocket

---

<div class="post-metadata">

**Author:** ![Skyrr](https://avatars.discourse-cdn.com/v4/letter/s/97f17d/32.png) [@Skyrr](https://discuss.huggingface.co/u/Skyrr)\
**Post date:** [October 6, 2022, 11:33am UTC](https://discuss.huggingface.co/t/504-gateway-timeout-with-http-request/24018/12 "2022-10-06T11:33:46Z")

</div>

Any ideas how to solve or bypass that?

---

<div class="post-metadata">

**Author:** ![chris-rannou](https://avatars.discourse-cdn.com/v4/letter/c/7feea3/32.png) [@chris-rannou](https://discuss.huggingface.co/u/chris-rannou)\
**Post date:** [October 6, 2022, 11:44am UTC](https://discuss.huggingface.co/t/504-gateway-timeout-with-http-request/24018/13 "2022-10-06T11:44:41Z")

</div>

The timeout is actually expected to prevent requests from hanging forever in the event of the space is crashed and cannot restart.  
Using the gradio interface directly, do you also experiment the 504 or only when directly querying the API ?

---

<div class="post-metadata">

**Author:** ![Skyrr](https://avatars.discourse-cdn.com/v4/letter/s/97f17d/32.png) [@Skyrr](https://discuss.huggingface.co/u/Skyrr)\
**Post date:** [October 6, 2022, 11:47am UTC](https://discuss.huggingface.co/t/504-gateway-timeout-with-http-request/24018/14 "2022-10-06T11:47:14Z")

</div>

Only with the API. Interface works fine. Are you saying that I can open a socket connection, send request and just listen for response?

---

<div class="post-metadata">

**Author:** ![chris-rannou](https://avatars.discourse-cdn.com/v4/letter/c/7feea3/32.png) [@chris-rannou](https://discuss.huggingface.co/u/chris-rannou)\
**Post date:** [October 6, 2022, 12:34pm UTC](https://discuss.huggingface.co/t/504-gateway-timeout-with-http-request/24018/15 "2022-10-06T12:34:51Z")

</div>

Yes, it seems that’s what gradio is doing

---

<div class="post-metadata">

**Author:** ![Skyrr](https://avatars.discourse-cdn.com/v4/letter/s/97f17d/32.png) [@Skyrr](https://discuss.huggingface.co/u/Skyrr)\
**Post date:** [October 6, 2022, 12:39pm UTC](https://discuss.huggingface.co/t/504-gateway-timeout-with-http-request/24018/16 "2022-10-06T12:39:49Z")

</div>

Okay. Thank you so much for helping me solve this.

---

<div class="post-metadata">

**Author:** ![freddyaboulton](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/freddyaboulton/32/32732_2.png) [@freddyaboulton](https://discuss.huggingface.co/u/freddyaboulton)\
**Post date:** [October 6, 2022, 5:30pm UTC](https://discuss.huggingface.co/t/504-gateway-timeout-with-http-request/24018/17 "2022-10-06T17:30:54Z")

</div>

Hi @Skyrr ! Thank you for posting and thank you @chris-rannou for helping.

If you want to use a space programmatically from another space, the best way is the `load` method on `gradio.Blocks`.

So if you do something like `other_app = gr.Blocks.load("spaces/<your-space>")` , you can use `other_app` like a regular python function. For example , `output = other_app(<input>, fn_index=0)`. This will take care of creating the websocket for you.

Here is an example using this approach to call stable diffusion from another app: [Using loaded interface as a function in Blocks return error · Issue #2369 · gradio-app/gradio · GitHub](https://github.com/gradio-app/gradio/issues/2369#issuecomment-1268766733)

Here is a guide talking about this more in depth: [Using Blocks Like Functions](https://gradio.app/using_blocks_like_functions/)

---

<div class="post-metadata">

**Author:** ![Skyrr](https://avatars.discourse-cdn.com/v4/letter/s/97f17d/32.png) [@Skyrr](https://discuss.huggingface.co/u/Skyrr)\
**Post date:** [October 7, 2022, 6:43am UTC](https://discuss.huggingface.co/t/504-gateway-timeout-with-http-request/24018/18 "2022-10-07T06:43:29Z")

</div>

Thank you for suggestion. The problem is that I need to connect to the app from flutter. I actually decieded that easiest option will be to just use whisper-large repo directly with inference api. Only issue there is that it’s often overloaded. I will se how to solve that. Maybe later I’ll use inference endpoints.

---

<div class="post-metadata">

**Author:** ![canonsky1992](https://avatars.discourse-cdn.com/v4/letter/c/4491bb/32.png) [@canonsky1992](https://discuss.huggingface.co/u/canonsky1992)\
**Post date:** [October 23, 2022, 1:26pm UTC](https://discuss.huggingface.co/t/504-gateway-timeout-with-http-request/24018/19 "2022-10-23T13:26:14Z")

</div>

Have you solved the problem yet? I’m having a 504 time out problem. When I request input to space, the result returned is as follows:  
b’\r\n504 Gateway Time-out\r\n\r\n

# 504 Gateway Time-out
\r\n\r\n\r\n’.  
If you have solved it please help me. Thank you

---

<div class="post-metadata">

**Author:** ![canonsky1992](https://avatars.discourse-cdn.com/v4/letter/c/4491bb/32.png) [@canonsky1992](https://discuss.huggingface.co/u/canonsky1992)\
**Post date:** [October 23, 2022, 1:28pm UTC](https://discuss.huggingface.co/t/504-gateway-timeout-with-http-request/24018/20 "2022-10-23T13:28:32Z")

</div>

![image](https://us1.discourse-cdn.com/hellohellohello/original/2X/b/b79ce55084b314794a7041060f7320020da14f07.png)

[Next page](https://discuss.huggingface.co/t/504-gateway-timeout-with-http-request/24018.md?page=2)
