-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathita_to_eng.py
More file actions
78 lines (66 loc) · 2.57 KB
/
Copy pathita_to_eng.py
File metadata and controls
78 lines (66 loc) · 2.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import speech_recognition as sr
from transformers import pipeline, AutoModelForSeq2SeqLM, AutoTokenizer
import torch
import wave
import io
from pydub import AudioSegment
from pydub.silence import split_on_silence
import numpy as np
def record_audio():
recognizer = sr.Recognizer()
with sr.Microphone() as source:
print("Parla ora...")
recognizer.adjust_for_ambient_noise(source)
audio = recognizer.listen(source)
return audio
def speech_to_text(audio):
recognizer = sr.Recognizer()
try:
text = recognizer.recognize_google(audio, language = "it-IT")
return text
except sr.UnknownValueError:
print("Non ho capito l'audio")
except sr.RequestError:
print("Errore nel servizio di riconosciemento vocale")
def remove_silence(audio):
wav_io = io.BytesIO(audio.get_wav_data())
with wave.open(wav_io, 'rb') as wav_file:
params = wav_file.getparams()
frames = wav_file.readframes(wav_file.getnframes())
audio_segment = AudioSegment(
data=frames,
sample_width=params.sampwidth,
frame_rate=params.framerate,
channels=params.nchannels
)
chunks = split_on_silence(audio_segment, min_silence_len=500, silence_thresh=-40)
output_audio = sum(chunks, AudioSegment.silent(duration=1000))
wav_io = io.BytesIO()
output_audio.export(wav_io, format="wav")
wav_io.seek(0)
with wave.open(wav_io, 'rb') as wav_file:
frames = wav_file.readframes(wav_file.getnframes())
sample_rate = wav_file.getframerate()
sample_width = wav_file.getsampwidth()
return sr.AudioData(frames, sample_rate, sample_width)
def translate_text(text):
device = "cuda" if torch.cuda.is_available() else "cpu"
model = AutoModelForSeq2SeqLM.from_pretrained("Helsinki-NLP/opus-mt-it-en").to(device)
tokenizer = AutoTokenizer.from_pretrained("Helsinki-NLP/opus-mt-it-en")
translator = pipeline("translation", model = model, tokenizer=tokenizer, device=0 if device == "cuda" else -1)
translation = translator(text)[0]["translation_text"]
return translation
def main():
while True:
audio = record_audio()
audio = remove_silence(audio)
italian_text = speech_to_text(audio)
if italian_text:
print(f"Testo italiano: {italian_text}")
english_text = translate_text(italian_text)
print(f"Traduzione inglese: {english_text}")
next_audio = input("Vuoi continuare? (y/n): ")
if next_audio.lower() != 'y':
break
if __name__ == "__main__":
main()