Spaces:
Sleeping
Sleeping
Added Application files
Browse files- .gitignore +1 -0
- dockerfile +8 -0
- language_detection.py +105 -0
- requirements.txt +0 -0
.gitignore
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
.env
|
dockerfile
ADDED
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
FROM python:3.11-slim
|
2 |
+
WORKDIR /app
|
3 |
+
COPY requirements.txt .
|
4 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
5 |
+
|
6 |
+
COPY . .
|
7 |
+
EXPOSE 5000
|
8 |
+
CMD [ "python","language_detection.py" ]
|
language_detection.py
ADDED
@@ -0,0 +1,105 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import whisper
|
2 |
+
import numpy as np
|
3 |
+
from datetime import datetime
|
4 |
+
from flask import Flask,request,jsonify
|
5 |
+
from transformers import pipeline
|
6 |
+
import os
|
7 |
+
from dotenv import load_dotenv
|
8 |
+
load_dotenv()
|
9 |
+
app=Flask(__name__)
|
10 |
+
model=whisper.load_model("base")
|
11 |
+
SAMPLE_RATE=16000
|
12 |
+
DURATION=10
|
13 |
+
OUTPUT_FILE="recorded_audio.wav"
|
14 |
+
translator = pipeline("translation", model="Helsinki-NLP/opus-mt-mul-en",token=os.environ.get("HUGGING_FACE_AUTH_TOKEN"))
|
15 |
+
|
16 |
+
|
17 |
+
def detect(audio_path):
|
18 |
+
audio=whisper.load_audio(audio_path)
|
19 |
+
audio=whisper.pad_or_trim(audio)
|
20 |
+
|
21 |
+
mel=whisper.log_mel_spectrogram(audio).to(model.device)
|
22 |
+
_,probs=model.detect_language(mel)
|
23 |
+
|
24 |
+
detected_lang=max(probs,key=probs.get)
|
25 |
+
conf=probs[detected_lang]
|
26 |
+
|
27 |
+
res=model.transcribe(audio_path,language=detected_lang)
|
28 |
+
trans=res["text"]
|
29 |
+
|
30 |
+
translation = trans if detected_lang == "en" else translator(trans)[0]["translation_text"]
|
31 |
+
return detected_lang,conf,trans,translation
|
32 |
+
|
33 |
+
@app.route('/detect-language',methods=['POST'])
|
34 |
+
def detect_endpoint():
|
35 |
+
try:
|
36 |
+
if 'audio' not in request.files:
|
37 |
+
return jsonify({"status":"error","message":"No audio file provided"}),400
|
38 |
+
else:
|
39 |
+
audio_file=request.files['audio']
|
40 |
+
audio_path=os.path.join("uploads",audio_file.filename)
|
41 |
+
os.makedirs("uploads",exist_ok=True)
|
42 |
+
audio_file.save(audio_path)
|
43 |
+
'''else:
|
44 |
+
audio_path=record()'''
|
45 |
+
det_lang,conf,_,_=detect(audio_path)
|
46 |
+
if os.path.exists(audio_path) and audio_path!=OUTPUT_FILE:
|
47 |
+
os.remove(audio_path)
|
48 |
+
return jsonify({
|
49 |
+
"status":"success",
|
50 |
+
"detected_language":det_lang,
|
51 |
+
"confidence":f"{conf:.2%}"
|
52 |
+
})
|
53 |
+
except Exception as e:
|
54 |
+
return jsonify({"status":"error","message":str(e)}),500
|
55 |
+
|
56 |
+
|
57 |
+
@app.route('/transcription',methods=['POST'])
|
58 |
+
def transcription_endpoint():
|
59 |
+
try:
|
60 |
+
if 'audio' not in request.files:
|
61 |
+
return jsonify({"status":"error","message":"No audio file provided"}),400
|
62 |
+
else:
|
63 |
+
audio_file=request.files['audio']
|
64 |
+
audio_path=os.path.join("uploads",audio_file.filename)
|
65 |
+
os.makedirs("uploads",exist_ok=True)
|
66 |
+
audio_file.save(audio_path)
|
67 |
+
'''else:
|
68 |
+
audio_path=record()'''
|
69 |
+
_,_,trans,_=detect(audio_path)
|
70 |
+
if os.path.exists(audio_path) and audio_path!=OUTPUT_FILE:
|
71 |
+
os.remove(audio_path)
|
72 |
+
return jsonify({
|
73 |
+
"status":"success",
|
74 |
+
"transcription":trans
|
75 |
+
})
|
76 |
+
except Exception as e:
|
77 |
+
return jsonify({"status":"error","message":str(e)}),500
|
78 |
+
|
79 |
+
|
80 |
+
@app.route('/translation',methods=['POST'])
|
81 |
+
def translation_endpoint():
|
82 |
+
try:
|
83 |
+
if 'audio' not in request.files:
|
84 |
+
return jsonify({"status":"error","message":"No audio file provided"}),400
|
85 |
+
else:
|
86 |
+
audio_file=request.files['audio']
|
87 |
+
audio_path=os.path.join("uploads",audio_file.filename)
|
88 |
+
os.makedirs("uploads",exist_ok=True)
|
89 |
+
audio_file.save(audio_path)
|
90 |
+
'''else:
|
91 |
+
audio_path=record()'''
|
92 |
+
det_lang,_,_,trans=detect(audio_path)
|
93 |
+
if os.path.exists(audio_path) and audio_path!=OUTPUT_FILE:
|
94 |
+
os.remove(audio_path)
|
95 |
+
return jsonify({
|
96 |
+
"status":"success",
|
97 |
+
"detected_language":det_lang,
|
98 |
+
"translation":trans
|
99 |
+
})
|
100 |
+
except Exception as e:
|
101 |
+
return jsonify({"status":"error","message":str(e)}),500
|
102 |
+
|
103 |
+
if __name__=="__main__":
|
104 |
+
app.run(debug=True,host="0.0.0.0",port=5000)
|
105 |
+
|
requirements.txt
ADDED
Binary file (1.52 kB). View file
|
|