pratikshahp commited on
Commit
3c4ad65
·
verified ·
1 Parent(s): 55f72d6

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -0
app.py ADDED
@@ -0,0 +1,51 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Transform an audio to text script with language detection.
2
+ # Author: Pratiksha Patel
3
+
4
+ # Description: This script record the audio, transform it to text, detect the language of the file and save it to a txt file.
5
+ # import required modules
6
+ import os
7
+ import whisper
8
+ from langdetect import detect
9
+ import torch
10
+ import streamlit as st
11
+ from audio_recorder_streamlit import audio_recorder
12
+ import numpy as np
13
+
14
+ # Function to open a file
15
+ def startfile(fn):
16
+ os.system('open %s' % fn)
17
+
18
+ # Function to create and open a txt file
19
+ def create_and_open_txt(text, filename):
20
+ # Create and write the text to a txt file
21
+ with open(filename, "w") as file:
22
+ file.write(text)
23
+ startfile(filename)
24
+
25
+ # Ask user to record audio
26
+ st.title("Audio to Text Transcription..")
27
+ audio_stream = audio_recorder(pause_threshold=3.0, sample_rate=16_000)
28
+
29
+ # Download the audio stream
30
+
31
+ # Load the base model and transcribe the audio
32
+ model = whisper.load_model("base")
33
+ result = model.transcribe(audio_stream)
34
+ transcribed_text = result["text"]
35
+ print(transcribed_text)
36
+
37
+
38
+
39
+
40
+
41
+
42
+ st.write("Transcription:")
43
+ st.write(transcription)
44
+
45
+
46
+ # Detect the language
47
+ language = detect(transcribed_text)
48
+ st.write(f"Detected language: {language}")
49
+
50
+ # Create and open a txt file with the text
51
+ create_and_open_txt(transcribed_text, f"output_{language}.txt")