Code
Code
class RealTimeTranscription(QWidget):
def __init__(self):
super().__init__()
self.p = pyaudio.PyAudio()
self.init_ui()
self.stream = None
self.model = Model("C:\\Users\\hp\Desktop\\D Final Project\\CODE FILES\\
Models\\vosk-model-small-en-in-0.4")
self.recognizer = KaldiRecognizer(self.model, 16000)
self.transcription_running = False
self.transcription_file = None
def init_ui(self):
self.setWindowTitle('Real-Time Transcription')
self.setGeometry(100, 100, 600, 400)
self.transcription_text_edit = QTextEdit()
self.transcription_text_edit.setReadOnly(True)
self.transcription_text_edit.setMinimumHeight(300)
self.device_combo_box = QComboBox()
self.list_audio_devices()
button_layout = QHBoxLayout()
button_layout.addWidget(self.start_button)
button_layout.addWidget(self.stop_button)
button_layout.addWidget(self.device_combo_box)
main_layout = QVBoxLayout()
main_layout.addWidget(self.transcription_text_edit)
main_layout.addLayout(button_layout)
self.setLayout(main_layout)
def list_audio_devices(self):
# List all available devices and select the Bluetooth device if found
bluetooth_device_index = None
for i in range(self.p.get_device_count()):
device_info = self.p.get_device_info_by_index(i)
if device_info['maxInputChannels'] > 0:
self.device_combo_box.addItem(device_info['name'], userData=i)
if 'Bluetooth' in device_info['name']: # Automatically select
Bluetooth device if present
bluetooth_device_index = i
if bluetooth_device_index is not None:
self.device_combo_box.setCurrentIndex(self.device_combo_box.findData(bluetooth_devi
ce_index))
def start_transcription(self):
if self.transcription_running:
return
device_index = self.device_combo_box.currentData()
device_info = self.p.get_device_info_by_index(device_index)
if device_info['maxInputChannels'] < 1:
print("Selected device does not support audio input.")
return
try:
self.stream = self.p.open(format=pyaudio.paInt16,
channels=1,
rate=16000,
input=True,
frames_per_buffer=8000,
input_device_index=device_index)
self.stream.start_stream()
self.transcription_running = True
self.transcription_thread = TranscriptionThread(self.stream,
self.recognizer)
self.transcription_thread.transcription_updated.connect(self.update_transcription)
self.transcription_thread.start()
timestamp = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
file_name = f"transcription_{timestamp}.txt"
self.transcription_file = open(file_name, "a")
except Exception as e:
print(f"Error opening audio stream: {e}")
def stop_transcription(self):
if not self.transcription_running:
return
self.stream.stop_stream()
self.stream.close()
self.transcription_running = False
self.transcription_thread.quit()
self.transcription_thread.wait()
if self.transcription_file:
self.transcription_file.close()
self.transcription_file = None
class TranscriptionThread(QThread):
transcription_updated = pyqtSignal(str)
def run(self):
try:
while True:
data = self.stream.read(8000, exception_on_overflow=False)
if len(data) == 0:
break
if self.recognizer.AcceptWaveform(data):
result = json.loads(self.recognizer.Result())
if 'text' in result:
self.transcription_updated.emit(result['text'] + "\n")
except Exception as e:
print(f"Error in transcription thread: {e}")
class TranslationScreen(QWidget):
def __init__(self):
super().__init__()
self.init_ui()
def init_ui(self):
self.setWindowTitle('Translation')
self.setGeometry(100, 100, 800, 600)
self.original_text_edit = QTextEdit()
self.translated_text_edit = QTextEdit()
self.translated_text_edit.setReadOnly(True)
self.language_combo_box = QComboBox()
self.language_combo_box.addItems(['English', 'Spanish', 'French', 'Hindi',
'Kannada'])
load_button = QPushButton('Load File')
load_button.clicked.connect(self.load_file)
translate_button = QPushButton('Translate')
translate_button.clicked.connect(self.translate_text)
text_layout = QVBoxLayout()
text_layout.addWidget(self.original_text_edit)
text_layout.addWidget(self.translated_text_edit)
button_layout = QHBoxLayout()
button_layout.addWidget(load_button)
button_layout.addWidget(self.language_combo_box)
button_layout.addWidget(translate_button)
main_layout = QVBoxLayout()
main_layout.addLayout(text_layout)
main_layout.addLayout(button_layout)
self.setLayout(main_layout)
def load_file(self):
file_dialog = QFileDialog()
file_path, _ = file_dialog.getOpenFileName(self, 'Open File', '', 'Text
Files (*.txt)')
if file_path:
with open(file_path, 'r') as file:
content = file.read()
self.original_text_edit.setPlainText(content)
def translate_text(self):
original_text = self.original_text_edit.toPlainText()
target_language = self.language_combo_box.currentText()
translator = Translator()
translated = translator.translate(original_text,
dest=target_language.lower())
self.translated_text_edit.setPlainText(translated.text)
class SummarizationScreen(QWidget):
def __init__(self):
super().__init__()
self.init_ui()
self.num_sentences = 0
def init_ui(self):
self.setWindowTitle('Summarization')
self.setGeometry(100, 100, 600, 400)
self.original_text_edit = QTextEdit()
self.summarized_text_edit = QTextEdit()
self.summarized_text_edit.setReadOnly(True)
self.load_button = QPushButton('Load File')
self.load_button.clicked.connect(self.load_file)
self.summarize_button = QPushButton('Summarize')
self.summarize_button.clicked.connect(self.summarize_text)
main_layout = QVBoxLayout()
main_layout.addWidget(self.original_text_edit)
main_layout.addWidget(self.load_button)
main_layout.addWidget(self.summarized_text_edit)
main_layout.addWidget(self.summarize_button)
self.setLayout(main_layout)
def load_file(self):
file_dialog = QFileDialog()
file_path, _ = file_dialog.getOpenFileName(self, 'Open File', '', 'Text
Files (*.txt)')
if file_path:
with open(file_path, 'r') as file:
lines = file.readlines()
non_empty_lines = [line for line in lines if line.strip()]
self.num_sentences = len(non_empty_lines)
content = "".join(non_empty_lines)
self.original_text_edit.setPlainText(content)
def summarize_text(self):
original_text = self.original_text_edit.toPlainText()
if self.num_sentences > 0:
num_sentences_summary = int(self.num_sentences * 0.75)
else:
num_sentences_summary = 2
parser = PlaintextParser.from_string(original_text, Tokenizer("english"))
summarizer = LexRankSummarizer()
summary = summarizer(parser.document, num_sentences_summary)
summarized_text = " ".join(str(sentence) for sentence in summary)
self.summarized_text_edit.setPlainText(summarized_text)
class LogoScreen(QWidget):
def __init__(self, main_window):
super().__init__()
self.main_window = main_window
self.init_ui()
self.setup_animation_and_music() # Setup for animation and delayed sound
def init_ui(self):
self.setWindowTitle('Logo Screen')
self.setGeometry(100, 100, 600, 400)
layout = QVBoxLayout(self)
self.logo_label.setAlignment(Qt.AlignCenter)
layout.addWidget(self.logo_label)
# Background color
self.setAutoFillBackground(True)
palette = self.palette()
palette.setColor(self.backgroundRole(), QColor(0, 0, 0))
self.setPalette(palette)
def setup_animation_and_music(self):
# Delay sound start by 2000 milliseconds (2 seconds)
QTimer.singleShot(2000, self.playSound)
def playSound(self):
# Setup the media player to play sound
self.player = QMediaPlayer() # Create a media player object
url = QUrl.fromLocalFile("sounds/s.wav") # Adjust the sound path as
necessary
self.player.setMedia(QMediaContent(url))
self.player.play() # Play the sound after a 2-second delay
def transition_to_transcription(self):
self.hide() # Hide the current screen
self.main_window.show_transcription() # Transition to the transcription
screen
class MainWindow(QWidget):
def __init__(self):
super().__init__()
self.init_ui()
def init_ui(self):
self.setWindowTitle('Main Window')
self.setGeometry(100, 100, 600, 400)
self.logo_screen = LogoScreen(self)
self.transcription_screen = RealTimeTranscription()
self.translation_screen = TranslationScreen()
self.summarization_screen = SummarizationScreen()
self.transcription_button = QPushButton('Transcription')
self.transcription_button.clicked.connect(self.show_transcription)
self.translation_button = QPushButton('Translation')
self.translation_button.clicked.connect(self.show_translation) # Ensure
method is defined
self.summarization_button = QPushButton('Summarization')
self.summarization_button.clicked.connect(self.show_summarization) #
Ensure method is defined
self.button_layout = QHBoxLayout()
self.button_layout.addWidget(self.transcription_button)
self.button_layout.addWidget(self.translation_button)
self.button_layout.addWidget(self.summarization_button)
self.main_layout = QVBoxLayout()
self.main_layout.addLayout(self.button_layout)
self.main_layout.addWidget(self.logo_screen)
self.main_layout.addWidget(self.transcription_screen)
self.main_layout.addWidget(self.translation_screen)
self.main_layout.addWidget(self.summarization_screen)
self.setLayout(self.main_layout)
def show_transcription(self):
self.show_buttons()
self.logo_screen.hide()
self.transcription_screen.show()
self.translation_screen.hide()
self.summarization_screen.hide()
def show_translation(self):
self.show_buttons()
self.logo_screen.hide()
self.transcription_screen.hide()
self.translation_screen.show()
self.summarization_screen.hide()
def show_summarization(self):
self.show_buttons()
self.logo_screen.hide()
self.transcription_screen.hide()
self.translation_screen.hide()
self.summarization_screen.show()
def show_logo(self):
self.transcription_button.hide()
self.translation_button.hide()
self.summarization_button.hide()
self.transcription_screen.hide()
self.translation_screen.hide()
self.summarization_screen.hide()
self.logo_screen.show()
def show_buttons(self):
self.transcription_button.show()
self.translation_button.show()
self.summarization_button.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
window = MainWindow()
window.show()
try:
sys.exit(app.exec_())
except KeyboardInterrupt:
print("Application terminated by user.")