ABIcode23 commited on
Commit
01e4cdf
1 Parent(s): e7a52aa

Upload 3 files

Browse files
Files changed (3) hide show
  1. Dockerfile +16 -19
  2. app.py +11 -65
  3. requirements.txt +8 -8
Dockerfile CHANGED
@@ -1,33 +1,30 @@
1
- FROM python:3.10-slim
2
-
3
- # Install system dependencies
4
- RUN apt-get update && apt-get install -y --no-install-recommends \
5
- build-essential \
6
- git \
7
- && rm -rf /var/lib/apt/lists/*
8
 
 
9
  WORKDIR /code
10
 
11
- # Copy requirements file
12
  COPY ./requirements.txt /code/requirements.txt
13
 
14
- # Upgrade pip and install requirements
15
- RUN pip install --no-cache-dir --upgrade pip && \
16
- pip install --no-cache-dir -r /code/requirements.txt
 
 
17
 
18
- # Create and use non-root user
19
- RUN useradd -m user
20
  USER user
21
 
22
- # Set environment variables
23
  ENV HOME=/home/user \
24
- PATH=/home/user/.local/bin:$PATH \
25
- PYTHONUNBUFFERED=1
26
 
 
27
  WORKDIR $HOME/app
28
 
29
- # Copy application code
30
  COPY --chown=user . $HOME/app
31
 
32
- # Run the application
33
- CMD ["python", "-m", "uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
 
1
+ ## Use the official Python 3.9 image
2
+ FROM python:3.9
 
 
 
 
 
3
 
4
+ ## Set the working directory to /code
5
  WORKDIR /code
6
 
7
+ ## Copy the requirements.txt file into the container at /code
8
  COPY ./requirements.txt /code/requirements.txt
9
 
10
+ ## Install the requirements from requirements.txt
11
+ RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
12
+
13
+ ## Set up a new user named "user"
14
+ RUN useradd user
15
 
16
+ ## Switch to the "user" user
 
17
  USER user
18
 
19
+ ## Set home to the user's home directory
20
  ENV HOME=/home/user \
21
+ PATH=/home/user/.local/bin:$PATH
 
22
 
23
+ ## Set the working directory to the user's home directory
24
  WORKDIR $HOME/app
25
 
26
+ ## Copy the current directory contents into the container at $HOME/app and set the owner to "user"
27
  COPY --chown=user . $HOME/app
28
 
29
+ ## Start the FASTAPI app on port 7860
30
+ CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
app.py CHANGED
@@ -1,74 +1,20 @@
1
- import os
2
- import logging
3
- from fastapi import FastAPI, HTTPException
4
- from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
5
- from peft import PeftModel, PeftConfig
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
- # Get Hugging Face token from environment variable
25
- hf_token = os.environ.get("HUGGINGFACE_TOKEN")
26
-
27
- logger.info("Loading PEFT configuration...")
28
- config = PeftConfig.from_pretrained("frankmorales2020/Mistral-7B-text-to-sql-flash-attention-2-dataeval")
29
-
30
- # Debugging: Print the configuration
31
- logger.info(f"Configuration: {config}")
32
-
33
- logger.info("Loading base model...")
34
- base_model = AutoModelForCausalLM.from_pretrained(
35
- "mistralai/Mistral-7B-Instruct-v0.3",
36
- use_auth_token=hf_token
37
- )
38
-
39
- logger.info("Loading PEFT model...")
40
- model = PeftModel.from_pretrained(base_model, "frankmorales2020/Mistral-7B-text-to-sql-flash-attention-2-dataeval")
41
-
42
- logger.info("Loading tokenizer...")
43
- tokenizer = AutoTokenizer.from_pretrained(
44
- "mistralai/Mistral-7B-Instruct-v0.3",
45
- use_auth_token=hf_token
46
- )
47
-
48
- logger.info("Creating pipeline...")
49
- pipe = pipeline("text2text-generation", model=model, tokenizer=tokenizer)
50
-
51
- logger.info("Model, tokenizer, and pipeline loaded successfully.")
52
- except Exception as e:
53
- logger.error(f"Error loading model or creating pipeline: {e}")
54
- raise
55
 
56
  @app.get("/")
57
  def home():
58
  return {"message": "Hello World"}
59
 
 
60
  @app.get("/generate")
61
- async def generate(text: str):
62
- if not pipe:
63
- raise HTTPException(status_code=503, detail="Model not loaded")
64
-
65
- try:
66
- output = pipe(text, max_length=100, num_return_sequences=1)
67
- return {"output": output[0]['generated_text']}
68
- except Exception as e:
69
- logger.error(f"Error during text generation: {e}")
70
- raise HTTPException(status_code=500, detail=f"Error during text generation: {str(e)}")
71
-
72
- if __name__ == "__main__":
73
- import uvicorn
74
- uvicorn.run(app, host="0.0.0.0", port=7860)
 
1
+ from fastapi import FastAPI
2
+ from transformers import pipeline
 
 
 
3
 
4
+ # Create a new FASTAPI app instance
 
 
 
 
5
  app = FastAPI()
6
 
7
+ # Initialize the text generation pipeline
8
+ pipe = pipeline("text2text-generation", model="juierror/text-to-sql-with-table-schema")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
 
10
  @app.get("/")
11
  def home():
12
  return {"message": "Hello World"}
13
 
14
+ # Define a function to handle the GET request at '/generate'
15
  @app.get("/generate")
16
+ def generate(text: str):
17
+ # Use the pipeline to generate text from the given input text
18
+ output = pipe(text)
19
+ # Return the generated text in JSON response
20
+ return {"output": output[0]['generated_text']}
 
 
 
 
 
 
 
 
 
requirements.txt CHANGED
@@ -1,9 +1,9 @@
1
- fastapi==0.103.0
 
2
  uvicorn[standard]==0.17.*
3
- torch>=1.13.0
4
- transformers>=4.34.0,<4.35.0
5
- numpy<2
6
- peft>=0.6.0,<0.7.0
7
- accelerate>=0.24.1,<0.25.0
8
- huggingface_hub>=0.16.4,<0.18.0
9
- tokenizers>=0.14.0,<0.15.0
 
1
+
2
+ requests==2.27.*
3
  uvicorn[standard]==0.17.*
4
+ sentencepiece==0.1.*
5
+ torch==1.11.*
6
+
7
+
8
+ fastapi==0.74.*
9
+ transformers==4.*