ollama-server / flask-app.py
DeathDaDev's picture
style: format code consistently and manage headers correctly in flask-app.py
4e482de
from flask import Flask, request, jsonify
import requests
app = Flask(__name__)
OLLAMA_API = "http://localhost:11434"
@app.route('/')
def home():
return "Ollama API is running. Use /api/* endpoints to access Ollama."
@app.route('/api/<path:path>', methods=['GET', 'POST', 'PUT', 'DELETE'])
def proxy(path):
url = f"{OLLAMA_API}/{path}"
resp = requests.request(
method=request.method,
url=url,
headers={key: value for (key, value) in request.headers if key != 'Host'},
data=request.get_data(),
cookies=request.cookies,
allow_redirects=False
)
excluded_headers = ['content-encoding', 'content-length', 'transfer-encoding', 'connection']
headers = [
(name, value) for (name, value) in resp.raw.headers.items()
if name.lower() not in excluded_headers
]
response = app.response_class(
response=resp.content,
status=resp.status_code,
headers=headers,
)
return response
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000)