sarahai commited on
Commit
f03516a
1 Parent(s): 1ac0541

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -0
app.py ADDED
@@ -0,0 +1,42 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import torch
3
+ import torchaudio
4
+ from transformers import Wav2Vec2ForCTC, Wav2Vec2Processor
5
+ import tempfile
6
+
7
+
8
+ model_name = "oyqiz/uzbek_stt"
9
+ processor = Wav2Vec2Processor.from_pretrained(model_name)
10
+ model = Wav2Vec2ForCTC.from_pretrained(model_name)
11
+
12
+
13
+ st.title("Ovozni matnga o'girish")
14
+ st.write("Audio faylingizni yuklang:")
15
+
16
+ # File uploader
17
+ uploaded_file = st.file_uploader("Audio faylingizni tanlang...", type=["wav", "mp3", "ogg"])
18
+
19
+ def transcribe_audio(audio_file):
20
+ waveform, sample_rate = torchaudio.load(audio_file)
21
+ if sample_rate != 16000:
22
+ waveform = torchaudio.transforms.Resample(orig_freq=sample_rate, new_freq=16000)(waveform)
23
+ sample_rate = 16000
24
+ input_values = processor(waveform, sampling_rate=sample_rate, return_tensors="pt").input_values
25
+ with torch.no_grad():
26
+ input_values = input_values.squeeze(1)
27
+ logits = model(input_values).logits
28
+ predicted_ids = torch.argmax(logits, dim=-1)
29
+ transcription = processor.batch_decode(predicted_ids)[0]
30
+ return transcription
31
+
32
+ if uploaded_file is not None:
33
+
34
+ with tempfile.NamedTemporaryFile(delete=False) as tmp_file:
35
+ tmp_file.write(uploaded_file.read())
36
+ tmp_file_path = tmp_file.name
37
+
38
+
39
+ transcription = transcribe_audio(tmp_file_path)
40
+
41
+ st.write("Natija:")
42
+ st.write(transcription)