Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,7 +4,8 @@ from fastapi.middleware.cors import CORSMiddleware
|
|
| 4 |
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
| 5 |
import os
|
| 6 |
import gc
|
| 7 |
-
import torch
|
|
|
|
| 8 |
|
| 9 |
app = FastAPI()
|
| 10 |
|
|
@@ -34,7 +35,7 @@ def summarize(req: SummarizeRequest):
|
|
| 34 |
}
|
| 35 |
|
| 36 |
if req.model_type not in models:
|
| 37 |
-
raise HTTPException(status_code=400, detail="Model
|
| 38 |
|
| 39 |
try:
|
| 40 |
model_repo = models[req.model_type]
|
|
@@ -47,7 +48,7 @@ def summarize(req: SummarizeRequest):
|
|
| 47 |
if req.model_type == "t5":
|
| 48 |
text_input = "summarize: " + text_input
|
| 49 |
|
| 50 |
-
print("-> [2/3]
|
| 51 |
inputs = tokenizer(text_input, return_tensors="pt", max_length=1024, truncation=True)
|
| 52 |
|
| 53 |
in_ids = inputs["input_ids"]
|
|
@@ -72,7 +73,7 @@ def summarize(req: SummarizeRequest):
|
|
| 72 |
early_stopping=True
|
| 73 |
)
|
| 74 |
|
| 75 |
-
print("-> [3/3]
|
| 76 |
summary_result = tokenizer.decode(summary_ids[0], skip_special_tokens=True)
|
| 77 |
|
| 78 |
summary_result = summary_result.strip()
|
|
@@ -88,10 +89,12 @@ def summarize(req: SummarizeRequest):
|
|
| 88 |
del tokenizer
|
| 89 |
del model
|
| 90 |
gc.collect()
|
| 91 |
-
print("->
|
| 92 |
|
| 93 |
return {"status": "success", "summary": summary_result}
|
| 94 |
|
| 95 |
except Exception as e:
|
| 96 |
-
print(
|
|
|
|
|
|
|
| 97 |
raise HTTPException(status_code=500, detail=str(e))
|
|
|
|
| 4 |
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
| 5 |
import os
|
| 6 |
import gc
|
| 7 |
+
import torch
|
| 8 |
+
import traceback
|
| 9 |
|
| 10 |
app = FastAPI()
|
| 11 |
|
|
|
|
| 35 |
}
|
| 36 |
|
| 37 |
if req.model_type not in models:
|
| 38 |
+
raise HTTPException(status_code=400, detail="Model not recognized")
|
| 39 |
|
| 40 |
try:
|
| 41 |
model_repo = models[req.model_type]
|
|
|
|
| 48 |
if req.model_type == "t5":
|
| 49 |
text_input = "summarize: " + text_input
|
| 50 |
|
| 51 |
+
print("-> [2/3] Starting the AI computation process (please wait)...", flush=True)
|
| 52 |
inputs = tokenizer(text_input, return_tensors="pt", max_length=1024, truncation=True)
|
| 53 |
|
| 54 |
in_ids = inputs["input_ids"]
|
|
|
|
| 73 |
early_stopping=True
|
| 74 |
)
|
| 75 |
|
| 76 |
+
print("-> [3/3] Computation complete! Translating tokens to text...", flush=True)
|
| 77 |
summary_result = tokenizer.decode(summary_ids[0], skip_special_tokens=True)
|
| 78 |
|
| 79 |
summary_result = summary_result.strip()
|
|
|
|
| 89 |
del tokenizer
|
| 90 |
del model
|
| 91 |
gc.collect()
|
| 92 |
+
print("-> Success. The RAM has been cleared.", flush=True)
|
| 93 |
|
| 94 |
return {"status": "success", "summary": summary_result}
|
| 95 |
|
| 96 |
except Exception as e:
|
| 97 |
+
print("=== FULL ERROR TRACEBACK ===", flush=True)
|
| 98 |
+
traceback.print_exc()
|
| 99 |
+
print("============================", flush=True)
|
| 100 |
raise HTTPException(status_code=500, detail=str(e))
|