Spaces:
Sleeping
Sleeping
Delete app.py
Browse files
app.py
DELETED
@@ -1,57 +0,0 @@
|
|
1 |
-
from flask import Flask, request, jsonify
|
2 |
-
from transformers import Wav2Vec2ForCTC, Wav2Vec2Tokenizer, pipeline
|
3 |
-
from pydub import AudioSegment
|
4 |
-
import torch
|
5 |
-
import torchaudio
|
6 |
-
from datetime import datetime, time
|
7 |
-
import pytz
|
8 |
-
|
9 |
-
app = Flask(__name__)
|
10 |
-
|
11 |
-
# Load speech recognition model and tokenizer
|
12 |
-
tokenizer = Wav2Vec2Tokenizer.from_pretrained("facebook/wav2vec2-base-960h")
|
13 |
-
model = Wav2Vec2ForCTC.from_pretrained("facebook/wav2vec2-base-960h")
|
14 |
-
|
15 |
-
# Load translation model explicitly
|
16 |
-
translation_model = "Helsinki-NLP/opus-mt-en-hi"
|
17 |
-
translation_pipeline = pipeline("translation", model=translation_model)
|
18 |
-
|
19 |
-
# Function to preprocess audio
|
20 |
-
def preprocess_audio(audio_file):
|
21 |
-
audio = AudioSegment.from_file(audio_file)
|
22 |
-
audio = audio.set_frame_rate(16000)
|
23 |
-
audio.export("processed.wav", format="wav")
|
24 |
-
waveform, sample_rate = torchaudio.load("processed.wav")
|
25 |
-
return waveform
|
26 |
-
|
27 |
-
# Function to check if the current time is after 6 PM IST
|
28 |
-
def is_after_6pm_ist():
|
29 |
-
ist = pytz.timezone('Asia/Kolkata')
|
30 |
-
current_time = datetime.now(ist).time()
|
31 |
-
return current_time >= time(18, 0)
|
32 |
-
|
33 |
-
@app.route('/translate', methods=['POST'])
|
34 |
-
def translate():
|
35 |
-
if not is_after_6pm_ist():
|
36 |
-
return jsonify({"error": "Service is available only after 6 PM IST"}), 403
|
37 |
-
|
38 |
-
if 'audio' not in request.files:
|
39 |
-
return jsonify({"error": "No audio file provided"}), 400
|
40 |
-
|
41 |
-
audio_file = request.files['audio']
|
42 |
-
waveform = preprocess_audio(audio_file)
|
43 |
-
|
44 |
-
input_values = tokenizer(waveform.squeeze().numpy(), return_tensors="pt").input_values
|
45 |
-
logits = model(input_values).logits
|
46 |
-
predicted_ids = torch.argmax(logits, dim=-1)
|
47 |
-
transcription = tokenizer.batch_decode(predicted_ids)[0]
|
48 |
-
|
49 |
-
translation = translation_pipeline(transcription)
|
50 |
-
translated_text = translation[0]['translation_text']
|
51 |
-
|
52 |
-
return jsonify({"transcription": transcription, "translation": translated_text})
|
53 |
-
|
54 |
-
if __name__ == '__main__':
|
55 |
-
app.run(debug=True, host='0.0.0.0', port=8080)
|
56 |
-
|
57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|