From 10bd63ccd281f79fed24772d10e0d30a7a42d82c Mon Sep 17 00:00:00 2001 From: huang qiwei Date: Mon, 5 Aug 2024 06:03:31 +0000 Subject: [PATCH] Add cors --- .devcontainer/devcontainer.json | 2 +- Dockerfile | 2 +- app.py | 9 +++++++-- requirements.txt | 6 ++++++ 4 files changed, 15 insertions(+), 4 deletions(-) create mode 100644 requirements.txt diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json index 8228268..5dc1017 100644 --- a/.devcontainer/devcontainer.json +++ b/.devcontainer/devcontainer.json @@ -12,7 +12,7 @@ // "forwardPorts": [], // Use 'postCreateCommand' to run commands after the container is created. - "postCreateCommand": "pip3 install --no-cache-dir flask requests openai opentelemetry-instrumentation-flask gunicorn" + "postCreateCommand": "pip3 install -r requirements.txt" // Configure tool-specific properties. // "customizations": {}, diff --git a/Dockerfile b/Dockerfile index 147593c..3d45e7b 100644 --- a/Dockerfile +++ b/Dockerfile @@ -8,7 +8,7 @@ WORKDIR /app COPY . /app # Install any needed packages specified in requirements.txt -RUN pip install --no-cache-dir flask requests openai opentelemetry-instrumentation-flask gunicorn +RUN pip install -r requirements.txt # Make port 80 available to the world outside this container EXPOSE 8000 diff --git a/app.py b/app.py index aec41c9..7127a21 100644 --- a/app.py +++ b/app.py @@ -1,6 +1,7 @@ import os from flask import Flask, request, Response, jsonify +from flask_cors import CORS import requests import json @@ -10,6 +11,8 @@ FlaskInstrumentor().instrument_app(app) +CORS(app) + default_resource = "default_resource" # format of the resource_mapper is {"deployment name": "openai resource name"}, e.g. {"gpt-4": "azureopenai1"} @@ -82,7 +85,8 @@ def handler(path): return 'Not Found', 404 resource = resource_mapper[deployment] - request_url = f"https://{resource}.openai.azure.com/openai/deployments/{deployment}/{path}?api-version={api_version}" + request_url = f"https://{resource}.openai.azure.com/openai/deployments/{ + deployment}/{path}?api-version={api_version}" headers = {'api-key': resource_keys[resource]} for key, value in request.headers.items(): @@ -139,7 +143,8 @@ def get_models(): if __name__ == '__main__': if os.getenv('MODEL_MAPPER') is None or os.getenv('RESOURCE_MAPPER') is None: - raise ValueError("MODEL_MAPPER and RESOURCE_MAPPER environment variables must be set") + raise ValueError( + "MODEL_MAPPER and RESOURCE_MAPPER environment variables must be set") if os.getenv('KEYS_MAPPER') is None: raise ValueError("KEYS_MAPPER environment variable must be set") app.run(host='0.0.0.0', port=8000, debug=True) diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..2b69ed0 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,6 @@ +flask +flask_cors +requests +openai +opentelemetry-instrumentation-flask +gunicorn \ No newline at end of file