Spaces:
Build error
Build error
Download app.py from nanostuff/audio_bot: direct link, hf CLI and curl.
- Browser
- Download file 7.16 kB
-
https://huggingface.co/spaces/nanostuff/audio_bot/resolve/main/app.py
- Command line
-
hf download hf://spaces/nanostuff/audio_bot/app.py
-
curl -L -o app.py https://huggingface.co/spaces/nanostuff/audio_bot/resolve/main/app.py
7.16 kB
| import gradio as gr | |
| import openai | |
| import os | |
| from gtts import gTTS | |
| # from langdetect import detect | |
| # Initialize the OpenAI API with your key and organization ID | |
| openai.api_key = os.getenv('OPEN_API_KEY') | |
| # all_messages = [] # List to store all conversation messages | |
| all_messages = [{"role": "system", "content": 'You are an AI chatbot. Respond to all input in 25 words or less'}] | |
| # def initialize_session(): | |
| # return [{"role": "system", "content": 'You are an AI chatbot. Respond to all input in 25 words or less'}] | |
| # all_messages = initialize_session() | |
| open("output_audio.mp3", "wb").close() | |
| def transcribe(audio, text): | |
| global all_messages | |
| language = 'en' | |
| transcript = {} | |
| # if audio: | |
| # audio_filename_with_extension = audio + '.wav' | |
| # os.rename(audio, audio_filename_with_extension) | |
| # audio_file = open(audio_filename_with_extension, "rb") | |
| # # Transcribe the audio file using the whisper-1 model | |
| # transcript = openai.audio.transcriptions.create(model="whisper-1", file=audio_file) | |
| # print("Transcription:", transcript) | |
| # # user_input = transcript("text") if isinstance(transcript, dict) else "" | |
| # # user_input = transcript.text if isinstance(transcript, dict) else "" | |
| # user_input = transcript.get("text", "") if isinstance(transcript, dict) else "" | |
| # user_input = transcript.text | |
| # print("Transcription:", user_input) | |
| # else : | |
| # user_input = text | |
| if audio: | |
| audio_filename_with_extension = audio + '.wav' | |
| os.rename(audio, audio_filename_with_extension) | |
| audio_file = open(audio_filename_with_extension, "rb") | |
| # Transcribe the audio file using the whisper-1 model | |
| transcript = openai.audio.transcriptions.create(model="whisper-1", file=audio_file) | |
| print("Transcription:", transcript) | |
| # user_input = transcript("text") if isinstance(transcript, dict) else "" | |
| # user_input = transcript.text if isinstance(transcript, dict) else "" | |
| user_input = transcript.get("text", "") if isinstance(transcript, dict) else "" | |
| user_input = transcript.text | |
| print("Transcription:", user_input) | |
| else: | |
| user_input = text | |
| # if not all_messages or (len(all_messages) == 1 and all_messages[0]["role"] == "system"): | |
| # all_messages = initialize_session() | |
| conversation_tokens = sum(len(message['content'].split()) for message in all_messages) | |
| # Limit the conversation tokens to the model's maximum limit (4096 - a safety margin) | |
| max_conversation_tokens = 4000 | |
| while conversation_tokens > max_conversation_tokens: | |
| oldest_message_tokens = len(all_messages[0]['content'].split()) | |
| if conversation_tokens - oldest_message_tokens > max_conversation_tokens: | |
| all_messages.pop(0) | |
| conversation_tokens -= oldest_message_tokens | |
| else: | |
| break | |
| # all_messages.append = [{"role": "system", "content": 'You are an AI chatbot. Respond to all input in 25 words or less'}] | |
| all_messages.append({"role": "user", "content": user_input}) | |
| # Generate a chat response using the gpt-3.5-turbo model | |
| response = openai.chat.completions.create(model="gpt-3.5-turbo", messages=all_messages) | |
| # Get the generated chat response from the model | |
| system_message = response.choices[0].message.content | |
| # all_messages.append(system_message.to_dict()) | |
| all_messages.append({"role": "assistant", "content": system_message}) | |
| chatbot_responses = [] # Initialize the list outside the loop | |
| # user_input = "" | |
| for message in all_messages: | |
| print(f"Message: {message}") | |
| # print(f"role: {message['role']}") | |
| if 'role' in message and 'content' in message: | |
| # print(f"role: {message['role']}") | |
| if message['role'] == 'user': | |
| user_input = message['content'] | |
| print(f"ui: {user_input}") | |
| elif message['role'] == 'assistant': | |
| chatbot_responses.append([user_input, message['content']]) | |
| # chatbot_responses.append(message['content']) | |
| # print(f"hdjdkdkksdk: {message['content']}") | |
| print(f"Assistant response appended: {message['content']}") | |
| print(f"cbr: {chatbot_responses}") | |
| # For English input, use English language voice | |
| tts = gTTS(text=chatbot_responses[-1][1], lang='en', slow=False) | |
| tts.save("output_audio.mp3") | |
| # tts = gTTS(text=chatbot_responses[-1][1], lang=language, slow=False) | |
| # detected_language = detect(user_input) | |
| # if detected_language == 'hi': | |
| # # For Hindi input, use the Hindi language voice | |
| # tts = gTTS(text=chatbot_responses[-1][1], lang='hi', slow=False, lang_check=False) | |
| # tts.save("output_audio.mp3") | |
| # else: | |
| # # # For English input, use English language voice | |
| # tts = gTTS(text=chatbot_responses[-1][1], lang='en', slow=False) | |
| # tts.save("output_audio.mp3") | |
| return [chatbot_responses[-1]], "output_audio.mp3" # Wrap the chatbot response in a list | |
| # return chatbot_responses, "output_audio.mp3" | |
| # out1 = gr.Audio("output_audio.mp3") | |
| css_styles = """ | |
| footer { visibility:hidden; } | |
| .audio_container { height: 100px; } | |
| .svelte-1e89no8 { padding: 10px; } | |
| .input_text_container { height: 100px; } | |
| @media only screen and (max-width: 450px) { .input_text_container { display: none; } } | |
| """ | |
| with gr.Blocks(theme=gr.themes.Monochrome(font=[gr.themes.GoogleFont("Inter"), "sans-serif"], | |
| spacing_size=gr.themes.sizes.spacing_md, | |
| radius_size=gr.themes.sizes.radius_md), | |
| css=css_styles, | |
| title="NanoAI Voice Assistant") as demo: | |
| # chatbot = gr.Chatbot().style(height=450) # Pass the all_messages list to the Chatbot widget | |
| chatbot = gr.Chatbot(clear_on_refresh=True).style(height=450) | |
| out1 = gr.Audio("output_audio.mp3", autoplay=True, label="Voice Response ") | |
| with gr.Row(): | |
| with gr.Column(scale=0.35): | |
| audio_input = gr.Audio(source="microphone", type="filepath", label="Record your question (Voice)", elem_classes="audio_container") | |
| with gr.Column(scale=0.65): | |
| text_input = gr.Textbox(label="Or Type your question here (Chat)", | |
| placeholder="What would you like to ask the bot...", | |
| elem_classes="input_text_container") | |
| with gr.Row(): | |
| with gr.Column(scale=0.50): | |
| text_button = gr.Button("Send a Message") | |
| with gr.Column(scale=0.50): | |
| reset_button = gr.Button("Clear ❌") | |
| text_button.click(transcribe, [audio_input, text_input], [chatbot, out1]) | |
| text_button.click(lambda x: gr.update(value=''), [], [text_input]) | |
| text_button.click(lambda: None, None, audio_input), | |
| text_button.click(lambda: None, None, out1), | |
| reset_button.click(lambda: None, None, chatbot), | |
| reset_button.click(lambda: None, None, out1) | |
| # audio_input = gr.Audio(source="microphone", type="filepath") | |
| demo.launch() | |