import os from fastapi import FastAPI, Body, File, UploadFile, Request from fastapi.responses import HTMLResponse, RedirectResponse from fastapi.staticfiles import StaticFiles from fastapi.templating import Jinja2Templates from models.Flashcardd import Flashcard from translations.translate import load_model_and_tokenizer, translate app = FastAPI() @app.get("/") async def home(): return {"message": "hola-mondo"} @app.post("/flashcards/create_flashcard") async def create_flashcard(new_flashcard=Body()) -> Flashcard: print(f"new flashcard: {new_flashcard}") from_lang = new_flashcard["from"] to_lang = new_flashcard["to"] model, tokenizer = load_model_and_tokenizer(from_lang=from_lang, to_lang=to_lang) translation = translate(new_flashcard["word"], model, tokenizer) return Flashcard( name=new_flashcard["word"], translation=translation, # sample_sentence=f"{new_flashcard['word']}", sample_sentence="notes..", # "translation": f"translation: {new_flashcard['word']}", # "translation": translation, )