Spaces:
Runtime error
Runtime error
iamspruce
commited on
Commit
·
a93ed2b
1
Parent(s):
f5d6f13
updated docker file
Browse files- Dockerfile +15 -6
- app/core/security.py +12 -0
- app/main.py +12 -4
- app/routers/paraphrase.py +18 -1
- app/routers/summarize.py +18 -1
- app/routers/translate.py +18 -1
Dockerfile
CHANGED
@@ -1,21 +1,30 @@
|
|
1 |
FROM python:3.10-slim
|
2 |
|
3 |
-
# Set working directory
|
4 |
WORKDIR /app
|
5 |
|
6 |
# Install system dependencies
|
|
|
7 |
RUN apt-get update && apt-get install -y git && rm -rf /var/lib/apt/lists/*
|
8 |
|
9 |
-
# Install Python dependencies
|
|
|
10 |
COPY requirements.txt .
|
11 |
RUN pip install --no-cache-dir -r requirements.txt
|
12 |
|
13 |
-
#
|
|
|
|
|
|
|
|
|
|
|
14 |
ENV HF_HOME=/cache
|
15 |
RUN mkdir -p /cache && chmod -R 777 /cache
|
16 |
|
17 |
-
# Copy
|
|
|
18 |
COPY app ./app
|
19 |
|
20 |
-
#
|
21 |
-
|
|
|
|
1 |
FROM python:3.10-slim
|
2 |
|
3 |
+
# Set working directory inside the container
|
4 |
WORKDIR /app
|
5 |
|
6 |
# Install system dependencies
|
7 |
+
# git is included for any potential future needs or if any dependency requires it for cloning
|
8 |
RUN apt-get update && apt-get install -y git && rm -rf /var/lib/apt/lists/*
|
9 |
|
10 |
+
# Install Python dependencies from requirements.txt
|
11 |
+
# Ensure requirements.txt is copied before installing to leverage Docker cache
|
12 |
COPY requirements.txt .
|
13 |
RUN pip install --no-cache-dir -r requirements.txt
|
14 |
|
15 |
+
# Download the spaCy English language model
|
16 |
+
# This is crucial for resolving the "[E050] Can't find model 'en_core_web_sm'" error
|
17 |
+
RUN python -m spacy download en_core_web_sm
|
18 |
+
|
19 |
+
# Setup Hugging Face cache directory and permissions
|
20 |
+
# This can help manage where Hugging Face models are stored within the container
|
21 |
ENV HF_HOME=/cache
|
22 |
RUN mkdir -p /cache && chmod -R 777 /cache
|
23 |
|
24 |
+
# Copy the entire application code into the container
|
25 |
+
# This copies your 'app' directory, including main.py, routers, models, etc.
|
26 |
COPY app ./app
|
27 |
|
28 |
+
# Command to run the FastAPI application using Uvicorn
|
29 |
+
# Binds the app to all network interfaces (0.0.0.0) on port 7860
|
30 |
+
CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "7860"]
|
app/core/security.py
CHANGED
@@ -1,7 +1,19 @@
|
|
1 |
from fastapi import Header, HTTPException
|
2 |
|
|
|
|
|
3 |
API_KEY = "12345"
|
4 |
|
5 |
def verify_api_key(x_api_key: str = Header(...)):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
if x_api_key != API_KEY:
|
|
|
7 |
raise HTTPException(status_code=401, detail="Unauthorized")
|
|
|
1 |
from fastapi import Header, HTTPException
|
2 |
|
3 |
+
# Define a simple API key for authentication.
|
4 |
+
# In a production environment, this should be a more robust and securely managed key.
|
5 |
API_KEY = "12345"
|
6 |
|
7 |
def verify_api_key(x_api_key: str = Header(...)):
|
8 |
+
"""
|
9 |
+
Dependency function to verify the API key provided in the request headers.
|
10 |
+
|
11 |
+
Args:
|
12 |
+
x_api_key (str): The API key expected in the 'X-API-Key' header.
|
13 |
+
|
14 |
+
Raises:
|
15 |
+
HTTPException: If the provided API key does not match the expected API_KEY.
|
16 |
+
"""
|
17 |
if x_api_key != API_KEY:
|
18 |
+
# Raise an HTTPException with 401 Unauthorized status if the key is invalid.
|
19 |
raise HTTPException(status_code=401, detail="Unauthorized")
|
app/main.py
CHANGED
@@ -1,13 +1,21 @@
|
|
1 |
from fastapi import FastAPI
|
|
|
2 |
from app.routers import analyze, paraphrase, translate, summarize
|
3 |
|
|
|
4 |
app = FastAPI()
|
5 |
|
6 |
@app.get("/")
|
7 |
def root():
|
|
|
|
|
|
|
|
|
8 |
return {"message": "Welcome to Grammafree API"}
|
9 |
|
10 |
-
|
11 |
-
|
12 |
-
app.include_router(
|
13 |
-
app.include_router(
|
|
|
|
|
|
1 |
from fastapi import FastAPI
|
2 |
+
# Import routers for different functionalities
|
3 |
from app.routers import analyze, paraphrase, translate, summarize
|
4 |
|
5 |
+
# Initialize the FastAPI application
|
6 |
app = FastAPI()
|
7 |
|
8 |
@app.get("/")
|
9 |
def root():
|
10 |
+
"""
|
11 |
+
Root endpoint for the API.
|
12 |
+
Returns a welcome message.
|
13 |
+
"""
|
14 |
return {"message": "Welcome to Grammafree API"}
|
15 |
|
16 |
+
# Include the routers for different API functionalities.
|
17 |
+
# This organizes the API into modular components.
|
18 |
+
app.include_router(analyze.router) # For text analysis (grammar, tone, etc.)
|
19 |
+
app.include_router(paraphrase.router) # For paraphrasing text
|
20 |
+
app.include_router(translate.router) # For translating text
|
21 |
+
app.include_router(summarize.router) # For summarizing text
|
app/routers/paraphrase.py
CHANGED
@@ -3,11 +3,28 @@ from pydantic import BaseModel
|
|
3 |
from app import models, prompts
|
4 |
from app.core.security import verify_api_key
|
5 |
|
|
|
6 |
router = APIRouter()
|
7 |
|
8 |
class Input(BaseModel):
|
|
|
|
|
|
|
|
|
9 |
text: str
|
10 |
|
11 |
@router.post("/paraphrase", dependencies=[Depends(verify_api_key)])
|
12 |
def paraphrase(input: Input):
|
13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
from app import models, prompts
|
4 |
from app.core.security import verify_api_key
|
5 |
|
6 |
+
# Create an APIRouter instance. This will handle routes specific to paraphrasing.
|
7 |
router = APIRouter()
|
8 |
|
9 |
class Input(BaseModel):
|
10 |
+
"""
|
11 |
+
Pydantic BaseModel for validating the input request body for the /paraphrase endpoint.
|
12 |
+
It expects a single field: 'text' (string).
|
13 |
+
"""
|
14 |
text: str
|
15 |
|
16 |
@router.post("/paraphrase", dependencies=[Depends(verify_api_key)])
|
17 |
def paraphrase(input: Input):
|
18 |
+
"""
|
19 |
+
Endpoint to paraphrase the given text.
|
20 |
+
|
21 |
+
Args:
|
22 |
+
input (Input): The request body containing the text to be paraphrased.
|
23 |
+
(dependencies=[Depends(verify_api_key)]): Ensures the API key is verified before execution.
|
24 |
+
|
25 |
+
Returns:
|
26 |
+
dict: A dictionary containing the paraphrased result.
|
27 |
+
"""
|
28 |
+
# Call the FLAN-T5 model with the paraphrase prompt to get the result.
|
29 |
+
paraphrased_text = models.run_flan_prompt(prompts.paraphrase_prompt(input.text))
|
30 |
+
return {"result": paraphrased_text}
|
app/routers/summarize.py
CHANGED
@@ -3,11 +3,28 @@ from pydantic import BaseModel
|
|
3 |
from app import models, prompts
|
4 |
from app.core.security import verify_api_key
|
5 |
|
|
|
6 |
router = APIRouter()
|
7 |
|
8 |
class Input(BaseModel):
|
|
|
|
|
|
|
|
|
9 |
text: str
|
10 |
|
11 |
@router.post("/summarize", dependencies=[Depends(verify_api_key)])
|
12 |
def summarize(input: Input):
|
13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
from app import models, prompts
|
4 |
from app.core.security import verify_api_key
|
5 |
|
6 |
+
# Create an APIRouter instance. This will handle routes specific to summarization.
|
7 |
router = APIRouter()
|
8 |
|
9 |
class Input(BaseModel):
|
10 |
+
"""
|
11 |
+
Pydantic BaseModel for validating the input request body for the /summarize endpoint.
|
12 |
+
It expects a single field: 'text' (string).
|
13 |
+
"""
|
14 |
text: str
|
15 |
|
16 |
@router.post("/summarize", dependencies=[Depends(verify_api_key)])
|
17 |
def summarize(input: Input):
|
18 |
+
"""
|
19 |
+
Endpoint to summarize the given text.
|
20 |
+
|
21 |
+
Args:
|
22 |
+
input (Input): The request body containing the text to be summarized.
|
23 |
+
(dependencies=[Depends(verify_api_key)]): Ensures the API key is verified before execution.
|
24 |
+
|
25 |
+
Returns:
|
26 |
+
dict: A dictionary containing the summarized result.
|
27 |
+
"""
|
28 |
+
# Call the FLAN-T5 model with the summarize prompt to get the result.
|
29 |
+
summarized_text = models.run_flan_prompt(prompts.summarize_prompt(input.text))
|
30 |
+
return {"result": summarized_text}
|
app/routers/translate.py
CHANGED
@@ -3,12 +3,29 @@ from pydantic import BaseModel
|
|
3 |
from app import models
|
4 |
from app.core.security import verify_api_key
|
5 |
|
|
|
6 |
router = APIRouter()
|
7 |
|
8 |
class TranslateInput(BaseModel):
|
|
|
|
|
|
|
|
|
9 |
text: str
|
10 |
target_lang: str
|
11 |
|
12 |
@router.post("/translate", dependencies=[Depends(verify_api_key)])
|
13 |
def translate(input: TranslateInput):
|
14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
from app import models
|
4 |
from app.core.security import verify_api_key
|
5 |
|
6 |
+
# Create an APIRouter instance. This will handle routes specific to translation.
|
7 |
router = APIRouter()
|
8 |
|
9 |
class TranslateInput(BaseModel):
|
10 |
+
"""
|
11 |
+
Pydantic BaseModel for validating the input request body for the /translate endpoint.
|
12 |
+
It expects two fields: 'text' (string) and 'target_lang' (string).
|
13 |
+
"""
|
14 |
text: str
|
15 |
target_lang: str
|
16 |
|
17 |
@router.post("/translate", dependencies=[Depends(verify_api_key)])
|
18 |
def translate(input: TranslateInput):
|
19 |
+
"""
|
20 |
+
Endpoint to translate the given text to a target language.
|
21 |
+
|
22 |
+
Args:
|
23 |
+
input (TranslateInput): The request body containing the text and target language.
|
24 |
+
(dependencies=[Depends(verify_api_key)]): Ensures the API key is verified before execution.
|
25 |
+
|
26 |
+
Returns:
|
27 |
+
dict: A dictionary containing the translated result.
|
28 |
+
"""
|
29 |
+
# Call the translation model with the text and target language.
|
30 |
+
translated_text = models.run_translation(input.text, input.target_lang)
|
31 |
+
return {"result": translated_text}
|