Spaces:
Build error
Build error
File size: 7,159 Bytes
6ada4db 0b44946 7a68ac2 8424213 e5662a0 c497020 549a3e5 e5662a0 7e46608 bb242b8 2bb53ee bb242b8 2bb53ee bb242b8 e5662a0 7a68ac2 e5662a0 026617c 0b44946 026617c 41fc166 026617c c497020 fbef3c3 41fc166 239e9de bb242b8 315c6c1 0b44946 8f3461e 0b44946 a9193e2 0b44946 8f3461e c497020 026617c 8f3461e c497020 026617c de6c220 580c975 8f3461e e5e07f8 4b7b809 7736b91 7e46608 10ce5d4 fbd4f88 87f3d05 fbd4f88 03197fd 87f3d05 fbd4f88 c3d2200 fbc2041 5429fac 794c36d a2b15c1 7be963e 177b631 0b44946 8424213 fbc2041 c497020 b6b87c5 c497020 e5662a0 7a68ac2 c497020 bb242b8 c497020 0b44946 026617c 0b44946 c497020 0b44946 74af6f5 0b44946 c497020 0b44946 c497020 8424213 8f3461e a4b1544 | 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 160 161 162 163 164 165 166 167 |
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()
|