zeroai87 commited on
Commit
82f21ae
·
verified ·
1 Parent(s): a131dea

Upload Dockerfile with huggingface_hub

Browse files
Files changed (1) hide show
  1. Dockerfile +145 -0
Dockerfile ADDED
@@ -0,0 +1,145 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Dockerfile ottimizzato per Hugging Face Spaces - Jaksel AI
2
+ # Usa immagine base con Python e CUDA per GPU
3
+ FROM python:3.11-slim
4
+
5
+ # Imposta environment variables
6
+ ENV DEBIAN_FRONTEND=noninteractive
7
+ ENV PYTHONUNBUFFERED=1
8
+ ENV OLLAMA_HOST=0.0.0.0
9
+ ENV PORT=7860
10
+ ENV HF_HOME=/data
11
+ ENV TRANSFORMERS_CACHE=/data/transformers_cache
12
+
13
+ # Installa dipendenze di sistema
14
+ RUN apt-get update && apt-get install -y \
15
+ curl \
16
+ ca-certificates \
17
+ && rm -rf /var/lib/apt/lists/*
18
+
19
+ # Installa Ollama
20
+ RUN curl -fsSL https://ollama.ai/install.sh | sh
21
+
22
+ # Copia l'applicazione
23
+ WORKDIR /app
24
+ COPY . .
25
+
26
+ # Installa dipendenze Python
27
+ RUN pip install --no-cache-dir fastapi uvicorn python-multipart aiohttp
28
+
29
+ # Crea directory per il modello
30
+ RUN mkdir -p /root/.ollama
31
+
32
+ # Scarica Jaksel model ( questo avviene al runtime per evitare problemi con HF cache )
33
+ # Il modello verrà scaricato al primo avvio
34
+
35
+ # Script di startup che gestisce il download del modello e avvia Ollama
36
+ COPY <<EOF /app/start.sh
37
+ #!/bin/bash
38
+ echo "🚀 Starting Jaksel AI on Hugging Face Spaces..."
39
+
40
+ # Assicura che la directory esista
41
+ mkdir -p /root/.ollama
42
+
43
+ # Avvia Ollama in background
44
+ echo "📥 Starting Ollama server..."
45
+ ollama serve --host 0.0.0.0 --port 11434 &
46
+
47
+ # Attendi che Ollama sia avviato
48
+ echo "⏳ Waiting for Ollama to start..."
49
+ sleep 10
50
+
51
+ # Pull Jaksel model ( se non già presente )
52
+ echo "🤖 Pulling Jaksel model..."
53
+ ollama pull zantara-jaksel:latest || echo "Model already exists or download failed, continuing..."
54
+
55
+ # Tenta di nuovo se il primo download fallisce
56
+ if ! ollama list | grep -q "zantara-jaksel"; then
57
+ echo "🔄 Retrying model download..."
58
+ sleep 5
59
+ ollama pull zantara-jaksel:latest
60
+ fi
61
+
62
+ # Crea proxy server per gli endpoint HF
63
+ python <<PYTHON
64
+ import subprocess
65
+ import time
66
+ import requests
67
+ from fastapi import FastAPI, Request
68
+ import uvicorn
69
+ import json
70
+
71
+ app = FastAPI(title="Jaksel AI Proxy")
72
+
73
+ @app.get("/")
74
+ async def root():
75
+ return {"message": "Jaksel AI is running!", "status": "healthy"}
76
+
77
+ @app.get("/health")
78
+ async def health():
79
+ try:
80
+ response = requests.get("http://127.0.0.1:11434/api/tags", timeout=5)
81
+ if response.status_code == 200:
82
+ models = response.json().get("models", [])
83
+ jaksel_found = any("zantara-jaksel" in m.get("name", "") for m in models)
84
+ return {
85
+ "status": "healthy",
86
+ "ollama": "connected",
87
+ "jaksel_loaded": jaksel_found,
88
+ "models": [m.get("name") for m in models]
89
+ }
90
+ else:
91
+ return {"status": "unhealthy", "ollama": "error"}
92
+ except Exception as e:
93
+ return {"status": "unhealthy", "error": str(e)}
94
+
95
+ @app.post("/api/generate")
96
+ @app.post("/api/chat")
97
+ async def proxy_ollama(request: Request):
98
+ """Proxy per richieste a Ollama"""
99
+ try:
100
+ # Ottieni body della richiesta
101
+ body = await request.json()
102
+
103
+ # Forward a Ollama
104
+ ollama_url = "http://127.0.0.1:11434" + request.scope.get("path", "")
105
+
106
+ response = requests.post(
107
+ ollama_url,
108
+ json=body,
109
+ headers={
110
+ "Content-Type": "application/json",
111
+ },
112
+ timeout=120 # Timeout più lungo per HF Spaces
113
+ )
114
+
115
+ return Response(
116
+ content=response.content,
117
+ status_code=response.status_code,
118
+ headers={
119
+ "Content-Type": "application/json",
120
+ }
121
+ )
122
+ except Exception as e:
123
+ return {
124
+ "error": f"Proxy error: {str(e)}",
125
+ "response": "Maaf, Jaksel lagi nggak bisa merespon. Coba lagi ya!"
126
+ }
127
+
128
+ from fastapi.responses import Response
129
+
130
+ print("🌐 Starting proxy server on port 7860...")
131
+ uvicorn.run(app, host="0.0.0.0", port=7860)
132
+ PYTHON
133
+
134
+ # Se il processo termina, aspetta un po' prima di uscire
135
+ sleep 30
136
+ EOF
137
+
138
+ # Rendi eseguibile lo script
139
+ RUN chmod +x /app/start.sh
140
+
141
+ # Esponi le porte
142
+ EXPOSE 7860 11434
143
+
144
+ # Comando di avvio
145
+ CMD ["/app/start.sh"]