Spaces:
Runtime error
Runtime error
DeathDaDev
commited on
Commit
•
598538d
1
Parent(s):
ae00b8b
Upload flask-app.py
Browse files- flask-app.py +34 -0
flask-app.py
ADDED
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from flask import Flask, request, jsonify
|
2 |
+
import requests
|
3 |
+
|
4 |
+
app = Flask(__name__)
|
5 |
+
OLLAMA_API = "http://localhost:11434"
|
6 |
+
|
7 |
+
@app.route('/')
|
8 |
+
def home():
|
9 |
+
return "Ollama API is running. Use /api/* endpoints to access Ollama."
|
10 |
+
|
11 |
+
@app.route('/api/<path:path>', methods=['GET', 'POST', 'PUT', 'DELETE'])
|
12 |
+
def proxy(path):
|
13 |
+
url = f"{OLLAMA_API}/{path}"
|
14 |
+
resp = requests.request(
|
15 |
+
method=request.method,
|
16 |
+
url=url,
|
17 |
+
headers={key: value for (key, value) in request.headers if key != 'Host'},
|
18 |
+
data=request.get_data(),
|
19 |
+
cookies=request.cookies,
|
20 |
+
allow_redirects=False)
|
21 |
+
|
22 |
+
excluded_headers = ['content-encoding', 'content-length', 'transfer-encoding', 'connection']
|
23 |
+
headers = [(name, value) for (name, value) in resp.raw.headers.items()
|
24 |
+
if name.lower() not in excluded_headers]
|
25 |
+
|
26 |
+
response = app.response_class(
|
27 |
+
response=resp.content,
|
28 |
+
status=resp.status_code,
|
29 |
+
headers=headers,
|
30 |
+
)
|
31 |
+
return response
|
32 |
+
|
33 |
+
if __name__ == '__main__':
|
34 |
+
app.run(host='0.0.0.0', port=5000)
|