Run Ollama with Qwen in Docker Space
Browse files- Dockerfile +31 -0
- README.md +15 -6
- app.py +38 -0
- requirements.txt +2 -0
- start.sh +21 -0
Dockerfile
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM ubuntu:22.04
|
| 2 |
+
|
| 3 |
+
ENV DEBIAN_FRONTEND=noninteractive
|
| 4 |
+
|
| 5 |
+
RUN apt-get update && apt-get install -y --no-install-recommends \
|
| 6 |
+
curl \
|
| 7 |
+
ca-certificates \
|
| 8 |
+
python3 \
|
| 9 |
+
python3-pip \
|
| 10 |
+
bash \
|
| 11 |
+
&& rm -rf /var/lib/apt/lists/*
|
| 12 |
+
|
| 13 |
+
# Install Ollama
|
| 14 |
+
RUN curl -fsSL https://ollama.com/install.sh | sh
|
| 15 |
+
|
| 16 |
+
WORKDIR /app
|
| 17 |
+
|
| 18 |
+
COPY requirements.txt /app/requirements.txt
|
| 19 |
+
RUN pip3 install --no-cache-dir -r /app/requirements.txt
|
| 20 |
+
|
| 21 |
+
COPY app.py /app/app.py
|
| 22 |
+
COPY start.sh /app/start.sh
|
| 23 |
+
RUN chmod +x /app/start.sh
|
| 24 |
+
|
| 25 |
+
# Hugging Face Docker Spaces must listen on 7860
|
| 26 |
+
EXPOSE 7860
|
| 27 |
+
|
| 28 |
+
# Change this to a different Qwen model if needed
|
| 29 |
+
ENV OLLAMA_MODEL=qwen2.5:0.5b
|
| 30 |
+
|
| 31 |
+
CMD ["/app/start.sh"]
|
README.md
CHANGED
|
@@ -1,10 +1,19 @@
|
|
| 1 |
---
|
| 2 |
-
title:
|
| 3 |
-
emoji:
|
| 4 |
-
colorFrom:
|
| 5 |
-
colorTo:
|
| 6 |
sdk: docker
|
| 7 |
-
|
| 8 |
---
|
| 9 |
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
---
|
| 2 |
+
title: Ollama Qwen Demo
|
| 3 |
+
emoji: 🤖
|
| 4 |
+
colorFrom: blue
|
| 5 |
+
colorTo: green
|
| 6 |
sdk: docker
|
| 7 |
+
app_port: 7860
|
| 8 |
---
|
| 9 |
|
| 10 |
+
This Space runs a local Ollama model with a Gradio UI.
|
| 11 |
+
|
| 12 |
+
Default model:
|
| 13 |
+
- `qwen2.5:0.5b`
|
| 14 |
+
|
| 15 |
+
You can change model by editing `OLLAMA_MODEL` in `Dockerfile`.
|
| 16 |
+
|
| 17 |
+
Notes:
|
| 18 |
+
- Free CPU Spaces are limited, so smaller Qwen variants are recommended.
|
| 19 |
+
- Larger models may fail or take a long time to pull.
|
app.py
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import requests
|
| 3 |
+
import gradio as gr
|
| 4 |
+
|
| 5 |
+
OLLAMA_URL = os.getenv("OLLAMA_URL", "http://127.0.0.1:11434")
|
| 6 |
+
MODEL = os.getenv("OLLAMA_MODEL", "qwen2.5:0.5b")
|
| 7 |
+
APP_KEY = os.getenv("APP_KEY", "")
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
def chat(prompt: str, key: str):
|
| 11 |
+
if APP_KEY and key != APP_KEY:
|
| 12 |
+
return "Unauthorized"
|
| 13 |
+
|
| 14 |
+
try:
|
| 15 |
+
res = requests.post(
|
| 16 |
+
f"{OLLAMA_URL}/api/generate",
|
| 17 |
+
json={"model": MODEL, "prompt": prompt, "stream": False},
|
| 18 |
+
timeout=120,
|
| 19 |
+
)
|
| 20 |
+
res.raise_for_status()
|
| 21 |
+
return res.json().get("response", "No response from model")
|
| 22 |
+
except Exception as e:
|
| 23 |
+
return f"Error: {e}"
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
with gr.Blocks(title="Ollama Qwen Demo") as demo:
|
| 27 |
+
gr.Markdown("# Ollama Qwen Demo")
|
| 28 |
+
gr.Markdown(f"Using model: `{MODEL}`")
|
| 29 |
+
|
| 30 |
+
prompt = gr.Textbox(label="Prompt", lines=6, placeholder="Ask Qwen anything...")
|
| 31 |
+
key = gr.Textbox(label="Access key (optional)", type="password")
|
| 32 |
+
output = gr.Textbox(label="Response", lines=12)
|
| 33 |
+
|
| 34 |
+
submit = gr.Button("Generate")
|
| 35 |
+
submit.click(chat, inputs=[prompt, key], outputs=output)
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|
requirements.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio==4.44.1
|
| 2 |
+
requests==2.32.3
|
start.sh
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env bash
|
| 2 |
+
set -euo pipefail
|
| 3 |
+
|
| 4 |
+
# Start Ollama in the background
|
| 5 |
+
ollama serve &
|
| 6 |
+
|
| 7 |
+
# Wait for Ollama API to become ready
|
| 8 |
+
for i in {1..60}; do
|
| 9 |
+
if curl -sf http://127.0.0.1:11434/api/tags >/dev/null; then
|
| 10 |
+
break
|
| 11 |
+
fi
|
| 12 |
+
sleep 1
|
| 13 |
+
done
|
| 14 |
+
|
| 15 |
+
# Pull model if missing
|
| 16 |
+
if ! ollama list | awk '{print $1}' | grep -qx "${OLLAMA_MODEL}"; then
|
| 17 |
+
echo "Pulling model ${OLLAMA_MODEL}..."
|
| 18 |
+
ollama pull "${OLLAMA_MODEL}"
|
| 19 |
+
fi
|
| 20 |
+
|
| 21 |
+
exec python3 app.py
|