mheed commited on
Commit
f91058b
β€’
1 Parent(s): f7dcf6d

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +142 -0
app.py ADDED
@@ -0,0 +1,142 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from pyChatGPT import ChatGPT
2
+ import os
3
+ session_token = os.environ.get('SessionToken')
4
+
5
+ import whisper
6
+ whisper_model = whisper.load_model("small")
7
+
8
+
9
+ def chat_hf(audio, custom_token):
10
+ try:
11
+ whisper_text = translate(audio)
12
+ api = ChatGPT(session_token)
13
+ resp = api.send_message(whisper_text)
14
+
15
+
16
+ api.refresh_auth() # refresh the authorization token
17
+ api.reset_conversation() # reset the conversation
18
+ gpt_response = resp['message']
19
+
20
+ except:
21
+ whisper_text = translate(audio)
22
+ api = ChatGPT(custom_token)
23
+ resp = api.send_message(whisper_text)
24
+
25
+
26
+ api.refresh_auth() # refresh the authorization token
27
+ api.reset_conversation() # reset the conversation
28
+ gpt_response = resp['message']
29
+
30
+ return whisper_text, gpt_response
31
+
32
+
33
+ def translate(audio):
34
+ print("""
35
+ β€”
36
+ Sending audio to Whisper ...
37
+ β€”
38
+ """)
39
+
40
+ audio = whisper.load_audio(audio)
41
+ audio = whisper.pad_or_trim(audio)
42
+
43
+ mel = whisper.log_mel_spectrogram(audio).to(whisper_model.device)
44
+
45
+ _, probs = whisper_model.detect_language(mel)
46
+
47
+ transcript_options = whisper.DecodingOptions(task="transcribe", fp16 = False)
48
+ #translate_options = whisper.DecodingOptions(task="translate", fp16 = False)
49
+
50
+ transcription = whisper.decode(whisper_model, mel, transcript_options)
51
+ #translation = whisper.decode(whisper_model, mel, translate_options)
52
+
53
+ print("language spoken: " + transcription.language)
54
+ print("transcript: " + transcription.text)
55
+ print("β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”")
56
+ #print("translated: " + translation.text)
57
+
58
+ return transcription.text
59
+
60
+ title = """
61
+ <div style="text-align: center; max-width: 500px; margin: 0 auto;">
62
+ <div
63
+ style="
64
+ display: inline-flex;
65
+ align-items: center;
66
+ gap: 0.8rem;
67
+ font-size: 1.75rem;
68
+ margin-bottom: 10px;
69
+ "
70
+ >
71
+ <h1 style="font-weight: 600; margin-bottom: 7px;">
72
+ Whisper to chatGPT
73
+ </h1>
74
+ </div>
75
+ <p style="margin-bottom: 10px;font-size: 94%;font-weight: 100;line-height: 1.5em;">
76
+ Chat with GPT with your voice in your native language !
77
+ <br />If it fails enter custom session key see video for reference refer
78
+ <a href="https://youtu.be/TdNSj_qgdFk" target="_blank">Bhavesh Baht video</a>
79
+ </p>
80
+ <p style="font-size: 94%">
81
+ You can skip the queue by duplicating this space:
82
+ <span style="display: flex;align-items: center;justify-content: center;height: 30px;">
83
+ <a href="https://huggingface.co/fffiloni/whisper-to-chatGPT?duplicate=true"><img src="https://img.shields.io/badge/-Duplicate%20Space-blue?labelColor=white&style=flat&logo=data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAAXNSR0IArs4c6QAAAP5JREFUOE+lk7FqAkEURY+ltunEgFXS2sZGIbXfEPdLlnxJyDdYB62sbbUKpLbVNhyYFzbrrA74YJlh9r079973psed0cvUD4A+4HoCjsA85X0Dfn/RBLBgBDxnQPfAEJgBY+A9gALA4tcbamSzS4xq4FOQAJgCDwV2CPKV8tZAJcAjMMkUe1vX+U+SMhfAJEHasQIWmXNN3abzDwHUrgcRGmYcgKe0bxrblHEB4E/pndMazNpSZGcsZdBlYJcEL9Afo75molJyM2FxmPgmgPqlWNLGfwZGG6UiyEvLzHYDmoPkDDiNm9JR9uboiONcBXrpY1qmgs21x1QwyZcpvxt9NS09PlsPAAAAAElFTkSuQmCC&logoWidth=14" alt="Duplicate Space"></a>
84
+ </span>
85
+ </p>
86
+ </div>
87
+ """
88
+
89
+ article = """
90
+ <div class="footer">
91
+ <p><a href="https://chat.openai.com/chat" target="_blank">chatGPT</a>
92
+ by <a href="https://openai.com/" style="text-decoration: underline;" target="_blank">OpenAI</a> -
93
+ Gradio Demo by πŸ€— <a href="https://twitter.com/fffiloni" target="_blank">Sylvain Filoni</a>
94
+ </p>
95
+ </div>
96
+ """
97
+
98
+ css = '''
99
+ #col-container {max-width: 700px; margin-left: auto; margin-right: auto;}
100
+ a {text-decoration-line: underline; font-weight: 600;}
101
+ .footer {
102
+ margin-bottom: 45px;
103
+ margin-top: 35px;
104
+ text-align: center;
105
+ border-bottom: 1px solid #e5e5e5;
106
+ }
107
+ .footer>p {
108
+ font-size: .8rem;
109
+ display: inline-block;
110
+ padding: 0 10px;
111
+ transform: translateY(10px);
112
+ background: white;
113
+ }
114
+ .dark .footer {
115
+ border-color: #303030;
116
+ }
117
+ .dark .footer>p {
118
+ background: #0b0f19;
119
+ }
120
+ '''
121
+
122
+ import gradio as gr
123
+
124
+ with gr.Blocks(css=css) as demo:
125
+
126
+ with gr.Column(elem_id="col-container"):
127
+
128
+ gr.HTML(title)
129
+
130
+ with gr.Row():
131
+ record_input = gr.Audio(source="microphone",type="filepath", show_label=False)
132
+ send_btn = gr.Button("Send my message !")
133
+ custom_token = gr.Textbox(label='If it fails, use your own session token', placeholder="your own session token")
134
+ with gr.Column():
135
+ audio_translation = gr.Textbox(type="text",label="Whisper translation")
136
+ gpt_response = gr.Textbox(type="text",label="chatGPT response")
137
+
138
+ gr.HTML(article)
139
+
140
+ send_btn.click(chat_hf, inputs=[record_input, custom_token], outputs=[audio_translation, gpt_response])
141
+
142
+ demo.queue(max_size=32, concurrency_count=20).launch(debug=True)