Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -1,21 +1,100 @@
|
|
| 1 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
from pathlib import Path
|
| 3 |
|
| 4 |
app = FastAPI()
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
| 8 |
|
| 9 |
-
|
| 10 |
-
save_directory.mkdir(parents=True, exist_ok=True)
|
| 11 |
|
| 12 |
-
|
|
|
|
| 13 |
|
| 14 |
-
|
| 15 |
-
content = await file.read()
|
| 16 |
-
saved_file.write(content)
|
| 17 |
|
| 18 |
-
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
from transformers import AutoModelForSpeechSeq2Seq, AutoProcessor, pipeline
|
| 3 |
+
from datasets import load_dataset
|
| 4 |
+
from googletrans import Translator
|
| 5 |
+
from fastapi import FastAPI, File, UploadFile, HTTPException
|
| 6 |
+
from fastapi.responses import JSONResponse
|
| 7 |
from pathlib import Path
|
| 8 |
|
| 9 |
app = FastAPI()
|
| 10 |
|
| 11 |
+
device = "cuda:0" if torch.cuda.is_available() else "cpu"
|
| 12 |
+
torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32
|
| 13 |
|
| 14 |
+
model_id = "openai/whisper-large-v3"
|
|
|
|
| 15 |
|
| 16 |
+
model = AutoModelForSpeechSeq2Seq.from_pretrained(model_id, torch_dtype=torch_dtype, low_cpu_mem_usage=True, use_safetensors=True)
|
| 17 |
+
model.to(device)
|
| 18 |
|
| 19 |
+
processor = AutoProcessor.from_pretrained(model_id)
|
|
|
|
|
|
|
| 20 |
|
| 21 |
+
pipe = pipeline(
|
| 22 |
+
"automatic-speech-recognition",
|
| 23 |
+
model=model,
|
| 24 |
+
tokenizer=processor.tokenizer,
|
| 25 |
+
feature_extractor=processor.feature_extractor,
|
| 26 |
+
max_new_tokens=256,
|
| 27 |
+
chunk_length_s=30,
|
| 28 |
+
batch_size=16,
|
| 29 |
+
return_timestamps=True,
|
| 30 |
+
torch_dtype=torch_dtype,
|
| 31 |
+
device=device,
|
| 32 |
+
)
|
| 33 |
|
| 34 |
+
dataset = load_dataset("distil-whisper/librispeech_long", "clean", split="validation")
|
| 35 |
+
|
| 36 |
+
@app.post("/voice_recognition")
|
| 37 |
+
async def process_audio(file: UploadFile = File(...)):
|
| 38 |
+
try:
|
| 39 |
+
# File
|
| 40 |
+
save_directory = Path("/home/user")
|
| 41 |
+
save_directory.mkdir(parents=True, exist_ok=True)
|
| 42 |
+
file_location = save_directory / file.filename
|
| 43 |
+
|
| 44 |
+
with open(file_location, "wb") as saved_file:
|
| 45 |
+
content = await file.read()
|
| 46 |
+
saved_file.write(content)
|
| 47 |
+
|
| 48 |
+
# JP
|
| 49 |
+
original = pipe(file_location)
|
| 50 |
+
original_version = original["text"]
|
| 51 |
+
|
| 52 |
+
# EN
|
| 53 |
+
result = pipe(file_location, generate_kwargs={"task": "translate"})
|
| 54 |
+
hasil = result["text"]
|
| 55 |
+
|
| 56 |
+
# ID
|
| 57 |
+
detect = detect_google(hasil)
|
| 58 |
+
id_ver = translate_google(hasil, f"{detect}", "ID")
|
| 59 |
+
|
| 60 |
+
# Additional modifications
|
| 61 |
+
id_ver = modify_text(id_ver)
|
| 62 |
+
|
| 63 |
+
return JSONResponse(content={"response": {"jp_text": original_version, "en_text": hasil, "id_text": id_ver}}, status_code=200)
|
| 64 |
+
|
| 65 |
+
except Exception as e:
|
| 66 |
+
return HTTPException(status_code=500, detail=f"Error: {e}")
|
| 67 |
+
|
| 68 |
+
def detect_google(text):
|
| 69 |
+
try:
|
| 70 |
+
translator = Translator()
|
| 71 |
+
detected_lang = translator.detect(text)
|
| 72 |
+
return detected_lang.lang.upper()
|
| 73 |
+
except Exception as e:
|
| 74 |
+
print(f"Error detect: {e}")
|
| 75 |
+
return None
|
| 76 |
+
|
| 77 |
+
def translate_google(text, source, target):
|
| 78 |
+
try:
|
| 79 |
+
translator = Translator()
|
| 80 |
+
translated_text = translator.translate(text, src=source, dest=target)
|
| 81 |
+
return translated_text.text
|
| 82 |
+
except Exception as e:
|
| 83 |
+
print(f"Error translate: {e}")
|
| 84 |
+
return None
|
| 85 |
+
|
| 86 |
+
def modify_text(text):
|
| 87 |
+
# Additional modifications, case-sensitive
|
| 88 |
+
replacements = {
|
| 89 |
+
"Tuan": "Master",
|
| 90 |
+
"tuan": "Master",
|
| 91 |
+
"Guru": "Master",
|
| 92 |
+
"guru": "Master",
|
| 93 |
+
"Monica": "Monika",
|
| 94 |
+
"monica": "Monika",
|
| 95 |
+
}
|
| 96 |
+
|
| 97 |
+
for original, replacement in replacements.items():
|
| 98 |
+
text = text.replace(original, replacement)
|
| 99 |
+
|
| 100 |
+
return text
|