Spaces:
Sleeping
Sleeping
barathm111
commited on
Commit
•
517f5d9
1
Parent(s):
580e179
Upload 3 files
Browse files- Dockerfile +30 -0
- app.py +38 -0
- requirements.txt +9 -0
Dockerfile
ADDED
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
## Use the official Python 3.9 image
|
2 |
+
FROM python:3.11
|
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"d
|
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
ADDED
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import FastAPI, HTTPException
|
2 |
+
from pydantic import BaseModel
|
3 |
+
from transformers import pipeline
|
4 |
+
|
5 |
+
app = FastAPI()
|
6 |
+
|
7 |
+
# Initialize the text generation pipeline
|
8 |
+
pipe = pipeline("text-generation", model="defog/llama-3-sqlcoder-8b", pad_token_id=2)
|
9 |
+
|
10 |
+
class QueryRequest(BaseModel):
|
11 |
+
text: str
|
12 |
+
|
13 |
+
@app.get("/")
|
14 |
+
def home():
|
15 |
+
return {"message": "SQL Generation Server is running"}
|
16 |
+
|
17 |
+
|
18 |
+
@app.post("/generate")
|
19 |
+
def generate(request: QueryRequest):
|
20 |
+
try:
|
21 |
+
text = request.text
|
22 |
+
prompt = f"Generate a valid SQL query for the following request. Only return the SQL query, nothing else:\n\n{text}\n\nSQL query:"
|
23 |
+
output = pipe(prompt, max_new_tokens=100)
|
24 |
+
|
25 |
+
generated_text = output[0]['generated_text']
|
26 |
+
sql_query = generated_text.split("SQL query:")[-1].strip()
|
27 |
+
|
28 |
+
# Basic validation
|
29 |
+
if not sql_query.lower().startswith(('select', 'show', 'describe')):
|
30 |
+
raise ValueError("Generated text is not a valid SQL query")
|
31 |
+
|
32 |
+
return {"output": sql_query}
|
33 |
+
except Exception as e:
|
34 |
+
raise HTTPException(status_code=500, detail=str(e))
|
35 |
+
|
36 |
+
if __name__ == "__main__":
|
37 |
+
import uvicorn
|
38 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|
requirements.txt
ADDED
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
requests==2.27.*
|
3 |
+
uvicorn[standard]==0.17.*
|
4 |
+
sentencepiece==0.1.*
|
5 |
+
torch==1.11.*
|
6 |
+
numpy<2.0.0
|
7 |
+
|
8 |
+
fastapi==0.74.*
|
9 |
+
transformers==4.*
|