thangquang09 commited on
Commit
de800a4
·
1 Parent(s): 78f1859

update Dockerfile

Browse files
Files changed (2) hide show
  1. app.py +16 -1
  2. start.sh +28 -7
app.py CHANGED
@@ -12,7 +12,7 @@ class Generate(BaseModel):
12
  duration: float
13
 
14
  chat_history = []
15
- model = Ollama(model="phi2")
16
 
17
  def generate_text(model: Ollama, prompt: str) -> {}:
18
  if prompt == "":
@@ -58,6 +58,21 @@ def generate_text(model: Ollama, prompt: str) -> {}:
58
  async def root():
59
  return {"message": "Hello World"}
60
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61
  @app.post("/api/generate", summary="Generate text from prompt", tags=["Generate"], response_model=Generate)
62
  def inference(input_prompt: str):
63
  return generate_text(model, input_prompt)
 
12
  duration: float
13
 
14
  chat_history = []
15
+ model = Ollama(model="phi2", base_url="http://localhost:11434")
16
 
17
  def generate_text(model: Ollama, prompt: str) -> {}:
18
  if prompt == "":
 
58
  async def root():
59
  return {"message": "Hello World"}
60
 
61
+ @app.get("/health")
62
+ async def health_check():
63
+ try:
64
+ import requests
65
+ response = requests.get("http://localhost:11434/api/version")
66
+ ollama_status = "OK" if response.status_code == 200 else "Not available"
67
+ except:
68
+ ollama_status = "Error"
69
+
70
+ return {
71
+ "status": "healthy",
72
+ "ollama_status": ollama_status,
73
+ "models_loaded": model is not None
74
+ }
75
+
76
  @app.post("/api/generate", summary="Generate text from prompt", tags=["Generate"], response_model=Generate)
77
  def inference(input_prompt: str):
78
  return generate_text(model, input_prompt)
start.sh CHANGED
@@ -1,17 +1,38 @@
1
  #!/bin/bash
2
 
3
- # Start Ollama in the background with proper home directory
4
  export HOME=/root
5
- ollama serve &
6
 
7
- # Wait for Ollama to start
8
- echo "Waiting for Ollama to start..."
9
- sleep 10
 
 
 
 
 
 
 
 
 
10
 
11
- # Pull models if needed
 
 
 
 
 
 
12
  echo "Running pull_models.sh"
13
  /app/pull_models.sh
14
 
15
- # Start FastAPI app
 
 
 
 
 
 
 
16
  echo "Starting FastAPI application..."
17
  uvicorn app:app --host 0.0.0.0 --port 7860
 
1
  #!/bin/bash
2
 
3
+ # Thiết lập home directory
4
  export HOME=/root
 
5
 
6
+ # Kiểm tra xem Ollama đã chạy chưa
7
+ echo "Checking if Ollama is already running..."
8
+ if pgrep -x "ollama" > /dev/null; then
9
+ echo "Ollama is already running."
10
+ else
11
+ echo "Starting Ollama server..."
12
+ ollama serve &
13
+
14
+ # Đợi Ollama khởi động
15
+ echo "Waiting for Ollama to start..."
16
+ sleep 15
17
+ fi
18
 
19
+ # Kiểm tra lại xem Ollama đã chạy chưa
20
+ if ! pgrep -x "ollama" > /dev/null; then
21
+ echo "ERROR: Failed to start Ollama!"
22
+ exit 1
23
+ fi
24
+
25
+ # Tải mô hình nếu cần
26
  echo "Running pull_models.sh"
27
  /app/pull_models.sh
28
 
29
+ # Kiểm tra kết nối đến Ollama server
30
+ echo "Testing connection to Ollama server..."
31
+ curl -s http://localhost:11434/api/version || {
32
+ echo "ERROR: Cannot connect to Ollama server!"
33
+ exit 1
34
+ }
35
+
36
+ # Bắt đầu ứng dụng FastAPI
37
  echo "Starting FastAPI application..."
38
  uvicorn app:app --host 0.0.0.0 --port 7860