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