Marroco93 commited on
Commit
27153aa
1 Parent(s): c108da3

no message

Browse files
Files changed (1) hide show
  1. main.py +16 -3
main.py CHANGED
@@ -78,15 +78,28 @@ async def generate_text(item: Item):
78
  # Stream response back to the client
79
  return StreamingResponse(generate_stream(item), media_type="application/x-ndjson")
80
 
 
 
 
 
 
 
 
 
 
 
 
 
81
  @app.post("/summarize")
82
  async def summarize_text(request: SummarizeRequest):
83
  try:
84
- # Perform the summarization
85
- summary = summarizer(request.text, max_length=130, min_length=30, do_sample=False)
86
- return JSONResponse(content={"summary": summary[0]['summary_text']})
87
  except Exception as e:
88
  # Handle exceptions that could arise during summarization
89
  raise HTTPException(status_code=500, detail=str(e))
 
90
 
91
  if __name__ == "__main__":
92
  uvicorn.run(app, host="0.0.0.0", port=8000)
 
78
  # Stream response back to the client
79
  return StreamingResponse(generate_stream(item), media_type="application/x-ndjson")
80
 
81
+ def split_text(text, max_size=1000):
82
+ # Splits the text into chunks of approximately `max_size` words
83
+ words = text.split()
84
+ for i in range(0, len(words), max_size):
85
+ yield ' '.join(words[i:i+max_size])
86
+
87
+ def summarize_large_text(text):
88
+ chunks = list(split_text(text))
89
+ summaries = [summarizer(chunk, max_length=500, min_length=100, do_sample=False) for chunk in chunks]
90
+ combined_summary = ' '.join(sum[0]['summary_text'] for sum in summaries)
91
+ return combined_summary
92
+
93
  @app.post("/summarize")
94
  async def summarize_text(request: SummarizeRequest):
95
  try:
96
+ # Adjusting summarization for very large texts
97
+ summarized_text = summarize_large_text(request.text)
98
+ return JSONResponse(content={"summary": summarized_text})
99
  except Exception as e:
100
  # Handle exceptions that could arise during summarization
101
  raise HTTPException(status_code=500, detail=str(e))
102
+
103
 
104
  if __name__ == "__main__":
105
  uvicorn.run(app, host="0.0.0.0", port=8000)