barathm2001 commited on
Commit
f5e4c93
·
verified ·
1 Parent(s): 57dfefc

Upload 3 files

Browse files
Files changed (3) hide show
  1. Dockerfile +7 -9
  2. app.py +48 -32
  3. requirements.txt +2 -2
Dockerfile CHANGED
@@ -1,26 +1,24 @@
1
- FROM python:3.10
2
 
3
  WORKDIR /code
4
 
5
  COPY ./requirements.txt /code/requirements.txt
6
 
7
- # Upgrade pip before installing requirements
8
- RUN pip install --no-cache-dir --upgrade pip
9
 
10
- # Install dependencies
11
- RUN pip install --no-cache-dir -r /code/requirements.txt
12
 
13
- # Create and use non-root user
14
  RUN useradd -m user
15
  USER user
16
 
17
- # Set environment variables
18
  ENV HOME=/home/user \
19
- PATH=/home/user/.local/bin:${PATH}
 
20
 
21
  WORKDIR $HOME/app
22
 
 
23
  COPY --chown=user . $HOME/app
24
 
25
- # Run the application
26
  CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
 
1
+ FROM python:3.10 as builder
2
 
3
  WORKDIR /code
4
 
5
  COPY ./requirements.txt /code/requirements.txt
6
 
7
+ RUN pip install --no-cache-dir --upgrade pip && \
8
+ pip install --no-cache-dir -r /code/requirements.txt
9
 
10
+ FROM python:3.10-slim
 
11
 
 
12
  RUN useradd -m user
13
  USER user
14
 
 
15
  ENV HOME=/home/user \
16
+ PATH=/home/user/.local/bin:${PATH} \
17
+ PYTHONUNBUFFERED=1
18
 
19
  WORKDIR $HOME/app
20
 
21
+ COPY --from=builder /usr/local/lib/python3.10/site-packages /usr/local/lib/python3.10/site-packages
22
  COPY --chown=user . $HOME/app
23
 
 
24
  CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
app.py CHANGED
@@ -1,46 +1,62 @@
1
- from fastapi import FastAPI
2
- from transformers import AutoModelForCausalLM
3
-
4
- # Wrap problematic imports in try-except blocks
5
- try:
6
- from peft import PeftModel, PeftConfig
7
- except ImportError as e:
8
- print(f"Error importing from peft: {e}")
9
- raise
10
-
11
- try:
12
- from mistral_common.tokens.tokenizers.mistral import MistralTokenizer
13
- except ImportError as e:
14
- print(f"Error importing from mistral_common: {e}")
15
- raise
16
 
17
  # Initialize FastAPI app
18
  app = FastAPI()
19
 
20
- # Load PEFT model configuration and base model
21
- try:
22
- config = PeftConfig.from_pretrained("frankmorales2020/Mistral-7B-text-to-sql-flash-attention-2-dataeval")
23
- base_model = AutoModelForCausalLM.from_pretrained("mistralai/Mistral-7B-Instruct-v0.3")
24
- model = PeftModel.from_pretrained(base_model, "frankmorales2020/Mistral-7B-text-to-sql-flash-attention-2-dataeval")
25
-
26
- # Load recommended tokenizer
27
- tokenizer = MistralTokenizer.from_pretrained("mistralai/Mistral-7B-Instruct-v0.3")
28
 
29
- # Create the pipeline
30
- from transformers import pipeline
31
- pipe = pipeline("text2text-generation", model=model, tokenizer=tokenizer)
32
- except Exception as e:
33
- print(f"Error loading model or creating pipeline: {e}")
34
- raise
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35
 
36
  @app.get("/")
37
  def home():
38
  return {"message": "Hello World"}
39
 
40
  @app.get("/generate")
41
- def generate(text: str):
 
 
 
42
  try:
43
- output = pipe(text)
44
  return {"output": output[0]['generated_text']}
45
  except Exception as e:
46
- return {"error": str(e)}
 
 
 
 
 
 
1
+ import logging
2
+ from fastapi import FastAPI, HTTPException
3
+ from transformers import AutoModelForCausalLM, pipeline
4
+ from peft import PeftModel, PeftConfig
5
+ from mistral_common.tokens.tokenizers.mistral import MistralTokenizer
6
+
7
+ # Set up logging
8
+ logging.basicConfig(level=logging.INFO)
9
+ logger = logging.getLogger(__name__)
 
 
 
 
 
 
10
 
11
  # Initialize FastAPI app
12
  app = FastAPI()
13
 
14
+ # Global variables for model, tokenizer, and pipeline
15
+ model = None
16
+ tokenizer = None
17
+ pipe = None
 
 
 
 
18
 
19
+ @app.on_event("startup")
20
+ async def load_model():
21
+ global model, tokenizer, pipe
22
+
23
+ try:
24
+ logger.info("Loading PEFT configuration...")
25
+ config = PeftConfig.from_pretrained("frankmorales2020/Mistral-7B-text-to-sql-flash-attention-2-dataeval")
26
+
27
+ logger.info("Loading base model...")
28
+ base_model = AutoModelForCausalLM.from_pretrained("mistralai/Mistral-7B-Instruct-v0.3")
29
+
30
+ logger.info("Loading PEFT model...")
31
+ model = PeftModel.from_pretrained(base_model, "frankmorales2020/Mistral-7B-text-to-sql-flash-attention-2-dataeval")
32
+
33
+ logger.info("Loading tokenizer...")
34
+ tokenizer = MistralTokenizer.from_pretrained("mistralai/Mistral-7B-Instruct-v0.3")
35
+
36
+ logger.info("Creating pipeline...")
37
+ pipe = pipeline("text2text-generation", model=model, tokenizer=tokenizer)
38
+
39
+ logger.info("Model, tokenizer, and pipeline loaded successfully.")
40
+ except Exception as e:
41
+ logger.error(f"Error loading model or creating pipeline: {e}")
42
+ raise
43
 
44
  @app.get("/")
45
  def home():
46
  return {"message": "Hello World"}
47
 
48
  @app.get("/generate")
49
+ async def generate(text: str):
50
+ if not pipe:
51
+ raise HTTPException(status_code=503, detail="Model not loaded")
52
+
53
  try:
54
+ output = pipe(text, max_length=100, num_return_sequences=1)
55
  return {"output": output[0]['generated_text']}
56
  except Exception as e:
57
+ logger.error(f"Error during text generation: {e}")
58
+ raise HTTPException(status_code=500, detail=f"Error during text generation: {str(e)}")
59
+
60
+ if __name__ == "__main__":
61
+ import uvicorn
62
+ uvicorn.run(app, host="0.0.0.0", port=7860)
requirements.txt CHANGED
@@ -2,9 +2,9 @@ fastapi==0.103.0
2
  requests==2.27.*
3
  uvicorn[standard]==0.17.*
4
  torch>=1.13.0
5
- transformers>=4.34.0,<4.35.0
6
  numpy<2
7
- peft>=0.6.0,<0.7.0
8
  accelerate>=0.24.1,<0.25.0
9
  huggingface_hub>=0.16.4,<0.18.0
10
  tokenizers>=0.14.0,<0.15.0
 
2
  requests==2.27.*
3
  uvicorn[standard]==0.17.*
4
  torch>=1.13.0
5
+ transformers>=4.34.0,<5.0.0
6
  numpy<2
7
+ peft>=0.7.0
8
  accelerate>=0.24.1,<0.25.0
9
  huggingface_hub>=0.16.4,<0.18.0
10
  tokenizers>=0.14.0,<0.15.0