chenhaodev commited on
Commit
1bafebd
·
1 Parent(s): ca430b9

update dockerfile for hf

Browse files
Files changed (2) hide show
  1. Dockerfile +36 -4
  2. app.py +41 -24
Dockerfile CHANGED
@@ -27,14 +27,46 @@ RUN pip3 install --no-cache-dir -r requirements.txt
27
  # Copy application code
28
  COPY . .
29
 
30
- # Create startup script
31
  RUN echo '#!/bin/bash\n\
 
 
 
 
 
 
32
  # Start Ollama server\n\
33
  ollama serve & \n\
34
- sleep 5\n\
35
  \n\
36
- # Pull the model if not exists\n\
37
- ollama pull deepseek-r1:1.5b\n\
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38
  \n\
39
  # Start the Gradio app\n\
40
  exec python3 -u app.py\n\
 
27
  # Copy application code
28
  COPY . .
29
 
30
+ # Create startup script with health checks and retries
31
  RUN echo '#!/bin/bash\n\
32
+ \n\
33
+ # Function to check if Ollama is responsive\n\
34
+ check_ollama() {\n\
35
+ curl -s http://localhost:11434/api/version &>/dev/null\n\
36
+ }\n\
37
+ \n\
38
  # Start Ollama server\n\
39
  ollama serve & \n\
 
40
  \n\
41
+ # Wait for Ollama to be responsive (up to 60 seconds)\n\
42
+ count=0\n\
43
+ while ! check_ollama && [ $count -lt 60 ]; do\n\
44
+ echo "Waiting for Ollama server to start..."\n\
45
+ sleep 1\n\
46
+ count=$((count + 1))\n\
47
+ done\n\
48
+ \n\
49
+ if ! check_ollama; then\n\
50
+ echo "Failed to start Ollama server"\n\
51
+ exit 1\n\
52
+ fi\n\
53
+ \n\
54
+ # Pull the model with retry logic\n\
55
+ max_retries=3\n\
56
+ retry_count=0\n\
57
+ while [ $retry_count -lt $max_retries ]; do\n\
58
+ if ollama pull deepseek-r1:1.5b; then\n\
59
+ break\n\
60
+ fi\n\
61
+ echo "Failed to pull model, retrying..."\n\
62
+ retry_count=$((retry_count + 1))\n\
63
+ sleep 5\n\
64
+ done\n\
65
+ \n\
66
+ if [ $retry_count -eq $max_retries ]; then\n\
67
+ echo "Failed to pull model after $max_retries attempts"\n\
68
+ exit 1\n\
69
+ fi\n\
70
  \n\
71
  # Start the Gradio app\n\
72
  exec python3 -u app.py\n\
app.py CHANGED
@@ -6,6 +6,8 @@ from typing import List, Optional, Tuple, Dict
6
  from ollama import Client
7
  import re
8
  import os
 
 
9
 
10
  # Set up logging configuration
11
  logging.basicConfig(
@@ -18,6 +20,30 @@ logging.basicConfig(
18
  )
19
  logger = logging.getLogger(__name__)
20
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
  def calculate_wer_metrics(
22
  hypothesis: str,
23
  reference: str,
@@ -97,19 +123,11 @@ def calculate_wer_metrics(
97
 
98
  return measures
99
 
100
- # Initialize Ollama client
101
- client = Client(host='http://localhost:11434')
102
-
103
  def extract_medical_terms(text: str) -> List[str]:
104
- """
105
- Extract medical terms from text using Qwen model via Ollama.
106
-
107
- Args:
108
- text (str): Input text
109
-
110
- Returns:
111
- List[str]: List of extracted medical terms
112
- """
113
  prompt = f"""Extract all medical terms from the following text.
114
  Return only the medical terms as a comma-separated list.
115
  Text: {text}"""
@@ -121,19 +139,15 @@ def extract_medical_terms(text: str) -> List[str]:
121
  stream=False
122
  )
123
 
124
- # Get the response text
125
  response_text = response['response']
126
 
127
  # Remove the thinking process
128
  if '<think>' in response_text and '</think>' in response_text:
129
- # Extract everything after </think>
130
  medical_terms_text = response_text.split('</think>')[-1].strip()
131
  else:
132
  medical_terms_text = response_text
133
 
134
- # Parse the comma-separated response
135
  medical_terms = [term.strip() for term in medical_terms_text.split(',')]
136
- # Remove empty terms and clean up
137
  return [term for term in medical_terms if term and not term.startswith('<') and not term.endswith('>')]
138
 
139
  except Exception as e:
@@ -323,11 +337,14 @@ def create_interface() -> gr.Blocks:
323
 
324
  if __name__ == "__main__":
325
  logger.info("Application started")
326
- app = create_interface()
327
- # Explicitly configure Gradio to be accessible from outside the container
328
- app.launch(
329
- server_name="0.0.0.0", # Bind to all interfaces
330
- server_port=7860,
331
- share=False, # Don't create a public URL
332
- debug=True # Enable debug mode for more information
333
- )
 
 
 
 
6
  from ollama import Client
7
  import re
8
  import os
9
+ import time
10
+ import requests
11
 
12
  # Set up logging configuration
13
  logging.basicConfig(
 
20
  )
21
  logger = logging.getLogger(__name__)
22
 
23
+ # Initialize Ollama client with retry logic
24
+ def init_ollama_client(max_retries=5):
25
+ client = None
26
+ for i in range(max_retries):
27
+ try:
28
+ client = Client(host='http://localhost:11434')
29
+ # Test the connection
30
+ response = requests.get('http://localhost:11434/api/version')
31
+ if response.status_code == 200:
32
+ logger.info("Successfully connected to Ollama")
33
+ return client
34
+ except Exception as e:
35
+ logger.warning(f"Attempt {i+1}/{max_retries} to connect to Ollama failed: {str(e)}")
36
+ if i < max_retries - 1:
37
+ time.sleep(2)
38
+ raise Exception("Failed to initialize Ollama client")
39
+
40
+ # Global client initialization
41
+ try:
42
+ client = init_ollama_client()
43
+ except Exception as e:
44
+ logger.error(f"Failed to initialize Ollama: {str(e)}")
45
+ client = None
46
+
47
  def calculate_wer_metrics(
48
  hypothesis: str,
49
  reference: str,
 
123
 
124
  return measures
125
 
 
 
 
126
  def extract_medical_terms(text: str) -> List[str]:
127
+ """Extract medical terms from text using Qwen model via Ollama."""
128
+ if client is None:
129
+ logger.error("Ollama client not initialized")
130
+ return []
 
 
 
 
 
131
  prompt = f"""Extract all medical terms from the following text.
132
  Return only the medical terms as a comma-separated list.
133
  Text: {text}"""
 
139
  stream=False
140
  )
141
 
 
142
  response_text = response['response']
143
 
144
  # Remove the thinking process
145
  if '<think>' in response_text and '</think>' in response_text:
 
146
  medical_terms_text = response_text.split('</think>')[-1].strip()
147
  else:
148
  medical_terms_text = response_text
149
 
 
150
  medical_terms = [term.strip() for term in medical_terms_text.split(',')]
 
151
  return [term for term in medical_terms if term and not term.startswith('<') and not term.endswith('>')]
152
 
153
  except Exception as e:
 
337
 
338
  if __name__ == "__main__":
339
  logger.info("Application started")
340
+ try:
341
+ app = create_interface()
342
+ app.launch(
343
+ server_name="0.0.0.0",
344
+ server_port=7860,
345
+ share=False,
346
+ debug=True
347
+ )
348
+ except Exception as e:
349
+ logger.error(f"Failed to launch application: {str(e)}")
350
+ raise