Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI
|
| 2 |
+
from typing import List
|
| 3 |
+
|
| 4 |
+
from pydantic import BaseModel
|
| 5 |
+
|
| 6 |
+
import torch
|
| 7 |
+
import transformers
|
| 8 |
+
|
| 9 |
+
app = FastAPI()
|
| 10 |
+
|
| 11 |
+
class HebrewText(BaseModel):
|
| 12 |
+
text: List[str]
|
| 13 |
+
|
| 14 |
+
@app.post("/diacritize/")
|
| 15 |
+
async def diacritize_hebrew(hebrew_text: HebrewText):
|
| 16 |
+
model_name = "sadafwalliyani/D_Nikud_model"
|
| 17 |
+
tokenizer = transformers.AutoTokenizer.from_pretrained(model_name)
|
| 18 |
+
model = transformers.AutoModel.from_pretrained(model_name)
|
| 19 |
+
|
| 20 |
+
input_ids = torch.tensor(tokenizer.encode(hebrew_text.text, return_tensors="pt")).to(model.device)
|
| 21 |
+
|
| 22 |
+
# Generate a response using the model's generate function
|
| 23 |
+
response = model.generate(
|
| 24 |
+
input_ids,
|
| 25 |
+
max_length=100,
|
| 26 |
+
num_beams=5,
|
| 27 |
+
early_stopping=True,
|
| 28 |
+
return_dict_in_generate=True,
|
| 29 |
+
output_scores=True,
|
| 30 |
+
output_hidden_states=False,
|
| 31 |
+
return_attention_mask=True,
|
| 32 |
+
use_cache=True,
|
| 33 |
+
)
|
| 34 |
+
|
| 35 |
+
# Decode the output
|
| 36 |
+
output_text = tokenizer.decode(response.sequences[0])
|
| 37 |
+
|
| 38 |
+
return {"text": output_text}
|