File size: 3,603 Bytes
ee531be
 
8fb0be5
ee531be
 
 
 
 
 
d74e638
ee531be
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8fb0be5
ee531be
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8fb0be5
ee531be
 
 
8fb0be5
ee531be
 
 
 
 
8fb0be5
ee531be
 
 
 
 
 
8fb0be5
ee531be
 
8fb0be5
ee531be
 
 
8fb0be5
ee531be
 
 
17e6aa5
ee531be
 
 
 
 
 
 
 
 
 
 
 
 
 
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import requests
import base64
import os
import json
import streamlit as st
from speechlib import Transcriptor

def transcribe_audio(file, log_folder, language, modelSize, ACCESS_TOKEN, voices_folder, quantization):
    transcriptor = Transcriptor(file, log_folder, language, modelSize, ACCESS_TOKEN, voices_folder, quantization)
    return transcriptor.whisper()
    
def transform_transcript(transcript):
    result = []
    for segment in transcript:
        start_time, end_time, text, speaker = segment
        result.append(f"{speaker} ({start_time:.1f} : {end_time:.1f}) : {text}")
    return '\n'.join(result)
    
st.title('Audio Transcription App')

ACCESS_TOKEN = st.secrets["HF_TOKEN"]

uploaded_file = st.file_uploader("Загрузите аудиофайл", type=["mp4", "wav", "m4a"])

if uploaded_file is not None:
    file_extension = uploaded_file.name.split(".")[-1]  # Получаем расширение файла
    temp_file_path = f"temp_file.{file_extension}"  # Создаем временное имя файла с правильным расширением
    
    with open(temp_file_path, "wb") as f:
        f.write(uploaded_file.getbuffer())
        
    log_folder = "logs"
    language = "ru"
    modelSize = os.getenv('WHISPER_MODEL_SIZE')
    voices_folder = ""
    quantization = False
    
    with st.spinner('Транскрибируем...'):
        result = transcribe_audio(temp_file_path, log_folder, language, modelSize, ACCESS_TOKEN, voices_folder, quantization)
        
    st.write("Результат транскрибации:")
    transcript = transform_transcript(result)
    st.text(transcript)
    
    with st.spinner('Резюмируем...'):
        username = st.secrets["GIGA_USERNAME"]
        password = st.secrets["GIGA_SECRET"]
        
        # Получаем строку с базовой авторизацией в формате Base64
        auth_str = f'{username}:{password}'
        auth_bytes = auth_str.encode('utf-8')
        auth_base64 = base64.b64encode(auth_bytes).decode('utf-8')
        url = os.getenv('GIGA_AUTH_URL')
        
        headers = {
            'Authorization': f'Basic {auth_base64}',  # вставляем базовую авторизацию
            'RqUID': os.getenv('GIGA_rquid'),
            'Content-Type': 'application/x-www-form-urlencoded',
            'Accept': 'application/json'
        }
        
        data = {
            'scope': os.getenv('GIGA_SCOPE')
        }
        
        response = requests.post(url, headers=headers, data=data, verify=False)
        access_token = response.json()['access_token']
        print('Got access token')
        
        url_completion = os.getenv('GIGA_COMPLETION_URL')
        
        data_copm = json.dumps({
          "model": os.getenv('GIGA_MODEL'),
          "messages": [
            {
              "role": "user",
              "content": os.getenv('GIGA_BASE_PROMPT') + transcript
            }
          ],
          "stream": False,
          "max_tokens": int(os.getenv('GIGA_MAX_TOKENS')),
        })
        
        headers_comp = {
          'Content-Type': 'application/json',
          'Accept': 'application/json',
          'Authorization': 'Bearer ' + access_token
        }
        
        response = requests.post(url_completion, headers=headers_comp, data=data_copm, verify=False)
        response_data = response.json()
        answer_from_llm = response_data['choices'][0]['message']['content']
        
    st.write("Результат резюмирования:")
    st.text(answer_from_llm)