app / app.py
GamerC0der's picture
Update app.py
292ad88 verified
from flask import Flask, request, jsonify
import requests
import json
app = Flask(__name__)
@app.route('/v1/chat/completions', methods=['POST'])
def chat_completions():
try:
response = requests.post(
'https://text.pollinations.ai/openai',
json=request.get_json(),
headers={
'Content-Type': 'application/json',
'User-Agent': request.headers.get('User-Agent', 'Flask Proxy')
}
)
response_data = response.json()
if 'model' in response_data and response_data['model'] == 'instant':
response_data['model'] = 'openai'
return jsonify(response_data), response.status_code
except requests.RequestException as e:
return jsonify({'error': 'Proxy request failed', 'details': str(e)}), 500
except json.JSONDecodeError:
return jsonify({'error': 'Invalid JSON response from target server'}), 500
if __name__ == '__main__':
app.run(host='localhost', port=5000, debug=True)