Update app.py

1. Improved error handling in send_msg to log specific exceptions.

2. Changed logging level to WARNING for better visibility.

3. Removed the list for port and used the port directly from the argument.
This commit is contained in:
SMIT PATEL 2024-10-27 21:38:54 +05:30 committed by GitHub
parent 726cc339bc
commit ca42998bbf
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -9,14 +9,13 @@ app.logger.setLevel(logging.ERROR)
log = logging.getLogger('werkzeug') log = logging.getLogger('werkzeug')
log.setLevel(logging.ERROR) log.setLevel(logging.ERROR)
messages = [] messages = []
port = [8000]
def send_msg(role, text): def send_msg(role, text):
try: try:
data = {"role": role, "text": text} data = {"role": role, "text": text}
response = requests.post(f"http://127.0.0.1:{port[-1]}/send_message", json=data) response = requests.post(f"http://127.0.0.1:{port}/send_message", json=data) # Updated to use port directly
except: except Exception as e: # Improved error handling
logging.info("flask app.py did not start for online log") logging.error(f"Error sending message: {e}")
@app.route("/") @app.route("/")
@ -63,6 +62,6 @@ if __name__ == "__main__":
parser = argparse.ArgumentParser(description='argparse') parser = argparse.ArgumentParser(description='argparse')
parser.add_argument('--port', type=int, default=8000, help="port") parser.add_argument('--port', type=int, default=8000, help="port")
args = parser.parse_args() args = parser.parse_args()
port.append(args.port) port = args.port # Removed list and used port directly
print(f"Please visit http://127.0.0.1:{port[-1]}/ for the front-end display page. \nIn the event of a port conflict, please modify the port argument (e.g., python3 app.py --port 8012).") print(f"Please visit http://127.0.0.1:{port}/ for the front-end display page. \nIn the event of a port conflict, please modify the port argument (e.g., python3 app.py --port 8012).")
app.run(host='0.0.0.0', debug=False, port=port[-1]) app.run(host='0.0.0.0', debug=False, port=port) # Updated to use port directly