File size: 1,360 Bytes
f03516a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import streamlit as st
import torch
import torchaudio
from transformers import Wav2Vec2ForCTC, Wav2Vec2Processor
import tempfile


model_name = "oyqiz/uzbek_stt"
processor = Wav2Vec2Processor.from_pretrained(model_name)
model = Wav2Vec2ForCTC.from_pretrained(model_name)


st.title("Ovozni matnga o'girish")
st.write("Audio faylingizni yuklang:")

# File uploader
uploaded_file = st.file_uploader("Audio faylingizni tanlang...", type=["wav", "mp3", "ogg"])

def transcribe_audio(audio_file):
    waveform, sample_rate = torchaudio.load(audio_file)
    if sample_rate != 16000:
        waveform = torchaudio.transforms.Resample(orig_freq=sample_rate, new_freq=16000)(waveform)
        sample_rate = 16000
    input_values = processor(waveform, sampling_rate=sample_rate, return_tensors="pt").input_values
    with torch.no_grad():
        input_values = input_values.squeeze(1)  
        logits = model(input_values).logits
    predicted_ids = torch.argmax(logits, dim=-1)
    transcription = processor.batch_decode(predicted_ids)[0]
    return transcription

if uploaded_file is not None:
    
    with tempfile.NamedTemporaryFile(delete=False) as tmp_file:
        tmp_file.write(uploaded_file.read())
        tmp_file_path = tmp_file.name
    
    
    transcription = transcribe_audio(tmp_file_path)
    
    st.write("Natija:")
    st.write(transcription)