File size: 9,400 Bytes
aead8c0 b0c5ac4 a144be7 b0c5ac4 aead8c0 b0c5ac4 a144be7 b0c5ac4 a144be7 b0c5ac4 aead8c0 b0c5ac4 aead8c0 b0c5ac4 aead8c0 b0c5ac4 aead8c0 b0c5ac4 aead8c0 b0c5ac4 aead8c0 b0c5ac4 aead8c0 b0c5ac4 aead8c0 b0c5ac4 a144be7 b0c5ac4 a144be7 b0c5ac4 | 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 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 | from openai import OpenAI
from dotenv import load_dotenv
import MAIAI
# from deep_translator import GoogleTranslator
# import speech_recognition as sr
# import assemblyai as aai
# import pyttsx3
from gtts import gTTS
import gradio as gr
from gradio.themes.base import Base
# Load environment variables
load_dotenv()
language_map = {
"Afrikaans": "af",
"Albanian": "sq",
"Amharic": "am",
"Arabic": "ar",
"Armenian": "hy",
"Azerbaijani": "az",
"Basque": "eu",
"Bengali": "bn",
"Bosnian": "bs",
"Bulgarian": "bg",
"Catalan": "ca",
"Cebuano": "ceb",
"中文" :"zh-CN",
"繁体中文": "zh-TW",
"简体中文": "zh-CN",
"Chinese" : "zh-CN",
"Chinese (Simplified)": "zh-CN",
"Chinese (Traditional)": "zh-TW",
"Corsican": "co",
"Croatian": "hr",
"Czech": "cs",
"Danish": "da",
"Dutch": "nl",
"English": "en",
"Esperanto": "eo",
"Estonian": "et",
"Filipino": "fil",
"Finnish": "fi",
"French": "fr",
"Frisian": "fy",
"Galician": "gl",
"Georgian": "ka",
"German": "de",
"Greek": "el",
"Gujarati": "gu",
"Haitian Creole": "ht",
"Hausa": "ha",
"Hawaiian": "haw",
"Hebrew": "he",
"Hindi": "hi",
"Hmong": "hmn",
"Hungarian": "hu",
"Icelandic": "is",
"Igbo": "ig",
"Indonesian": "id",
"Irish": "ga",
"Italian": "it",
"Japanese": "ja",
"Javanese": "jv",
"Kannada": "kn",
"Kazakh": "kk",
"Khmer": "km",
"Kinyarwanda": "rw",
"Korean": "ko",
"Kurdish": "ku",
"Kyrgyz": "ky",
"Lao": "lo",
"Latin": "la",
"Latvian": "lv",
"Lithuanian": "lt",
"Luxembourgish": "lb",
"Macedonian": "mk",
"Malagasy": "mg",
"Malay": "ms",
"Malayalam": "ml",
"Maltese": "mt",
"Maori": "mi",
"Marathi": "mr",
"Mongolian": "mn",
"Myanmar (Burmese)": "my",
"Nepali": "ne",
"Norwegian": "no",
"Nyanja (Chichewa)": "ny",
"Odia (Oriya)": "or",
"Pashto": "ps",
"Persian": "fa",
"Polish": "pl",
"Portuguese": "pt",
"Punjabi": "pa",
"Romanian": "ro",
"Russian": "ru",
"Samoan": "sm",
"Scots Gaelic": "gd",
"Serbian": "sr",
"Sesotho": "st",
"Shona": "sn",
"Sindhi": "sd",
"Sinhala (Sinhalese)": "si",
"Slovak": "sk",
"Slovenian": "sl",
"Somali": "so",
"Spanish": "es",
"Sundanese": "su",
"Swahili": "sw",
"Swedish": "sv",
"Tajik": "tg",
"Tamil": "ta",
"Tatar": "tt",
"Telugu": "te",
"Thai": "th",
"Turkish": "tr",
"Turkmen": "tk",
"Ukrainian": "uk",
"Urdu": "ur",
"Uyghur": "ug",
"Uzbek": "uz",
"Vietnamese": "vi",
"Welsh": "cy",
"Xhosa": "xh",
"Yiddish": "yi",
"Yoruba": "yo",
"Zulu": "zu"
}
# def pytts(input_text):
# if input_text:
# engine = pyttsx3.init()
# # # Get available voices and print them out
# # voices = engine.getProperty('voices')
# # for index, voice in enumerate(voices):
# # print(f"Voice {index}: {voice.id} - {voice.languages} - {voice.gender} - {voice.name}")
# # # Set voice (change index based on what is available on your system)
# # engine.setProperty('voice', voices[1].id) # Change the index to switch voices
# # # Set speech rate
# # rate = engine.getProperty('rate')
# # engine.setProperty('rate', rate - 50) # Decrease rate; increase to make it faster
# # # Set volume
# # volume = engine.getProperty('volume')
# # engine.setProperty('volume', volume + 0.25) # Increase volume; decrease to lower the volume
# # Speak text
# engine.say(input_text)
# engine.runAndWait()
def gtts(input_text,language='English'):
if input_text:
# Map the user-friendly language name to the IETF tag
lang = language_map.get(language, 'en') # Default to 'en' if language not found
tts = gTTS(text=input_text, lang=lang, slow=False)
audio_file = "output.mp3"
tts.save(audio_file)
return audio_file
# def assembly_speech_to_text(audio_file_path):
# aai.settings.api_key = "e00881b941ff47ea914594c40f6dbc20"
# transcriber = aai.Transcriber()
# transcript = transcriber.transcribe(audio_file_path)
# return transcript.text
# def google_speech_to_text(audio_file_path):
# if audio_file_path:
# recognizer = sr.Recognizer()
# with sr.AudioFile(audio_file_path) as source:
# audio_data = recognizer.record(source)
# try:
# text = recognizer.recognize_google(audio_data)
# return text
# except sr.UnknownValueError:
# return "Google Speech Recognition could not understand audio"
# except sr.RequestError as e:
# return f"Could not request results from Google Speech Recognition service; {e}"
def openai_speech_to_text(audio_file_path):
if audio_file_path:
client = OpenAI()
audio_file= open(audio_file_path, "rb")
transcription = client.audio.transcriptions.create(
model="whisper-1",
response_format="text",
file=audio_file
)
return transcription
def chat(text, history, native_language, language, persona, tone = "Casual", model = "gpt-4o-mini"):
print(tone, native_language, language, persona)
# if audio != None:
# text = speechtotext(audio)
casual = "This is in a casual, internet texting context, use of local slangs is encouraged." if tone == "Casual" else ""
teacher = MAIAI.Agent(model=model, temperature=0.5, role=f"You are a {language} teacher teaching {native_language} speaking student.")
responder = MAIAI.Agent(model=model, temperature=0.5, role=f"""You are {language} speaking {persona}. Respond to the user's text in {language}. Refer to Chat History for context. Keep the conversation going. {casual}""")
translator = MAIAI.Agent(model=model, temperature=0.5, role=f"You are a language translator")
feedback_task = MAIAI.Task(
agent=teacher,
goal=f"""Text: {text}
Point out and translate any non-{language} from the text into {language}.
Correct any linguistic error in the text and give example driven feedback on how to improve the text.
You MUST give your feedback in {native_language}.
{casual}
"""
)
respond_task = MAIAI.Task(
agent=responder,
goal=f"""{text}
Respond to the text above in {language}.
Refer to Chat History for context.
Chat History: {history}"""
)
feedback = feedback_task.execute()
response = respond_task.execute()
translate_task = MAIAI.Task(
agent=translator,
goal=f"translate {response} from {language} to {native_language}"
)
translation = translate_task.execute()
# # Get IETF tags for target and native languages
# native_lang = language_map.get(native_language, 'en')
# # Translate the response to the target language
# translated_response = GoogleTranslator(source='auto', target=native_lang).translate(response)
output = f"""
***Feedback:***
{feedback}
-----------
***{persona}:***
{response}
({translation})
"""
history.append((text, output))
return "", history, response
# Sample Function Call ------------------------------------
# feedback,response = chat("Soy jugando Demonslayer! Y tu?", "English","Spanish","friendly lady",,casual_tone="Casual")
# print(f"""
# Feedback: {feedback}
# Reply: {response}
# """)
# Gradio Custom Chatbot -------------------------------------------------------
with gr.Blocks(fill_height=True, theme=Base()) as demo:
chatbot = gr.Chatbot(
elem_id="chatbot",
bubble_full_width=False,
scale=1,
)
with gr.Row():
chat_input = gr.Textbox(interactive=True, scale=8,
inputs=[gr.components.Audio(sources="microphone", type="filepath", label="Speak or upload audio")],
value=openai_speech_to_text)
submit_button = gr.Button("Submit", scale=1)
with gr.Row():
response = gr.Textbox(visible=False, label="Read out Chat Response")
output_audio = gr.Audio(label="Reply Audio", type="filepath", scale = 9)
read_out_loud = gr.Button("Read Reply", scale = 1)
with gr.Accordion(label = "Settings"):
native_language = gr.components.Dropdown(choices=["English","中文","Spanish"], value="English", allow_custom_value=True, label="I speak")
language = gr.components.Dropdown(choices=["English","中文","Spanish"], value="English", allow_custom_value=True, label="I want to learn")
persona = gr.components.Textbox(value = "LinguAI Chatbot", label="I want to talk to")
tone = gr.components.Dropdown(choices=["Casual","Formal"], value="Casual", label="Tone")
chat_input.submit(chat, [chat_input, chatbot, native_language, language, persona, tone], [chat_input, chatbot, response])
submit_button.click(chat, [chat_input, chatbot, native_language, language, persona, tone], [chat_input, chatbot, response])
read_out_loud.click(gtts,[response,language],output_audio)
demo.launch() |