File size: 6,223 Bytes
b56c7f5 316cb92 b56c7f5 316cb92 b56c7f5 316cb92 b56c7f5 |
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 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
import gradio as gr
#from pyChatGPT import ChatGPT
import os
import requests
api = os.environ.get('API_ENDPOINT')
#session_token = os.environ.get('SessionToken')
#cf_clearance_token = os.environ.get('ClearanceToken')
#cf_bm_token = os.environ.get('cf_bm_token')
whisper = gr.Interface.load(name="spaces/sanchit-gandhi/whisper-large-v2")
def call_api(message):
response = requests.get(f'{api}?q={message}')
if response.status_code == 200:
return str(response.text).split('\n', 2)[2]
else:
return """Sorry, I'm quite busy right now, but please try again later :)"""
def chat_hf(audio, task):
try:
whisper_text = translate(audio, task)
if whisper_text == "ERROR: You have to either use the microphone or upload an audio file":
gpt_response = "MISSING AUDIO: Record your voice by clicking the microphone button, do not forget to stop recording before sending your message ;)"
else:
gpt_response = call_api(whisper_text)
#api = ChatGPT(session_token, cf_clearance_token, cf_bm_token)
#api = ChatGPT(session_token)
#api.refresh_auth() # refresh the authorization token
#if reset_conversation:
#
# api.reset_conversation() # reset the conversation
#resp = api.send_message(whisper_text)
#gpt_response = resp['message']
except:
gpt_response = """Sorry, I'm quite busy right now, but please try again later :)"""
print(f"""
{whisper_text}
ββββ
{gpt_response}
""")
return whisper_text, gpt_response
def translate(audio, task):
if task == "transcribe":
text_result = whisper(audio, None, "transcribe", fn_index=0)
else:
text_result = whisper(audio, None, "translate", fn_index=0)
return text_result
title = """
<div style="text-align: center; max-width: 500px; margin: 0 auto;">
<div
style="
display: inline-flex;
align-items: center;
gap: 0.8rem;
font-size: 1.75rem;
margin-bottom: 10px;
"
>
<h1 style="font-weight: 600; margin-bottom: 7px;">
Whisper-to-chatGPT
</h1>
</div>
<p style="margin-bottom: 10px;font-size: 94%;font-weight: 100;line-height: 1.5em;">
Chat with GPT with your voice in your native language !
<!--<br />If you need a custom session key, see
<a href="https://youtu.be/TdNSj_qgdFk" target="_blank">Bhavesh Baht video for reference</a>
</p>-->
<!--<p style="font-size: 94%">
You can skip the queue by duplicating this space:
<span style="display: flex;align-items: center;justify-content: center;height: 30px;">
<a href="https://huggingface.co/nightfury/whisperAI?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>
</span>
</p>-->
</div>
"""
article = """
<p style="font-size: 0.8em;line-height: 1.2em;">Note: this demo is not able to sustain a conversation from earlier responses.
For more detailed results and dialogue, you should use the official ChatGPT interface.
<br />β
<br/>Also, be aware that audio records from iOS devices will not be decoded as expected by Gradio. For the best experience, record your voice from a computer instead of your smartphone ;)</p>
<div class="footer">
<p>Whisper &
<a href="https://chat.openai.com/chat" target="_blank">chatGPT</a>
by <a href="https://openai.com/" style="text-decoration: underline;" target="_blank">OpenAI</a> -
Gradio Demo by π€ <a href="https://twitter.com" target="_blank">DJ</a>
</p>
</div>
"""
css = '''
#col-container, #col-container-2 {max-width: 510px; margin-left: auto; margin-right: auto;}
a {text-decoration-line: underline; font-weight: 600;}
div#record_btn > .mt-6 {
margin-top: 0!important;
}
div#record_btn > .mt-6 button {
width: 100%;
height: 40px;
}
.footer {
margin-bottom: 45px;
margin-top: 10px;
text-align: center;
border-bottom: 1px solid #e5e5e5;
}
.footer>p {
font-size: .8rem;
display: inline-block;
padding: 0 10px;
transform: translateY(10px);
background: white;
}
.dark .footer {
border-color: #303030;
}
.dark .footer>p {
background: #0b0f19;
}
'''
with gr.Blocks(css=css) as demo:
with gr.Column(elem_id="col-container"):
gr.HTML(title)
with gr.Row():
record_input = gr.Audio(source="microphone",type="filepath", show_label=False,elem_id="record_btn")
task = gr.Radio(choices=["transcribe","translate"], value="transcribe", show_label=False)
with gr.Row():
#reset_conversation = gr.Checkbox(label="Reset conversation?", value=False)
send_btn = gr.Button("Send my request !")
#custom_token = gr.Textbox(label='If it fails, use your own session token', placeholder="your own session token", max_lines=3)
with gr.Column(elem_id="col-container-2"):
audio_translation = gr.Textbox(type="text",label="Whisper transcript")
gpt_response = gr.Textbox(type="text",label="chatGPT response")
gr.HTML(article)
send_btn.click(chat_hf, inputs=[record_input, task], outputs=[audio_translation, gpt_response])
demo.queue(max_size=32, concurrency_count=20).launch(debug=True)
|