Juslin Jay commited on
Commit ·
f57564e
1
Parent(s): f58f4f2
Update summarizer.py
Browse files- app/summarizer.py +23 -22
app/summarizer.py
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
from fastapi import FastAPI
|
| 2 |
from pydantic import BaseModel
|
| 3 |
-
from transformers import
|
| 4 |
|
| 5 |
app = FastAPI()
|
| 6 |
|
|
@@ -11,43 +11,44 @@ class TranslationRequest(BaseModel):
|
|
| 11 |
min_length: int = 20
|
| 12 |
|
| 13 |
# Download and cache the model during initialization
|
|
|
|
| 14 |
try:
|
|
|
|
| 15 |
cache_dir = "./model_cache"
|
| 16 |
-
model_name = "
|
| 17 |
-
|
| 18 |
print(f"Loading tokenizer from {model_name}...")
|
| 19 |
-
tokenizer =
|
| 20 |
-
|
| 21 |
print(f"Loading model from {model_name}...")
|
| 22 |
-
model =
|
| 23 |
-
|
| 24 |
-
print("
|
| 25 |
-
|
| 26 |
except Exception as e:
|
| 27 |
print(f"Error loading model: {str(e)}")
|
| 28 |
raise
|
| 29 |
|
| 30 |
@app.post("/summarize")
|
| 31 |
-
async def
|
| 32 |
-
#
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
summary_ids = model.generate(
|
| 38 |
inputs["input_ids"],
|
| 39 |
-
max_length=request.max_length,
|
| 40 |
min_length=request.min_length,
|
| 41 |
num_beams=4,
|
| 42 |
length_penalty=2.0,
|
| 43 |
early_stopping=True
|
| 44 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
return {"summary": summary}
|
| 49 |
-
|
| 50 |
@app.get("/health")
|
| 51 |
async def health_check():
|
| 52 |
-
return {"status": "healthy", "model": "
|
| 53 |
-
# To run the FastAPI app, use the command:
|
|
|
|
| 1 |
from fastapi import FastAPI
|
| 2 |
from pydantic import BaseModel
|
| 3 |
+
from transformers import BartTokenizer, BartForConditionalGeneration
|
| 4 |
|
| 5 |
app = FastAPI()
|
| 6 |
|
|
|
|
| 11 |
min_length: int = 20
|
| 12 |
|
| 13 |
# Download and cache the model during initialization
|
| 14 |
+
# This happens only once when the app starts
|
| 15 |
try:
|
| 16 |
+
# Explicitly download to a specific directory with proper error handling
|
| 17 |
cache_dir = "./model_cache"
|
| 18 |
+
model_name = "facebook/bart-large-cnn"
|
| 19 |
+
|
| 20 |
print(f"Loading tokenizer from {model_name}...")
|
| 21 |
+
tokenizer = BartTokenizer.from_pretrained(model_name, from_tf=True, cache_dir=cache_dir, local_files_only=False)
|
| 22 |
+
|
| 23 |
print(f"Loading model from {model_name}...")
|
| 24 |
+
model = BartForConditionalGeneration.from_pretrained(model_name, cache_dir=cache_dir, local_files_only=False)
|
| 25 |
+
|
| 26 |
+
print("Model and tokenizer loaded successfully!")
|
|
|
|
| 27 |
except Exception as e:
|
| 28 |
print(f"Error loading model: {str(e)}")
|
| 29 |
raise
|
| 30 |
|
| 31 |
@app.post("/summarize")
|
| 32 |
+
async def translate_text(request: TranslationRequest):
|
| 33 |
+
# Process the input text
|
| 34 |
+
inputs = tokenizer(request.text, return_tensors="pt", max_length=1024, truncation=True)
|
| 35 |
+
|
| 36 |
+
# Generate summary
|
|
|
|
| 37 |
summary_ids = model.generate(
|
| 38 |
inputs["input_ids"],
|
| 39 |
+
max_length=request.max_length,
|
| 40 |
min_length=request.min_length,
|
| 41 |
num_beams=4,
|
| 42 |
length_penalty=2.0,
|
| 43 |
early_stopping=True
|
| 44 |
)
|
| 45 |
+
|
| 46 |
+
# Decode the generated summary
|
| 47 |
+
translation = tokenizer.decode(summary_ids[0], skip_special_tokens=True)
|
| 48 |
+
|
| 49 |
+
return {"summary": translation}
|
| 50 |
|
| 51 |
+
# Basic health check endpoint
|
|
|
|
|
|
|
|
|
|
| 52 |
@app.get("/health")
|
| 53 |
async def health_check():
|
| 54 |
+
return {"status": "healthy", "model": "facebook/bart-large-cnn"}
|
|
|