sshh12 / llm_convo

Use ChatGPT over Twilio to create an AI phone agent (works for incoming or outgoing calls).
MIT License
89 stars 19 forks source link

Deployment on ubuntu server #7

Closed Shakirsadiq6 closed 3 months ago

Shakirsadiq6 commented 7 months ago

I need to deploy the app on the server but I am not able to do it as I was using gunicorn and nginx to deploy. Could you please guide me for its deployment, so I can use my domain name instead of the ngrok.

I tried the below changes but it didn't work I got port already in use error at WSGIServer line

class TwilioServer:
    def __init__(self, remote_host: str, port: int, static_dir: str, existing_flask_app):
        self.app = existing_flask_app
tws = TwilioServer(remote_host=remote_host, port=port, static_dir=static_dir, existing_flask_app=existing_app)
if __name__ == "__main__":
    logging.getLogger().setLevel(logging.INFO)
    remote_host, remote_port = "localhost", 8500
    deploy_app = Flask(__name__)

    main(remote_port, remote_port , False, deploy_app)

    # Run the Flask app
    deploy_app.run(debug=True, port=8500)
sshh12 commented 7 months ago

Hey! feel free to post the error message and I can help clarify what needs to be changed

Shakirsadiq6 commented 7 months ago

Hi @sshh12 Thanks for replying

I tried to run the code using gunicorn for the deployment purpose: I ran this command: gunicorn -k gevent main:TwilioServer

main is the name of my py file which consists the code of examples folder of this repository.

The server started but I got 500 error:

Traceback (most recent call last):
  File "/Users/apple/Desktop/December/xincon-calling-bot-twilio/env/lib/python3.10/site-packages/gunicorn/workers/base_async.py", line 55, in handle
    self.handle_request(listener_name, req, client, addr)
  File "/Users/apple/Desktop/December/xincon-calling-bot-twilio/env/lib/python3.10/site-packages/gunicorn/workers/ggevent.py", line 128, in handle_request
    super().handle_request(listener_name, req, sock, addr)
  File "/Users/apple/Desktop/December/xincon-calling-bot-twilio/env/lib/python3.10/site-packages/gunicorn/workers/base_async.py", line 108, in handle_request
    respiter = self.wsgi(environ, resp.start_response)
TypeError: TwilioServer.__init__() missing 1 required positional argument: 'static_dir'
[2023-12-18 11:03:54 +0530] [4897] [ERROR] Error handling request /
Traceback (most recent call last):
  File "/Users/apple/Desktop/December/xincon-calling-bot-twilio/env/lib/python3.10/site-packages/gunicorn/workers/base_async.py", line 55, in handle
    self.handle_request(listener_name, req, client, addr)
  File "/Users/apple/Desktop/December/xincon-calling-bot-twilio/env/lib/python3.10/site-packages/gunicorn/workers/ggevent.py", line 128, in handle_request
    super().handle_request(listener_name, req, sock, addr)
  File "/Users/apple/Desktop/December/xincon-calling-bot-twilio/env/lib/python3.10/site-packages/gunicorn/workers/base_async.py", line 108, in handle_request
    respiter = self.wsgi(environ, resp.start_response)
TypeError: TwilioServer.__init__() missing 1 required positional argument: 'static_dir'
[2023-12-18 11:03:54 +0530] [4897] [ERROR] Error handling request /favicon.ico
Traceback (most recent call last):
  File "/Users/apple/Desktop/December/xincon-calling-bot-twilio/env/lib/python3.10/site-packages/gunicorn/workers/base_async.py", line 55, in handle
    self.handle_request(listener_name, req, client, addr)
  File "/Users/apple/Desktop/December/xincon-calling-bot-twilio/env/lib/python3.10/site-packages/gunicorn/workers/ggevent.py", line 128, in handle_request
    super().handle_request(listener_name, req, sock, addr)
  File "/Users/apple/Desktop/December/xincon-calling-bot-twilio/env/lib/python3.10/site-packages/gunicorn/workers/base_async.py", line 108, in handle_request
    respiter = self.wsgi(environ, resp.start_response)
TypeError: TwilioServer.__init__() missing 1 required positional argument: 'static_dir'
Shakirsadiq6 commented 6 months ago

@sshh12 could you please me with this?

sshh12 commented 6 months ago

Hey @Shakirsadiq6, I'm still traveling for the holidays. Will get back to you in a week or so.

Shakirsadiq6 commented 6 months ago

Sure thanks @sshh12 I'll waiting for you response.

Enjoy your holidays!

sshh12 commented 6 months ago

Hm ok this might be tricky. I'm not sure if some of the way this was written will work with guincorn/gevent so my advice would be just to use this as-is and if you want parallel serving you could run multiple instances with different local ports. Then use something like nginx to load the balance between these. Will also note that you do not need ngrok for your own domain name as long as the flask server (or nginx) is exposed to the internet.

As for this specific exception, I think this issue is the command main:TwilioServer, the second part should be the module of the wsgi server so potentially main:deploy_app.

Shakirsadiq6 commented 6 months ago

I actually saw the other issue on this repo where you said to do the parallel serving. Indeed I don't want to use the ngrok, I want to use my own domain. Both main:TwilioServer and main:deploy_app didn't work.

Is there any way by which I can deploy it? With or without gunicorn

sshh12 commented 6 months ago

Just want to clarify, you don't need gunicorn to use this with your own domain without ngrok.

For your domain, you can just set TwilioServer port to 80 and then point your domain to your server's public IP address (e.g., with an A record). No code change is required.

Shakirsadiq6 commented 3 months ago

Hi @sshh12 I was using the ngrok as my issue for the deployment wasn't fixed but I had some issues while using ngrok. I tried to use my public IP and domain also. But I am facing 405 error on incoming-voice route. But if I pass both the methods ["GET", "POST"], it doesn't throw an error but also doesn't respond with anything:

GET /incoming-voice?msg=Gather+End&Called=%2B15169166898&Digits=1&ToState=NY&CallerCountry=US&Direction=inbound&CallerState=NY&ToZip=&CallSid=CA039d114cc39d45540c0947096312d6f8&To=%2B15169166898&CallerZip=10118&ToCountry=US&FinishedOnKey=&CalledZip=&ApiVersion=2010-04-01&CalledCity=&CallStatus=in-progress&From=%2B12125609218&AccountSid=ACf5e9dcf64ba02a384e75c68198a9dcec&CalledCountry=US&CallerCity=NEW+YORK&ToCity=&FromCountry=US&Caller=%2B12125609218&FromCity=NEW+YORK&CalledState=NY&FromZip=10118&FromState=NY HTTP/1.1" 405 308 0.000808
sshh12 commented 3 months ago

I would double check your twilio settings, this is what I'm using:

chrome_VZSfJHN6FV

From what you posted it looks like it's using a GET whereas code is written for POST only.

Shakirsadiq6 commented 3 months ago

OK @sshh12 thanks for the response. I have same things selected in the twilio and I have modified your code a lot. I have a language selection panel and then once a user selects the language they get redirected to a new url where they start conversation with my custom LLM. I think maybe I have some issue with the code which I have added over your code.

Thanks for the help. I'll let you know if I need any further help.