Spaces:
Runtime error
Runtime error
iamspruce
commited on
Commit
·
f02b7c3
1
Parent(s):
8a2a7e9
added cache
Browse files- Dockerfile +24 -1
- app/main.py +51 -21
- app/test_main.py +39 -0
Dockerfile
CHANGED
|
@@ -1,10 +1,33 @@
|
|
|
|
|
| 1 |
FROM python:3.10-slim
|
| 2 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
WORKDIR /app
|
| 4 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
COPY requirements.txt .
|
|
|
|
|
|
|
| 6 |
RUN pip install --no-cache-dir -r requirements.txt
|
| 7 |
|
|
|
|
| 8 |
COPY . .
|
| 9 |
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Use an official lightweight Python base image
|
| 2 |
FROM python:3.10-slim
|
| 3 |
|
| 4 |
+
# Set environment variables
|
| 5 |
+
ENV PYTHONDONTWRITEBYTECODE=1
|
| 6 |
+
ENV PYTHONUNBUFFERED=1
|
| 7 |
+
ENV TRANSFORMERS_CACHE=/data
|
| 8 |
+
|
| 9 |
+
# Set working directory
|
| 10 |
WORKDIR /app
|
| 11 |
|
| 12 |
+
# Install system dependencies
|
| 13 |
+
RUN apt-get update && apt-get install -y \
|
| 14 |
+
git \
|
| 15 |
+
&& rm -rf /var/lib/apt/lists/*
|
| 16 |
+
|
| 17 |
+
# Copy requirements (you must have a requirements.txt)
|
| 18 |
COPY requirements.txt .
|
| 19 |
+
|
| 20 |
+
# Install Python dependencies
|
| 21 |
RUN pip install --no-cache-dir -r requirements.txt
|
| 22 |
|
| 23 |
+
# Copy entire project
|
| 24 |
COPY . .
|
| 25 |
|
| 26 |
+
# Create /data dir for model caching
|
| 27 |
+
RUN mkdir -p /data
|
| 28 |
+
|
| 29 |
+
# Expose port (optional in HF Spaces)
|
| 30 |
+
EXPOSE 7860
|
| 31 |
+
|
| 32 |
+
# Run FastAPI with Uvicorn
|
| 33 |
+
CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "7860"]
|
app/main.py
CHANGED
|
@@ -1,6 +1,11 @@
|
|
| 1 |
from fastapi import FastAPI, Body
|
| 2 |
from pydantic import BaseModel
|
| 3 |
from app import models, prompts
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
app = FastAPI()
|
| 6 |
|
|
@@ -10,36 +15,61 @@ class TextInput(BaseModel):
|
|
| 10 |
tone: str = None
|
| 11 |
target_lang: str = None
|
| 12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
@app.post("/rewrite")
|
| 14 |
def rewrite(input: TextInput):
|
| 15 |
text = input.text
|
| 16 |
mode = input.mode
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
-
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
|
|
|
|
|
|
| 24 |
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
|
|
|
|
|
|
| 28 |
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
|
|
|
|
|
|
| 32 |
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
|
|
|
| 36 |
|
| 37 |
-
|
| 38 |
-
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
|
| 44 |
-
|
| 45 |
-
|
|
|
|
|
|
| 1 |
from fastapi import FastAPI, Body
|
| 2 |
from pydantic import BaseModel
|
| 3 |
from app import models, prompts
|
| 4 |
+
import logging
|
| 5 |
+
|
| 6 |
+
# Setup basic logging
|
| 7 |
+
logging.basicConfig(level=logging.INFO)
|
| 8 |
+
logger = logging.getLogger(__name__)
|
| 9 |
|
| 10 |
app = FastAPI()
|
| 11 |
|
|
|
|
| 15 |
tone: str = None
|
| 16 |
target_lang: str = None
|
| 17 |
|
| 18 |
+
@app.get("/")
|
| 19 |
+
def root():
|
| 20 |
+
return {"message": "Welcome to Build Your Own Grammarly API. Use /rewrite POST endpoint."}
|
| 21 |
+
|
| 22 |
@app.post("/rewrite")
|
| 23 |
def rewrite(input: TextInput):
|
| 24 |
text = input.text
|
| 25 |
mode = input.mode
|
| 26 |
+
logger.info(f"Received request | Mode: {mode} | Text: {text}")
|
| 27 |
+
|
| 28 |
+
try:
|
| 29 |
+
if mode == "grammar":
|
| 30 |
+
result = models.run_grammar_correction(text)
|
| 31 |
+
logger.info(f"Grammar corrected: {result}")
|
| 32 |
+
return {"result": result}
|
| 33 |
|
| 34 |
+
elif mode == "paraphrase":
|
| 35 |
+
prompt = prompts.paraphrase_prompt(text)
|
| 36 |
+
result = models.run_flan_prompt(prompt)
|
| 37 |
+
logger.info(f"Paraphrased: {result}")
|
| 38 |
+
return {"result": result}
|
| 39 |
|
| 40 |
+
elif mode == "clarity":
|
| 41 |
+
prompt = prompts.clarity_prompt(text)
|
| 42 |
+
result = models.run_flan_prompt(prompt)
|
| 43 |
+
logger.info(f"Clarity enhanced: {result}")
|
| 44 |
+
return {"result": result}
|
| 45 |
|
| 46 |
+
elif mode == "fluency":
|
| 47 |
+
prompt = prompts.fluency_prompt(text)
|
| 48 |
+
result = models.run_flan_prompt(prompt)
|
| 49 |
+
logger.info(f"Fluency improved: {result}")
|
| 50 |
+
return {"result": result}
|
| 51 |
|
| 52 |
+
elif mode == "tone" and input.tone:
|
| 53 |
+
prompt = prompts.tone_prompt(text, input.tone)
|
| 54 |
+
result = models.run_flan_prompt(prompt)
|
| 55 |
+
logger.info(f"Tone changed to {input.tone}: {result}")
|
| 56 |
+
return {"result": result}
|
| 57 |
|
| 58 |
+
elif mode == "translate" and input.target_lang:
|
| 59 |
+
result = models.run_translation(text, input.target_lang)
|
| 60 |
+
logger.info(f"Translated to {input.target_lang}: {result}")
|
| 61 |
+
return {"result": result}
|
| 62 |
|
| 63 |
+
elif mode == "pronoun":
|
| 64 |
+
prompt = prompts.pronoun_friendly_prompt(text)
|
| 65 |
+
result = models.run_flan_prompt(prompt)
|
| 66 |
+
logger.info(f"Inclusive pronoun version: {result}")
|
| 67 |
+
return {"result": result}
|
| 68 |
|
| 69 |
+
else:
|
| 70 |
+
logger.error("Invalid request parameters.")
|
| 71 |
+
return {"error": "Invalid request"}
|
| 72 |
|
| 73 |
+
except Exception as e:
|
| 74 |
+
logger.error(f"Error while processing: {str(e)}")
|
| 75 |
+
return {"error": str(e)}
|
app/test_main.py
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi.testclient import TestClient
|
| 2 |
+
from app.main import app
|
| 3 |
+
|
| 4 |
+
client = TestClient(app)
|
| 5 |
+
|
| 6 |
+
def test_grammar():
|
| 7 |
+
response = client.post("/rewrite", json={"text": "She go to school yesterday.", "mode": "grammar"})
|
| 8 |
+
assert response.status_code == 200
|
| 9 |
+
assert "result" in response.json()
|
| 10 |
+
|
| 11 |
+
def test_paraphrase():
|
| 12 |
+
response = client.post("/rewrite", json={"text": "I love programming.", "mode": "paraphrase"})
|
| 13 |
+
assert response.status_code == 200
|
| 14 |
+
assert "result" in response.json()
|
| 15 |
+
|
| 16 |
+
def test_clarity():
|
| 17 |
+
response = client.post("/rewrite", json={"text": "This sentence is a bit confusing.", "mode": "clarity"})
|
| 18 |
+
assert response.status_code == 200
|
| 19 |
+
assert "result" in response.json()
|
| 20 |
+
|
| 21 |
+
def test_fluency():
|
| 22 |
+
response = client.post("/rewrite", json={"text": "He no went there before.", "mode": "fluency"})
|
| 23 |
+
assert response.status_code == 200
|
| 24 |
+
assert "result" in response.json()
|
| 25 |
+
|
| 26 |
+
def test_tone():
|
| 27 |
+
response = client.post("/rewrite", json={"text": "Leave me alone.", "mode": "tone", "tone": "friendly"})
|
| 28 |
+
assert response.status_code == 200
|
| 29 |
+
assert "result" in response.json()
|
| 30 |
+
|
| 31 |
+
def test_translation():
|
| 32 |
+
response = client.post("/rewrite", json={"text": "How are you?", "mode": "translate", "target_lang": "fr"})
|
| 33 |
+
assert response.status_code == 200
|
| 34 |
+
assert "result" in response.json()
|
| 35 |
+
|
| 36 |
+
def test_pronoun():
|
| 37 |
+
response = client.post("/rewrite", json={"text": "Each user must provide his email.", "mode": "pronoun"})
|
| 38 |
+
assert response.status_code == 200
|
| 39 |
+
assert "result" in response.json()
|