Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from fastapi import FastAPI
|
| 3 |
+
from pydantic import BaseModel
|
| 4 |
+
from transformers import pipeline, AutoTokenizer
|
| 5 |
+
|
| 6 |
+
# Ρύθμιση cache directory για να αποφύγουμε permission errors
|
| 7 |
+
os.environ["HF_HOME"] = "/app/cache"
|
| 8 |
+
|
| 9 |
+
MODEL_NAME = "IMISLab/GreekT5-mt5-small-greeksum"
|
| 10 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
| 11 |
+
summarizer = pipeline("summarization", model=MODEL_NAME, tokenizer=tokenizer)
|
| 12 |
+
|
| 13 |
+
app = FastAPI()
|
| 14 |
+
|
| 15 |
+
class TextInput(BaseModel):
|
| 16 |
+
text: str
|
| 17 |
+
|
| 18 |
+
@app.post("/summarize/")
|
| 19 |
+
async def summarize_text(input: TextInput):
|
| 20 |
+
tokens = tokenizer.tokenize(input.text)
|
| 21 |
+
token_count = len(tokens)
|
| 22 |
+
|
| 23 |
+
summary = summarizer(
|
| 24 |
+
input.text,
|
| 25 |
+
max_length=200,
|
| 26 |
+
min_length=30,
|
| 27 |
+
do_sample=False
|
| 28 |
+
)
|
| 29 |
+
|
| 30 |
+
return {
|
| 31 |
+
"token_count": token_count,
|
| 32 |
+
"summary": summary[0]["summary_text"]
|
| 33 |
+
}
|
| 34 |
+
|
| 35 |
+
if __name__ == "__main__":
|
| 36 |
+
import uvicorn
|
| 37 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|