import gradio as gr from PIL import Image import os import openai #api_key = os.environ.get('api_key') from share_btn import community_icon_html, loading_icon_html, share_js token = os.environ.get('HF_TOKEN') whisper = gr.Interface.load(name="spaces/sanchit-gandhi/whisper-large-v2") tts = gr.Interface.load(name="spaces/Flux9665/IMS-Toucan") talking_face = gr.Blocks.load(name="spaces/fffiloni/one-shot-talking-face", api_key=token) def infer(audio, openai_api_key): whisper_result = whisper(audio, None, "translate", fn_index=0) gpt_response = try_api(whisper_result, openai_api_key) audio_response = tts(gpt_response[0], "English Text", "English Accent", "English Speaker's Voice", fn_index=0) portrait_link = talking_face("wise_woman_portrait.png", audio_response, fn_index=0) return gr.Textbox.update(value=whisper_result, visible=True), portrait_link, gr.Textbox.update(value=gpt_response[1], visible=True), gr.Group.update(visible=True), gr.Button.update(visible=True) def try_api(message, openai_api_key): try: response = call_api(message, openai_api_key) return response, "no error" except openai.error.Timeout as e: #Handle timeout error, e.g. retry or log print(f"OpenAI API request timed out: {e}") return "oups", f"OpenAI API request timed out:
{e}
" except openai.error.APIError as e: #Handle API error, e.g. retry or log print(f"OpenAI API returned an API Error: {e}") return "oups", f"OpenAI API returned an API Error:
{e}
" except openai.error.APIConnectionError as e: #Handle connection error, e.g. check network or log print(f"OpenAI API request failed to connect: {e}") return "oups", f"OpenAI API request failed to connect:
{e}
" except openai.error.InvalidRequestError as e: #Handle invalid request error, e.g. validate parameters or log print(f"OpenAI API request was invalid: {e}") return "oups", f"OpenAI API request was invalid:
{e}
" except openai.error.AuthenticationError as e: #Handle authentication error, e.g. check credentials or log print(f"OpenAI API request was not authorized: {e}") return "oups", f"OpenAI API request was not authorized:
{e}
" except openai.error.PermissionError as e: #Handle permission error, e.g. check scope or log print(f"OpenAI API request was not permitted: {e}") return "oups", f"OpenAI API request was not permitted:
{e}
" except openai.error.RateLimitError as e: #Handle rate limit error, e.g. wait or log print(f"OpenAI API request exceeded rate limit: {e}") return "oups", f"OpenAI API request exceeded rate limit:
{e}
" def call_api(message, openai_api_key): print("starting open ai") augmented_prompt = message + prevent_code_gen openai.api_key = openai_api_key response = openai.Completion.create( model="text-davinci-003", prompt=augmented_prompt, temperature=0.5, max_tokens=2048, top_p=1, frequency_penalty=0, presence_penalty=0.6 ) print(response) #return str(response.choices[0].text).split("\n",2)[2] return str(response.choices[0].text) def clean_components(): return gr.Audio.update(value=None), gr.HTML.update(visible=False), gr.Textbox.update(visible=False), gr.Video.update(value=None), gr.Group.update(visible=False), gr.Button.update(visible=False) title = """

GPT Talking Portrait

Use Whisper to ask, alive portrait responds !

""" article = """

You may also like:

""" prevent_code_gen = """ If i am asking for code generation, do not provide me with code. Instead, give me a summury of good hints about how i could do what i asked, but shortly. If i am not asking for code generation, do as usual. """ with gr.Blocks(css="style.css") as demo: with gr.Column(elem_id="col-container"): gr.HTML(title) gpt_response = gr.Video(label="Talking Portrait response", elem_id="video_out") whisper_tr = gr.Textbox(label="whisper english translation", elem_id="text_inp", visible=False) with gr.Row(elem_id="secondary-buttons"): clean_btn = gr.Button(value="Clean", elem_id="clean-btn", visible=False) with gr.Group(elem_id="share-btn-container", visible=False) as share_group: community_icon = gr.HTML(community_icon_html) loading_icon = gr.HTML(loading_icon_html) share_button = gr.Button("Share to community", elem_id="share-btn") error_handler = gr.HTML(visible=False, show_label=False, elem_id="error_handler") with gr.Column(elem_id="col-container-2"): with gr.Column(): with gr.Row(): record_input = gr.Audio(source="microphone",type="filepath", label="Audio input", show_label=True, elem_id="record_btn") openai_api_key = gr.Textbox(max_lines=1, type="password", label="🔐 Your OpenAI API Key", placeholder="sk-123abc...") send_btn = gr.Button("Send my request !") gr.HTML(article) clean_btn.click(clean_components, scroll_to_output=True, inputs=[], outputs=[record_input, error_handler, whisper_tr, gpt_response, share_group, clean_btn]) send_btn.click(infer, inputs=[record_input, openai_api_key], outputs=[whisper_tr, gpt_response, error_handler, share_group, clean_btn]) share_button.click(None, [], [], _js=share_js) demo.queue(max_size=32, concurrency_count=20).launch(debug=True)