Spaces:
Sleeping
Sleeping
Commit
·
338a103
1
Parent(s):
833c9f8
Upscale
Browse files- Dockerfile +11 -0
- main.py +96 -0
- requirements.txt +4 -0
Dockerfile
ADDED
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
FROM python:3.10.9
|
2 |
+
|
3 |
+
WORKDIR /code
|
4 |
+
|
5 |
+
COPY ./requirements.txt /code/requirements.txt
|
6 |
+
|
7 |
+
RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
|
8 |
+
|
9 |
+
COPY . .
|
10 |
+
|
11 |
+
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]
|
main.py
ADDED
@@ -0,0 +1,96 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 pyngrok import ngrok
|
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 |
+
file_path = f"{file.filename}"
|
41 |
+
with open(file_path, "wb") as f:
|
42 |
+
f.write(file.file.read())
|
43 |
+
|
44 |
+
# JP
|
45 |
+
original = pipe(file_path)
|
46 |
+
original_version = original["text"]
|
47 |
+
|
48 |
+
# EN
|
49 |
+
result = pipe(file_path, generate_kwargs={"task": "translate"})
|
50 |
+
hasil = result["text"]
|
51 |
+
|
52 |
+
# ID
|
53 |
+
detect = detect_google(hasil)
|
54 |
+
id_ver = translate_google(hasil, f"{detect}", "ID")
|
55 |
+
|
56 |
+
# Additional modifications
|
57 |
+
id_ver = modify_text(id_ver)
|
58 |
+
|
59 |
+
return JSONResponse(content={"response": {"jp_text": original_version, "en_text": hasil, "id_text": id_ver}}, status_code=200)
|
60 |
+
|
61 |
+
except Exception as e:
|
62 |
+
return HTTPException(status_code=500, detail=f"Error: {e}")
|
63 |
+
|
64 |
+
def detect_google(text):
|
65 |
+
try:
|
66 |
+
translator = Translator()
|
67 |
+
detected_lang = translator.detect(text)
|
68 |
+
return detected_lang.lang.upper()
|
69 |
+
except Exception as e:
|
70 |
+
print(f"Error detect: {e}")
|
71 |
+
return None
|
72 |
+
|
73 |
+
def translate_google(text, source, target):
|
74 |
+
try:
|
75 |
+
translator = Translator()
|
76 |
+
translated_text = translator.translate(text, src=source, dest=target)
|
77 |
+
return translated_text.text
|
78 |
+
except Exception as e:
|
79 |
+
print(f"Error translate: {e}")
|
80 |
+
return None
|
81 |
+
|
82 |
+
def modify_text(text):
|
83 |
+
# Additional modifications, case-sensitive
|
84 |
+
replacements = {
|
85 |
+
"Tuan": "Master",
|
86 |
+
"tuan": "Master",
|
87 |
+
"Guru": "Master",
|
88 |
+
"guru": "Master",
|
89 |
+
"Monica": "Monika",
|
90 |
+
"monica": "Monika",
|
91 |
+
}
|
92 |
+
|
93 |
+
for original, replacement in replacements.items():
|
94 |
+
text = text.replace(original, replacement)
|
95 |
+
|
96 |
+
return text
|
requirements.txt
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
fastapi
|
2 |
+
uvicorn
|
3 |
+
googletrans==4.0.0rc1
|
4 |
+
--upgrade git+https://github.com/huggingface/transformers.git accelerate datasets[audio]
|