Developing speech to text transcription on python

Hello all, I need kindly your help to develop a python program which converts speech to text. I am using whisper, torch and sys modules but the app does not seems to work correctly yet. Consequently, I do not know yet how to implement and approach this problem yet.
This is my code

import whisper
import torch
import sys

def transcribe_audio(audio_path, language="el"):
    """
    Transcribes the given audio file to text in the specified language using Whisper.

    Args:
        audio_path (str): Path to the audio file.
        language (str): Language code for transcription (default is 'el' for Greek).

    Returns:
        str: The transcribed text.
    """

    device = "cuda" if torch.cuda.is_available() else "cpu"
    print(f"Using device: {device}")


    model = whisper.load_model("small", device=device)


    print(f"Transcribing {audio_path} in language: {language}...")
    result = model.transcribe(audio_path, language=language, task="transcribe")
    return result["text"]

if __name__ == "__main__":
    if len(sys.argv) < 2:
        print("Usage: python speech_to_text.py <audio_file_path>")
        sys.exit(1)

    audio_file = sys.argv[1]
    greek_text = transcribe_audio(audio_file, language="el")
    print("Transcribed Greek Text:")
    print(greek_text)

Thank you in advance,

Alex

Well i’m not a python expert, but shouldnt that just be transcribe_audio(audio_file, "el") ?

You have be more specific about what the issue is. The code seems fine.

I try running your code in my local computer but couldn’t pip install wisper, some compatibility issue with the latest Python version.

So I run it in Google colab, using this sample file:

https://www.biblicallanguagecenter.com/wp-content/uploads/2011/04/ab-1Jn1.mp3

My greek is not good. So here is what I got.

Transcribed Greek Text:
Κεφαλαιών α, ο εναπαρχές, ο ακεκόμεν, ο εοράκμεν του σωθέρμου σημών, ο εθεασάμεθα, καεγίρε σημών εψηλαφισάν, πρει, του λόγου τεζοές. Καεζοεία φανερώθε, καεοράκαμεν, καεμαρτυρούμεν, καεαπανγέλουμεν εμην τεζοέντεν εώνιον, έτης ειν πρόστον πατέρα, καεφανερώθε εμην. Ο εοράκαμεν, κα ακεκόμεν, απανγέλουμεν καεημην, ειναι καημής, κοινουνιαν αχητεμεθαίμων, καεικοινουνιαδε αιεμετερα μετά του Πατρός, καημετά του Ιηουαυτού Ιησού Χριστού. Καεταύτε γράφωμεν εμμής, ειναι εχαράημων, ειππλευρωμέναι. Καεστην αυτε εγγελία, ειναι ακεκόμεν απ’ αυτού, και αναγγέλουμεν εμην, ότι ο Θεός φώσε στην, και σκοττία εναυτό ουκ εστην ουδεμία. Εάν είπομεν ότι κοινωνίαν εχομεν μεταυτού, και εντοσκοττή περιπατώμεν, ψευδόμεθα, κ’ ουπιούμεν τευναλήθιον. Εάν δε εντοφωτή περιπατώμεν, ως αυτος εστιν εντοφωτή, κοινωνίαν εχομεν μεταλλέλλον, και το εμμε Ιησού του Ιηουαυτού καταρρίζε εμμάς αποπάσες αμαρτίας. Εάν είπομεν ότι αμαρτίαν ουκ εχομεν, εαυτούς πλανόμεν, και εαλέθεια ουκ εστιν εμμην. Εάν ομολοχόμεν τάς αμαρτίας εμμον, πιστώσες την καιδικαίως, είνα αφ’ εμμην τάς αμαρτίας, και καθαρίσαι εμάς αποπάσες αδικίας. Εάν είπομεν ότι ουκ εμαρτέκαμεν, ψευστιν πιούμεν αυτον, και ο λόγος αυτού ουκ εστιν εμμην.

1 Like

One quick thing you might wanna check: sometimes Whisper has trouble with language codes or file formats. Try removing the language=“el” part and let it auto-detect the language - see if that helps. Also, double-check that your audio file is in a supported format like WAV or MP3. I had a similar issue once and it turned out the audio was just too short or too quiet.

1 Like

Hello guys and sorry for my late response. I will check out the supported format (mp3 or wav of the file I want to transcribe)again. The problem now is that I have no output on the console. I will test a larger wav or mp3 file format to see what’s going on.

Thank you in advance

Alex.