| |
|
|
| from fastapi import FastAPI |
|
|
| from fastapi.middleware.cors import CORSMiddleware |
| import os |
|
|
| from pytube import YouTube |
|
|
| import whisper |
|
|
| from transformers import M2M100ForConditionalGeneration, M2M100Tokenizer |
|
|
| import torch |
|
|
| |
|
|
| nombre_archivo = "audio.mp3" |
| app = FastAPI() |
|
|
| origins = [ |
| "http://localhost", |
| "http://localhost:8080", |
| "http://localhost:3000", |
| "http://localhost:8000", |
| ] |
|
|
| app.add_middleware( |
| CORSMiddleware, |
| allow_origins=origins, |
| allow_credentials=True, |
| allow_methods=['*'], |
| allow_headers=["*"], |
| ) |
|
|
| |
| if torch.cuda.is_available(): |
| device = torch.device("cuda") |
| else: |
| device = torch.device("cpu") |
|
|
|
|
| |
| |
| @app.get("/") |
| def index(): |
| return "hola" |
|
|
| @app.post("/descargar/{url}") |
| def descargar_audio(url): |
| url = "https://www.youtube.com/watch?v=" + url |
| yt = YouTube(url) |
| ruta_archivo = "/data" + nombre_archivo |
| yt.streams.filter(only_audio=True).first().download(output_path=ruta_archivo, filename=nombre_archivo) |
| return {"url": "Listo"} |
|
|
| @app.get("/transcribir") |
| def transcribir(): |
| print("Transcribiendo...") |
| model = whisper.load_model("medium", device=device) |
| result = model.transcribe(nombre_archivo, fp16=False) |
| transcipcion = result["text"] |
| print(transcipcion) |
| return {"transcripcion": transcipcion} |
|
|
| @app.get("/traducir/{idioma}/{idioma_destino}") |
| def traducir(idioma,idioma_destino): |
|
|
| model = M2M100ForConditionalGeneration.from_pretrained("facebook/m2m100_1.2B").to(device) |
| tokenizer = M2M100Tokenizer.from_pretrained("facebook/m2m100_1.2B") |
|
|
| f = open ('texto.txt','r') |
| texto = f.read() |
| f.close() |
|
|
| listaPalabras = texto.split(" ") |
| lista = [] |
| palabras_sobrantes = [] |
| result_stringF = "" |
|
|
| i = 0 |
| while i < len(listaPalabras): |
|
|
| if i % 100 == 0: |
| textoResult = " ".join(lista) |
| tokenizer.src_lang = idioma |
| encoded_hi = tokenizer(textoResult, return_tensors="pt").to(device) |
| generated_tokens = model.generate(**encoded_hi, forced_bos_token_id=tokenizer.get_lang_id(idioma_destino)) |
| |
| results = tokenizer.batch_decode(generated_tokens, skip_special_tokens=True) |
| result_string = " ".join(results) |
| result_stringF = result_stringF + result_string |
| lista = [] |
|
|
| lista.append(listaPalabras[i]) |
|
|
| if i == len(listaPalabras) - 1: |
| palabras_sobrantes.extend(lista) |
|
|
| i = i + 1 |
|
|
| if len(palabras_sobrantes) > 0: |
| textoResult = " ".join(palabras_sobrantes) |
| tokenizer.src_lang = idioma |
| encoded_hi = tokenizer(textoResult, return_tensors="pt").to(device) |
| generated_tokens = model.generate(**encoded_hi, forced_bos_token_id=tokenizer.get_lang_id(idioma_destino)) |
| |
| results = tokenizer.batch_decode(generated_tokens, skip_special_tokens=True) |
| result_string = " ".join(results) |
| result_stringF = result_stringF + result_string |
|
|
|
|
| return {"Traduccion": results} |
|
|