from flask import Flask, request, jsonify import requests import os app = Flask(__name__) # Base URL for the proxied API BASE_URL = "https://api.chatanywhere.org/" # API Key from environment variables API_KEY = os.getenv("openaikey") if not API_KEY: raise ValueError("API key not found in environment variables") @app.route('/', defaults={'path': ''}, methods=['GET', 'POST', 'PUT', 'DELETE', 'PATCH']) @app.route('/', methods=['GET', 'POST', 'PUT', 'DELETE', 'PATCH']) def proxy(path): # Build the proxied URL proxied_url = BASE_URL + path # Retrieve the request parameters params = request.args.to_dict() # Retrieve the request body data = request.get_data() if request.method in ['POST', 'PUT', 'PATCH'] else None # Retrieve headers and add the Authorization header headers = {key: value for key, value in request.headers if key != 'Host'} headers['Authorization'] = f"Bearer {API_KEY}" # Forward the request to the proxied API try: response = requests.request( method=request.method, url=proxied_url, params=params, data=data, headers=headers ) # Return the response to the client return (response.content, response.status_code, response.headers.items()) except requests.exceptions.RequestException as e: return jsonify({"error": str(e)}), 500 if __name__ == '__main__': app.run(host='0.0.0.0', port=7860, debug=True)