MufinApps commited on
Commit
2cc66b4
·
1 Parent(s): 4865c01

Updated version

Browse files
Files changed (2) hide show
  1. app.py +214 -0
  2. requirements.txt +72 -0
app.py ADDED
@@ -0,0 +1,214 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from openai import OpenAI
3
+ import threading as th
4
+ import os
5
+
6
+ os.environ['OPENAI_API_KEY']="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"
7
+ client = OpenAI(api_key=os.environ['OPENAI_API_KEY'])
8
+
9
+
10
+ def translateoutput(text,language):
11
+ completion = client.chat.completions.create(
12
+ model="gpt-3.5-turbo",
13
+ messages=[
14
+ {"role": "system", "content": f"You will be provided with a sentence in English, and your task is to translate it into {language}."},
15
+ {"role": "user", "content":text}
16
+ ]
17
+ )
18
+ return completion.choices[0].message.content
19
+
20
+
21
+ # Initialize a global variable to hold previous output
22
+ language_info={
23
+ 'Afrikaans': 'af',
24
+ 'English': 'en',
25
+ 'Arabic': 'ar',
26
+ 'Armenian': 'hy',
27
+ 'Azerbaijani': 'az',
28
+ 'Belarusian': 'be',
29
+ 'Bosnian': 'bs',
30
+ 'Bulgarian': 'bg',
31
+ 'Catalan': 'ca',
32
+ 'Chinese': 'zh',
33
+ 'Croatian': 'hr',
34
+ 'Czech': 'cs',
35
+ 'Danish': 'da',
36
+ 'Dutch': 'nl',
37
+ 'English': 'en',
38
+ 'Estonian': 'et',
39
+ 'Finnish': 'fi',
40
+ 'French': 'fr',
41
+ 'Galician': 'gl',
42
+ 'German': 'de',
43
+ 'Greek': 'el',
44
+ 'Hebrew': 'he',
45
+ 'Hindi': 'hi',
46
+ 'Hungarian': 'hu',
47
+ 'Icelandic': 'is',
48
+ 'Indonesian': 'id',
49
+ 'Italian': 'it',
50
+ 'Japanese': 'ja',
51
+ 'Kannada': 'kn',
52
+ 'Kazakh': 'kk',
53
+ 'Korean': 'ko',
54
+ 'Latvian': 'lv',
55
+ 'Lithuanian': 'lt',
56
+ 'Macedonian': 'mk',
57
+ 'Malay': 'ms',
58
+ 'Marathi': 'mr',
59
+ 'Maori': 'mi',
60
+ 'Nepali': 'ne',
61
+ 'Norwegian': 'no',
62
+ 'Persian': 'fa',
63
+ 'Polish': 'pl',
64
+ 'Portuguese': 'pt',
65
+ 'Romanian': 'ro',
66
+ 'Russian': 'ru',
67
+ 'Serbian': 'sr',
68
+ 'Slovak': 'sk',
69
+ 'Slovenian': 'sl',
70
+ 'Spanish': 'es',
71
+ 'Swahili': 'sw',
72
+ 'Swedish': 'sv',
73
+ 'Tagalog': 'tl',
74
+ 'Tamil': 'ta',
75
+ 'Thai': 'th',
76
+ 'Turkish': 'tr',
77
+ 'Ukrainian': 'uk',
78
+ 'Urdu': 'ur',
79
+ 'Vietnamese': 'vi',
80
+ 'Welsh': 'cy',
81
+ 'Other': 'Other'
82
+ }
83
+
84
+ def translate(audio_file,lan):
85
+ message=""
86
+ au=open(audio_file, 'rb')
87
+ result = client.audio.translations.create(file=au,model="whisper-1")
88
+ text=result.text
89
+ # translation_text += text
90
+ if lan=="English" or lan=="Other" or text=="":
91
+ message=text
92
+ else:
93
+
94
+ text=translateoutput(text,lan)
95
+ message=text
96
+ th.current_thread().return_value=message
97
+
98
+
99
+
100
+ def transcription(audio_file,input_lang):
101
+ global language_info
102
+ au=open(audio_file, 'rb')
103
+ if input_lang=="Other":
104
+ result = client.audio.transcriptions.create(file=au,model="whisper-1")
105
+ th.current_thread().return_value=result.text
106
+ else:
107
+ result = client.audio.transcriptions.create(file=au,model="whisper-1",language=language_info[input_lang])
108
+ th.current_thread().return_value=result.text
109
+
110
+
111
+
112
+ def func(audio_file,input_lang,lan,state="",state1=""):
113
+
114
+ t1 = th.Thread(target=translate, args=(audio_file,lan,))
115
+ t2 = th.Thread(target=transcription, args=(audio_file,input_lang,))
116
+ t1.start()
117
+ t2.start()
118
+ t1.join()
119
+ t2.join()
120
+ translation_text=t1.return_value
121
+ transcribe_text=t2.return_value
122
+ state+=transcribe_text+" "
123
+ state1+=translation_text+" "
124
+ state=state.replace(".","\n")
125
+ state1=state1.replace(".","\n")
126
+
127
+
128
+
129
+
130
+ return state,state1
131
+
132
+
133
+ def gpt_api(text):
134
+
135
+ if text=="":
136
+ return ""
137
+ if len(text)>2000:
138
+ text=text[-2000:]
139
+ completion = client.chat.completions.create(
140
+ model="gpt-3.5-turbo",
141
+ messages=[
142
+ {"role": "system", "content": "your task is to make a concise summery of text in the same language used in given text"},
143
+ {"role": "user", "content":text}
144
+ ]
145
+ )
146
+
147
+ message=completion.choices[0].message.content
148
+ th.current_thread().return_value=message
149
+
150
+
151
+ def make_summery(text,text1):
152
+ t1=th.Thread(target=gpt_api, args=(text,))
153
+ t2=th.Thread(target=gpt_api, args=(text,))
154
+ t1.start()
155
+ t2.start()
156
+ t1.join()
157
+ t2.join()
158
+ return t1.return_value,t2.return_value
159
+
160
+
161
+
162
+
163
+
164
+
165
+ def clear_output_data():
166
+
167
+
168
+ return "","","",""
169
+
170
+
171
+ css='''#clear {background-color: ##919cbf;border-radius:5%;}
172
+ #clear:hover {background-color: #ff0000;transition: 0.5s;}
173
+ #summery {background-color: ##919cbf;border-radius:5%;}
174
+ #summery:hover {background-color:#2dcc9a ;transition: 0.5s;}
175
+ # div {background-image:url("https://images.unsplash.com/photo-1506259091721-347e791bab0f?auto=format&fit=crop&q=80&w=1470&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D");
176
+ # background-size: cover;
177
+ # background-position: center;
178
+ # background-repeat: no-repeat;
179
+ # background-attachment: fixed;
180
+ # color=white;!imoportant;
181
+ }
182
+ '''
183
+
184
+ with gr.Blocks(theme=gr.themes.Soft(),css=css) as app:
185
+
186
+ gr.Markdown("## Mufin Real-Time Audio Transcription And Translation",elem_id="heading")
187
+ gr.Markdown("### say any language we are here to translate it for team!!",elem_classes="heading")
188
+
189
+ with gr.Row():
190
+ mic = gr.Audio(sources="microphone",streaming=True,type='filepath',label='Speak')
191
+
192
+ input_lan=gr.Dropdown(choices=language_info.keys(),label="Choose Input Language please",value="English",interactive=True)
193
+ lan=gr.Dropdown(choices=language_info.keys(),label="Choose a language for translation",value="Korean",interactive=True)
194
+ summery=gr.Button(value="Summery",variant="secondary",size="small",elem_id="summery")
195
+ clear_output = gr.ClearButton(value="Clear Output",variant="stop",size="small",elem_id="clear")
196
+ with gr.Row():
197
+ with gr.Column():
198
+
199
+ text=gr.Textbox(interactive=False,label="Transcription",lines=10,max_lines=10)
200
+
201
+ with gr.Column():
202
+ text1 = gr.Textbox(interactive=False,label="Translation",lines=10,max_lines=10,)
203
+ st=mic.stream(func, [mic,input_lan,lan,text,text1], [text,text1],show_progress=False)
204
+ with gr.Row():
205
+ sumer_ts=gr.Textbox(label="Summery of Transcription",interactive=False,lines=4,max_lines=4)
206
+ sumer_tr=gr.Textbox(label="Summery of Translation",interactive=False,lines=4,max_lines=4)
207
+
208
+ # pass
209
+ summery.click(make_summery,[text,text1],[sumer_ts,sumer_tr],cancels=[st],queue=False)
210
+ clear_output.click(clear_output_data,[],[text,text1,sumer_tr,sumer_ts],cancels=[st],queue=False)
211
+ # gr.update(visible=True)
212
+
213
+ app.queue()
214
+ app.launch()
requirements.txt ADDED
@@ -0,0 +1,72 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ aiofiles==23.2.1
2
+ aiohttp==3.8.6
3
+ aiosignal==1.3.1
4
+ altair==5.1.2
5
+ annotated-types==0.6.0
6
+ anyio==3.7.1
7
+ async-timeout==4.0.3
8
+ attrs==23.1.0
9
+ certifi==2023.7.22
10
+ charset-normalizer==3.3.2
11
+ click==8.1.7
12
+ colorama==0.4.6
13
+ contourpy==1.2.0
14
+ cycler==0.12.1
15
+ distro==1.8.0
16
+ fastapi==0.104.1
17
+ ffmpy==0.3.1
18
+ filelock==3.13.1
19
+ fonttools==4.44.0
20
+ frozenlist==1.4.0
21
+ fsspec==2023.10.0
22
+ gradio==4.1.2
23
+ gradio_client==0.7.0
24
+ h11==0.14.0
25
+ httpcore==1.0.1
26
+ httpx==0.25.1
27
+ huggingface-hub==0.18.0
28
+ idna==3.4
29
+ importlib-resources==6.1.1
30
+ Jinja2==3.1.2
31
+ jsonschema==4.19.2
32
+ jsonschema-specifications==2023.7.1
33
+ kiwisolver==1.4.5
34
+ markdown-it-py==3.0.0
35
+ MarkupSafe==2.1.3
36
+ matplotlib==3.8.1
37
+ mdurl==0.1.2
38
+ multidict==6.0.4
39
+ numpy==1.26.1
40
+ openai==1.2.0
41
+ orjson==3.9.10
42
+ packaging==23.2
43
+ pandas==2.1.2
44
+ Pillow==10.1.0
45
+ pydantic==2.4.2
46
+ pydantic_core==2.10.1
47
+ pydub==0.25.1
48
+ Pygments==2.16.1
49
+ pyparsing==3.1.1
50
+ python-dateutil==2.8.2
51
+ python-multipart==0.0.6
52
+ pytz==2023.3.post1
53
+ PyYAML==6.0.1
54
+ referencing==0.30.2
55
+ requests==2.31.0
56
+ rich==13.6.0
57
+ rpds-py==0.12.0
58
+ semantic-version==2.10.0
59
+ shellingham==1.5.4
60
+ six==1.16.0
61
+ sniffio==1.3.0
62
+ starlette==0.27.0
63
+ tomlkit==0.12.0
64
+ toolz==0.12.0
65
+ tqdm==4.66.1
66
+ typer==0.9.0
67
+ typing_extensions==4.8.0
68
+ tzdata==2023.3
69
+ urllib3==2.0.7
70
+ uvicorn==0.24.0.post1
71
+ websockets==11.0.3
72
+ yarl==1.9.2