0% found this document useful (0 votes)
4 views

Code

Uploaded by

Sneha Singh
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Code

Uploaded by

Sneha Singh
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 7

import sys

from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QHBoxLayout,


QTextEdit, QPushButton, QFileDialog, QComboBox, QLabel
from PyQt5.QtCore import QThread, pyqtSignal, QTimer, Qt
from googletrans import Translator
from datetime import datetime
from sumy.parsers.plaintext import PlaintextParser
from sumy.nlp.tokenizers import Tokenizer
from sumy.summarizers.lex_rank import LexRankSummarizer
from PyQt5.QtGui import QColor, QFont, QPixmap
from PyQt5.QtMultimedia import QSound
import json
from vosk import Model, KaldiRecognizer
import pyaudio
from PyQt5.QtGui import QMovie
from PyQt5.QtWidgets import QWidget, QVBoxLayout, QLabel, QApplication
from PyQt5.QtCore import QTimer, Qt
from PyQt5.QtGui import QMovie, QFont
from PyQt5.QtMultimedia import QMediaPlayer, QMediaContent
from PyQt5.QtCore import QUrl

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.start_button = QPushButton('Start Transcription')


self.start_button.clicked.connect(self.start_transcription)
self.stop_button = QPushButton('Stop Transcription')
self.stop_button.clicked.connect(self.stop_transcription)

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 update_transcription(self, text):


self.transcription_text_edit.append(text)
if self.transcription_file:
self.transcription_file.write(text + "\n")
self.transcription_file.flush()

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 __init__(self, stream, recognizer):


super().__init__()
self.stream = stream
self.recognizer = recognizer

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)

# Set up QLabel for GIF


self.logo_label = QLabel(self)
self.movie = QMovie('images/F9.gif') # Ensure the path is correct
self.logo_label.setMovie(self.movie)
self.movie.start() # Start the GIF animation

self.logo_label.setAlignment(Qt.AlignCenter)
layout.addWidget(self.logo_label)

# Additional label styling


self.transcriber_label = QLabel('Transcriber AI', self)
self.transcriber_label.setAlignment(Qt.AlignCenter)
self.transcriber_label.setFont(QFont('Arial', 24))
self.transcriber_label.setStyleSheet("color: white")
layout.addWidget(self.transcriber_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)

# Set the timer to transition after 5 seconds


QTimer.singleShot(8000, self.transition_to_transcription)

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)

# Initially hide everything except the logo screen


self.show_logo()

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.")

You might also like