File size: 871 Bytes
5dbee9b
 
 
 
 
2fcc9a4
5dbee9b
 
 
20dbd9d
5dbee9b
20dbd9d
 
 
 
 
5dbee9b
b0c97d8
 
 
20dbd9d
b0c97d8
20dbd9d
5dbee9b
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
from fastapi import FastAPI
from pydantic import BaseModel
from transformers import pipeline

app = FastAPI()
summarizer = pipeline("summarization", model="facebook/bart-large-cnn")

class SummarizationRequest(BaseModel):
    inputs: str
    post_id: str

class SummarizationResponse(BaseModel):
    post_id: str
    summary: str

@app.post("/summarize", response_model=SummarizationResponse)
async def summarize_text(request: SummarizationRequest):
    input_length = len(request.inputs.split())
    max_length = max(50, int(input_length * 0.5))
    min_length = max(20, int(input_length * 0.2))

    summary = summarizer(request.inputs, max_length=max_length, min_length=min_length, do_sample=False)
    return {"post_id": request.post_id, "summary": summary[0]["summary_text"]}

@app.get("/")
def greet_json():
    return {"message": "BART Summarizer API is running"}