Skip to content

Commit

Permalink
Merge pull request #14 from karthagokul/beta
Browse files Browse the repository at this point in the history
code cleanup
  • Loading branch information
karthagokul authored Mar 19, 2023
2 parents 335d02b + ad86cca commit e5bf90d
Show file tree
Hide file tree
Showing 13 changed files with 200 additions and 136 deletions.
18 changes: 17 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,23 @@

# sentimeter

Reading the Emotions From Text / Youtube video or live speech
Sentimeter is an app written in python to understand the emotions in audio / text. Currently it has the below modules
* Audio Module
Process the Emotions in a given audio file. Currently it supports only wav file . In future we will add more formats

* Live Speech
Process the Emotions in a live speech

* Telegram Mode
Sentimeter has a Telegram Bot, it can process the emotions in a Telegram chat

* Text File
Process a text file and identifies the emotions

* Youtube Video
Under Development




## Usage
Expand Down
6 changes: 6 additions & 0 deletions __init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
Application Main
"""
8 changes: 7 additions & 1 deletion core/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
Application Main
"""
from core.emotions_engine import EmotionsEngine
from core.sttengine import STTEngine
from core.user_interfaces import SentimeterSimpleUI
from core.user_interfaces import SentimeterSimpleUI
41 changes: 22 additions & 19 deletions core/emotions_engine.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
Class Description
The Core Engine
"""

import logging
Expand All @@ -9,14 +12,14 @@

class SentiMeterModule:
def __init__(self) -> None:
self.active=False
self.active = False

def start(self):
self.active=True
self.active = True
return self.active

def stop(self):
self.active=False
self.active = False
return self.active

def is_active(self):
Expand All @@ -25,45 +28,45 @@ def is_active(self):

class EngineObserver:
def __init__(self, name) -> None:
'''
"""
A Name is given for observer
'''
"""
self.name = name

def on_event(self, result, emotions):
'''
"""
Observer Abstract Method
'''
"""
pass


class EmotionsEngine():
'''
class EmotionsEngine:
"""
The class responsible to identify the emotions of a given text
'''
"""

def __init__(self) -> None:
'''
"""
constructor , the object acts as singleton
'''
"""
self._listeners = {}

def add_observer(self, observer):
'''
"""
The Engine events are propogated via an observer mechanism, Objects can register via this method
'''
"""
self._listeners[observer.name] = observer

def remove_observer(self, observer):
'''
"""
Remove an observer ,Todo : Check if observer is not registered to avoid exceptions
'''
"""
self._listeners.pop(observer.name)

def process_text(self, result):
'''
"""
The Functions to start the processing
'''
"""
emotions = te.get_emotion(result)
# Lets inform all the listerners
for obj in self._listeners:
Expand Down
21 changes: 13 additions & 8 deletions core/sttengine.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,36 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
Speech to Text Module, Currently uses Google STT, Future we will add more offline Engines
"""
import logging
import speech_recognition as sr


class STTEngine:
'''
"""
Speech to Text Engine, Currently using Google Service, Options to be added for Sphinix
'''
"""

def __init__(self, recognizer) -> None:
'''
"""
constructor
'''
"""
self.recognizer = recognizer
self.lang = "en-IN"
pass

def speech_to_text(self, audiodata):
'''
"""
function to get the text for inputed audio data
'''
"""
try:
actual_result = self.recognizer.recognize_google(
audiodata, language=self.lang, show_all=True
)
except sr.UnknownValueError:
logging.fatal(
"Google Speech Recognition could not understand audio")
logging.fatal("Google Speech Recognition could not understand audio")
exit(-1)
except sr.RequestError as e:
logging.fatal("Error service; {0}".format(e))
Expand Down
7 changes: 7 additions & 0 deletions core/user_interfaces.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
File which can handle various user interfaces , Currently only basic commandlines
"""
import core.emotions_engine as emotions_engine
from core.emotions_engine import EngineObserver


class SentimeterSimpleUI(EngineObserver):
def __init__(self, name) -> None:
super().__init__(name)
Expand Down
9 changes: 8 additions & 1 deletion modules/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
Application Main
"""

from modules.livespeech_module import LiveSpeechModule
from modules.telegram_module import TelegramModule
from modules.audio_module import AudioModule
from modules.audio_module import AudioModule
44 changes: 26 additions & 18 deletions modules/audio_module.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
Module for processing offline Audiofile
"""
import core.emotions_engine as emotions_engine
from core.emotions_engine import SentiMeterModule
from pydub import AudioSegment
Expand All @@ -12,21 +18,22 @@
import speech_recognition as sr
from core.user_interfaces import SentimeterSimpleUI

class AudioModule (SentiMeterModule):
'''

class AudioModule(SentiMeterModule):
"""
Classs Abstracts Audio Related features
'''
"""

def __init__(self,path) -> None:
'''
def __init__(self, path) -> None:
"""
Constructor
path : location to a .wav file , in future we can add support for mp3 etc
'''
"""
super().__init__()
self.recognizer = sr.Recognizer()
self.backend = STTEngine(self.recognizer)
self.path=path
self.ui=SentimeterSimpleUI("Audio UI")
self.path = path
self.ui = SentimeterSimpleUI("Audio UI")

def start(self):
super().start()
Expand All @@ -35,14 +42,15 @@ def start(self):
# open the audio file using pydub
sound = AudioSegment.from_wav(self.path)
# split audio sound where silence is 1000 miliseconds or more and get chunks
chunks = split_on_silence(sound,
# experiment with this value for your target audio file
min_silence_len=1000,
# adjust this per requirement
silence_thresh=sound.dBFS-14,
# keep the silence for 1 second, adjustable as well
keep_silence=1000,
)
chunks = split_on_silence(
sound,
# experiment with this value for your target audio file
min_silence_len=1000,
# adjust this per requirement
silence_thresh=sound.dBFS - 14,
# keep the silence for 1 second, adjustable as well
keep_silence=1000,
)
folder_name = "audio-chunks"
# create a directory to store the audio chunks
if not os.path.isdir(folder_name):
Expand All @@ -61,9 +69,9 @@ def start(self):
return True

def stop(self):
'''
"""
Routines to stop the Engine
'''
"""
super().stop()
logging.debug("Stopping IntelliAudio")
if not self.sttrunner.is_alive:
Expand Down
41 changes: 21 additions & 20 deletions modules/livespeech_module.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
IntelliAudio Class who proces live speech Audio Data file , Todo : May be split the class?
"""
Expand All @@ -19,56 +22,55 @@


class STTLiveTranscoder(Thread):
'''
"""
Threaded implementation to handle live audio with a processing FIFO
'''
"""

def __init__(self, backend):
'''
"""
Constructor
'''
"""
Thread.__init__(self)
self.backend = backend
self.queue = Queue()
self.ui=SentimeterSimpleUI("Live UI")
self.ui = SentimeterSimpleUI("Live UI")

def put_data(self, audio_data):
'''
"""
Adds the audio data to processing queue
'''
"""
self.queue.put(audio_data)

def run(self):
'''
"""
Runner
'''
"""
while True:
audiodata = self.queue.get()
result = self.backend.speech_to_text(audiodata)
emotions_engine.engine.process_text(result)
return True


class LiveSpeechModule (SentiMeterModule):
'''
class LiveSpeechModule(SentiMeterModule):
"""
Classs Abstracts Audio Related features
'''
"""

def __init__(self) -> None:
'''
"""
Constructor
'''
"""
super().__init__()
self.recognizer = sr.Recognizer()
self.entries = []
self.backend = STTEngine(self.recognizer)
self.sttrunner = STTLiveTranscoder(self.backend)


def start(self):
'''
"""
Process Microphone
'''
"""
super().start()
self.sttrunner.start()
while True:
Expand All @@ -77,11 +79,10 @@ def start(self):
audio_data = self.recognizer.record(source, duration=5)
self.sttrunner.put_data(audio_data)


def stop(self):
'''
"""
Routines to stop the Engine
'''
"""
super().stop()
logging.debug("Stopping IntelliAudio")
if not self.sttrunner.is_alive:
Expand Down
Loading

0 comments on commit e5bf90d

Please sign in to comment.