Spaces:
Sleeping
Sleeping
yousuff.s commited on
Commit ·
c38633f
1
Parent(s): bdf1636
add Flask app and entrypoint script for Ollama service
Browse files- Dockerfile +3 -69
- app.py +74 -0
- entrypoint.sh +20 -0
Dockerfile
CHANGED
|
@@ -2,80 +2,14 @@ FROM ollama/ollama:latest
|
|
| 2 |
|
| 3 |
WORKDIR /app
|
| 4 |
|
| 5 |
-
# Install dependencies
|
| 6 |
RUN apt-get update && apt-get install -y curl python3 python3-pip && \
|
| 7 |
rm -rf /var/lib/apt/lists/*
|
| 8 |
|
| 9 |
-
# Install Flask
|
| 10 |
RUN pip3 install flask requests --break-system-packages
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
'import requests' \
|
| 16 |
-
'' \
|
| 17 |
-
'app = Flask(__name__)' \
|
| 18 |
-
'OLLAMA_URL = "http://localhost:11434"' \
|
| 19 |
-
'' \
|
| 20 |
-
'@app.route("/")' \
|
| 21 |
-
'def home():' \
|
| 22 |
-
' return """<!DOCTYPE html>' \
|
| 23 |
-
'<html><head><title>Ollama API</title></head>' \
|
| 24 |
-
'<body style="font-family:Arial;padding:50px;text-align:center;">' \
|
| 25 |
-
'<h1>🦙 Ollama API Running!</h1>' \
|
| 26 |
-
'<p>Model: llama3.2:3b</p>' \
|
| 27 |
-
'<h3>Test with:</h3>' \
|
| 28 |
-
'<pre style="background:#f4f4f4;padding:20px;text-align:left;">curl -X POST https://YOUR-SPACE.hf.space/api/generate \\' \
|
| 29 |
-
' -H "Content-Type: application/json" \\' \
|
| 30 |
-
' -d '"'"'{"model":"llama3.2:3b","prompt":"Hello!","stream":false}'"'"'</pre>' \
|
| 31 |
-
'</body></html>"""' \
|
| 32 |
-
'' \
|
| 33 |
-
'@app.route("/api/generate", methods=["POST"])' \
|
| 34 |
-
'def generate():' \
|
| 35 |
-
' try:' \
|
| 36 |
-
' data = request.json' \
|
| 37 |
-
' resp = requests.post(f"{OLLAMA_URL}/api/generate", json=data, stream=data.get("stream", False))' \
|
| 38 |
-
' if data.get("stream", False):' \
|
| 39 |
-
' def gen():' \
|
| 40 |
-
' for line in resp.iter_lines():' \
|
| 41 |
-
' if line: yield line + b"\\n"' \
|
| 42 |
-
' return Response(gen(), content_type="application/x-ndjson")' \
|
| 43 |
-
' return jsonify(resp.json())' \
|
| 44 |
-
' except Exception as e:' \
|
| 45 |
-
' return jsonify({"error": str(e)}), 500' \
|
| 46 |
-
'' \
|
| 47 |
-
'@app.route("/api/chat", methods=["POST"])' \
|
| 48 |
-
'def chat():' \
|
| 49 |
-
' try:' \
|
| 50 |
-
' data = request.json' \
|
| 51 |
-
' resp = requests.post(f"{OLLAMA_URL}/api/chat", json=data, stream=data.get("stream", False))' \
|
| 52 |
-
' if data.get("stream", False):' \
|
| 53 |
-
' def gen():' \
|
| 54 |
-
' for line in resp.iter_lines():' \
|
| 55 |
-
' if line: yield line + b"\\n"' \
|
| 56 |
-
' return Response(gen(), content_type="application/x-ndjson")' \
|
| 57 |
-
' return jsonify(resp.json())' \
|
| 58 |
-
' except Exception as e:' \
|
| 59 |
-
' return jsonify({"error": str(e)}), 500' \
|
| 60 |
-
'' \
|
| 61 |
-
'if __name__ == "__main__":' \
|
| 62 |
-
' app.run(host="0.0.0.0", port=7860)' \
|
| 63 |
-
> /app/app.py
|
| 64 |
-
|
| 65 |
-
# Create startup script
|
| 66 |
-
RUN printf '%s\n' \
|
| 67 |
-
'#!/bin/bash' \
|
| 68 |
-
'set -e' \
|
| 69 |
-
'echo "Starting Ollama..."' \
|
| 70 |
-
'ollama serve &' \
|
| 71 |
-
'sleep 10' \
|
| 72 |
-
'echo "Pulling model..."' \
|
| 73 |
-
'ollama pull llama3.2:3b' \
|
| 74 |
-
'echo "Starting Flask..."' \
|
| 75 |
-
'python3 /app/app.py &' \
|
| 76 |
-
'echo "Ready!"' \
|
| 77 |
-
'wait' \
|
| 78 |
-
> /entrypoint.sh && chmod +x /entrypoint.sh
|
| 79 |
|
| 80 |
EXPOSE 7860 11434
|
| 81 |
|
|
|
|
| 2 |
|
| 3 |
WORKDIR /app
|
| 4 |
|
|
|
|
| 5 |
RUN apt-get update && apt-get install -y curl python3 python3-pip && \
|
| 6 |
rm -rf /var/lib/apt/lists/*
|
| 7 |
|
|
|
|
| 8 |
RUN pip3 install flask requests --break-system-packages
|
| 9 |
|
| 10 |
+
COPY app.py /app/app.py
|
| 11 |
+
COPY entrypoint.sh /entrypoint.sh
|
| 12 |
+
RUN chmod +x /entrypoint.sh
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
EXPOSE 7860 11434
|
| 15 |
|
app.py
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from flask import Flask, request, jsonify, Response
|
| 2 |
+
import requests
|
| 3 |
+
|
| 4 |
+
app = Flask(__name__)
|
| 5 |
+
OLLAMA_URL = "http://localhost:11434"
|
| 6 |
+
|
| 7 |
+
@app.route('/')
|
| 8 |
+
def home():
|
| 9 |
+
return '''<!DOCTYPE html>
|
| 10 |
+
<html><head><title>Ollama API</title></head>
|
| 11 |
+
<body style="font-family:Arial;padding:50px;text-align:center;">
|
| 12 |
+
<h1>🦙 Ollama API Running!</h1>
|
| 13 |
+
<p>Model: llama3.2:3b</p>
|
| 14 |
+
<h3>Test with:</h3>
|
| 15 |
+
<pre style="background:#f4f4f4;padding:20px;text-align:left;max-width:700px;margin:20px auto;">
|
| 16 |
+
curl -X POST https://my-ollama.hf.space/api/generate \\
|
| 17 |
+
-H "Content-Type: application/json" \\
|
| 18 |
+
-d '{"model":"llama3.2:3b","prompt":"Hello!","stream":false}'
|
| 19 |
+
</pre>
|
| 20 |
+
</body></html>'''
|
| 21 |
+
|
| 22 |
+
@app.route('/api/generate', methods=['POST'])
|
| 23 |
+
def generate():
|
| 24 |
+
print("Received request to /api/generate")
|
| 25 |
+
try:
|
| 26 |
+
data = request.get_json()
|
| 27 |
+
print(f"Request data: {data}")
|
| 28 |
+
|
| 29 |
+
resp = requests.post(
|
| 30 |
+
f"{OLLAMA_URL}/api/generate",
|
| 31 |
+
json=data,
|
| 32 |
+
stream=data.get('stream', False),
|
| 33 |
+
timeout=120
|
| 34 |
+
)
|
| 35 |
+
|
| 36 |
+
if data.get('stream', False):
|
| 37 |
+
def gen():
|
| 38 |
+
for line in resp.iter_lines():
|
| 39 |
+
if line:
|
| 40 |
+
yield line + b'\n'
|
| 41 |
+
return Response(gen(), content_type='application/x-ndjson')
|
| 42 |
+
else:
|
| 43 |
+
return jsonify(resp.json())
|
| 44 |
+
except Exception as e:
|
| 45 |
+
print(f"Error: {e}")
|
| 46 |
+
return jsonify({"error": str(e)}), 500
|
| 47 |
+
|
| 48 |
+
@app.route('/api/chat', methods=['POST'])
|
| 49 |
+
def chat():
|
| 50 |
+
print("Received request to /api/chat")
|
| 51 |
+
try:
|
| 52 |
+
data = request.get_json()
|
| 53 |
+
resp = requests.post(
|
| 54 |
+
f"{OLLAMA_URL}/api/chat",
|
| 55 |
+
json=data,
|
| 56 |
+
stream=data.get('stream', False),
|
| 57 |
+
timeout=120
|
| 58 |
+
)
|
| 59 |
+
|
| 60 |
+
if data.get('stream', False):
|
| 61 |
+
def gen():
|
| 62 |
+
for line in resp.iter_lines():
|
| 63 |
+
if line:
|
| 64 |
+
yield line + b'\n'
|
| 65 |
+
return Response(gen(), content_type='application/x-ndjson')
|
| 66 |
+
else:
|
| 67 |
+
return jsonify(resp.json())
|
| 68 |
+
except Exception as e:
|
| 69 |
+
print(f"Error: {e}")
|
| 70 |
+
return jsonify({"error": str(e)}), 500
|
| 71 |
+
|
| 72 |
+
if __name__ == '__main__':
|
| 73 |
+
print("Starting Flask on port 7860...")
|
| 74 |
+
app.run(host='0.0.0.0', port=7860, debug=False)
|
entrypoint.sh
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/bin/bash
|
| 2 |
+
set -e
|
| 3 |
+
|
| 4 |
+
echo "Starting Ollama..."
|
| 5 |
+
ollama serve &
|
| 6 |
+
OLLAMA_PID=$!
|
| 7 |
+
|
| 8 |
+
echo "Waiting for Ollama to be ready..."
|
| 9 |
+
sleep 10
|
| 10 |
+
|
| 11 |
+
echo "Pulling model..."
|
| 12 |
+
ollama pull llama3.2:3b
|
| 13 |
+
|
| 14 |
+
echo "Starting Flask..."
|
| 15 |
+
python3 /app/app.py &
|
| 16 |
+
FLASK_PID=$!
|
| 17 |
+
|
| 18 |
+
echo "Ready!"
|
| 19 |
+
|
| 20 |
+
wait $OLLAMA_PID $FLASK_PID
|