504 Gateway Timeout with http request

Hi.

Yes, I did solve it. But I am not using spaces anymore as I need much more powerful hardware.

But I can give you the solution.

I just used the socket as recommended here by Chris-rannou.

I don’t know exactly what language you need to have the client in, but for python, it’s just this.

Server:

#!/usr/bin/env python

import asyncio
import websockets

async def echo(websocket):
    async for message in websocket:
        await websocket.send(message)

async def main():
    async with websockets.serve(echo, "localhost", 8765):
        await asyncio.Future()  # run forever

asyncio.run(main())

Client

#!/usr/bin/env python

import asyncio
import websockets

async def hello():
    async with websockets.connect("ws://localhost:8765") as websocket:
        await websocket.send("Hello world!")
        await websocket.recv()

asyncio.run(hello())

Took from websockets for python documentation here.

You still need to access the server from outside. I used ngrok tunnel that’s running on the same port as the server (for example, 8000). Then you can use gradio, which has its address static to get the ngrok URL automatically anytime you reboot the space. Please note that you need to create a free account on ngrok to run the tunnel for unlimited time. With the completely free account, you can get 1 tunnel with unlimited time.

I used this with my space (it’s no longer accessible, I made it private), so this is my system implementation. It might not be perfect, but it’s working.

from fastapi import FastAPI, WebSocket
import websockets
import socket
import whisper
import asyncio
import base64
import gradio as gr
import threading
from pyngrok import ngrok
import gradio as gr


ngrok.set_auth_token("Your token here")
ngrok.kill()
ngrok_tunnel = ngrok.connect(8000)
print(ngrok_tunnel.public_url)


model = whisper.load_model("large")


def transcribe(path):
    return model.transcribe(path)["text"]



def base64decode(path):
    return base64.b64decode(path)
    
def ng_path(text):
    return ngrok_tunnel.public_url





async def handler(websocket):
    async for message in websocket:
        data = base64decode(message)
        open('audio.wav', 'wb').write(data)
        transcript = transcribe('audio.wav')
        print("transcribing is done")
        await websocket.send(transcript)
        await websocket.close(reason="Sending done")


start_server = websockets.serve(handler, "127.0.0.1", 8000, max_size=2**30,max_queue=2**5,close_timeout=60**2,ping_timeout=60**2)
asyncio.get_event_loop().run_until_complete(start_server)
threading.Thread(target=asyncio.get_event_loop().run_forever).start()
interface = gr.Interface(inputs="text",outputs="text", fn=ng_path, title="X", description="XX", allow_flagging=False)
interface.launch()

You can get the gradio API address by clicking the API docs link on the main space page.

Good luck with your project!

2 Likes