pius-code commited on
Commit
2c871b7
·
1 Parent(s): 3ba8f58

enhance summarize endpoint to dynamically adjust summary length based on input word count and return parameters used

Browse files
Files changed (1) hide show
  1. main.py +35 -4
main.py CHANGED
@@ -5,16 +5,47 @@ from transformers import pipeline
5
  app = FastAPI()
6
  summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
7
 
8
-
9
  class TextInput(BaseModel):
10
  text: str
11
 
12
-
13
  @app.get("/")
14
  async def root():
15
  return {"message": "Welcome to the Text Summarization API!"}
16
 
17
  @app.post("/summarize")
18
  async def summarize_text(input: TextInput):
19
- summary = (summarizer(input.text, max_length=130, min_length=30, do_sample=False))
20
- return {"summary": summary[0]['summary_text']}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
  app = FastAPI()
6
  summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
7
 
 
8
  class TextInput(BaseModel):
9
  text: str
10
 
 
11
  @app.get("/")
12
  async def root():
13
  return {"message": "Welcome to the Text Summarization API!"}
14
 
15
  @app.post("/summarize")
16
  async def summarize_text(input: TextInput):
17
+ # Count approximate number of words (could be improved with tokenizer count in the future)
18
+ word_count = len(input.text.split())
19
+
20
+
21
+ if word_count < 50:
22
+ max_length = max(10, word_count // 2)
23
+ min_length = max(3, word_count // 4)
24
+ elif word_count < 200:
25
+ max_length = max(50, word_count // 3)
26
+ min_length = max(15, word_count // 6)
27
+ else:
28
+ max_length = max(100, word_count // 4)
29
+ min_length = max(30, word_count // 8)
30
+
31
+ # Prevent max_length from being too large (BART has token limits)
32
+ max_length = min(max_length, 1024)
33
+
34
+ # Generate summary with dynamic parameters
35
+ summary = summarizer(
36
+ input.text,
37
+ max_length=max_length,
38
+ min_length=min_length,
39
+ do_sample=True,
40
+ temperature=0.7,
41
+ num_beams=4
42
+ )
43
+
44
+ return {
45
+ "summary": summary[0]["summary_text"],
46
+ "parameters_used": {
47
+ "input_word_count": word_count,
48
+ "max_length": max_length,
49
+ "min_length": min_length
50
+ }
51
+ }