File size: 3,900 Bytes
2cc66b4
81ce2b1
2cc66b4
cfbbaa5
81ce2b1
1d13d55
2135ecb
9977a81
11b7b41
2cc66b4
81ce2b1
 
11b7b41
1d13d55
 
2cc66b4
 
 
11b7b41
1d13d55
8f5c3df
81ce2b1
2cc66b4
 
 
 
 
cfbbaa5
81ce2b1
 
2cc66b4
8f5c3df
 
 
2cc66b4
 
 
81ce2b1
 
 
 
 
 
 
2cc66b4
 
 
 
 
 
 
 
 
8f5c3df
81ce2b1
2cc66b4
81ce2b1
 
 
2cc66b4
 
81ce2b1
 
 
 
2cc66b4
 
 
8f5c3df
 
 
2cc66b4
 
 
 
 
 
 
 
 
 
 
 
 
 
81ce2b1
 
 
 
2cc66b4
81ce2b1
2cc66b4
 
 
 
 
 
 
11b7b41
81ce2b1
11b7b41
2cc66b4
 
 
 
 
 
 
 
 
605cec3
2cc66b4
 
 
 
 
8f5c3df
2cc66b4
81ce2b1
2cc66b4
 
 
 
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import gradio as gr
from openai import OpenAI
import threading as th
import os
import json
from transcription_service import transcribe_speech_local,translation_service



def translateoutput(text,language):
    if text=="" or text is None:
        return ""
    language=language_info[language]
    return translation_service(text,language)
    
    

# Initialize a global variable to hold previous output
language_info=json.load(open("languages_info.json","r"))['wispher_language']


   

    

def transcription(audio_file,input_lang):
    global language_info

    return transcribe_speech_local(audio_file,language_info[input_lang])
    





def func(audio_file,input_lang,lan,state="",state1=""):
    
    text_transcription=transcription(audio_file,input_lang)
    text_translation=translateoutput(text_transcription,lan)
    if text_transcription is None:
        text_transcription=""
        
    state+=str(text_transcription)+" "
    state1+=str(text_translation)+" "
    state=state.replace(".","\n")
    state1=state1.replace(".","\n")
    
    

    
    return state,state1


def gpt_api(text,language):
    if text=="" or text is None:
        return ""
    client = OpenAI()

    response = client.chat.completions.create(
    model="gpt-3.5-turbo",
    messages=[
        {"role": "system", "content": f"You will be provided with a text data, and your task is to make very concise summery it into {language}."},
        {"role": "user", "content": text},
    ])
    message=response.choices[0].message.content
    th.current_thread().return_value=message


def make_summery(text,text1,input_lan,output_lan):
    t1=th.Thread(target=gpt_api, args=(text,input_lan,))
    t2=th.Thread(target=gpt_api, args=(text1,output_lan,))
    t1.start()
    t2.start()
    t1.join()
    t2.join()
    return t1.return_value,t2.return_value
  

    



def clear_output_data():
    return "","","",""

def switch(input_lan,lan):
    return lan,input_lan



with gr.Blocks(theme=gr.themes.Soft(),css="css.txt") as app:
    
    gr.Markdown("## Mufin Real-Time Audio Transcription And Translation",elem_id="heading")
    gr.Markdown("### say any language we are here to translate it for team!!",elem_classes="heading")
    
    with gr.Row():
            mic = gr.Audio(sources="microphone",streaming=True,type='filepath',label='Speak')
            
            input_lan=gr.Dropdown(choices=language_info.keys(),label="Choose Input Language please",value="English",interactive=True)
            switchbutton=gr.Button(value='\u21C6',variant="secondary",elem_id="switchbtn")
            lan=gr.Dropdown(choices=language_info.keys(),label="Choose a language for translation",value="Korean",interactive=True)
            summery=gr.Button(value="Summery",variant="secondary",size="small",elem_id="summery")
            clear_output = gr.ClearButton(value="Clear Output",variant="stop",size="small",elem_id="clear")
    with gr.Row():
        with gr.Column():
        
            text=gr.Textbox(interactive=False,label="Transcription",lines=10,max_lines=10)
            
        with gr.Column():   
            text1 = gr.Textbox(interactive=False,label="Translation",lines=10,max_lines=10,)
            st=mic.stream(func, [mic,input_lan,lan,text,text1], [text,text1],show_progress=False,)
    with gr.Row():
        sumer_ts=gr.Textbox(label="Summery of Transcription",interactive=False,lines=4,max_lines=4)
        sumer_tr=gr.Textbox(label="Summery of Translation",interactive=False,lines=4,max_lines=4)
        
        # pass
        summery.click(make_summery,[text,text1,input_lan,lan],[sumer_ts,sumer_tr],cancels=[st],queue=False)
        clear_output.click(clear_output_data,[],[text,text1,sumer_tr,sumer_ts],cancels=[st],queue=False)
        switchbutton.click(switch,[input_lan,lan],[input_lan,lan],cancels=[st],queue=False)
        # gr.update(visible=True)

app.queue()
app.launch()