MikeMai commited on
Commit
b0c5ac4
·
verified ·
1 Parent(s): 4b3be62

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +249 -22
app.py CHANGED
@@ -1,26 +1,219 @@
1
  from openai import OpenAI
2
  from dotenv import load_dotenv
3
- import gradio as gr
4
  import MAIAI
5
 
 
 
 
 
 
 
 
 
 
 
 
6
  # Load environment variables
7
  load_dotenv()
8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
  def chat(text, history, native_language, language, persona, tone = "Casual", model = "gpt-4o-mini"):
10
 
11
  print(tone, native_language, language, persona)
12
 
 
 
 
13
  casual = "This is in a casual, internet texting context, use of local slangs is encouraged." if tone == "Casual" else ""
14
 
15
  teacher = MAIAI.Agent(model=model, temperature=0.5, role=f"You are a {language} teacher teaching {native_language} speaking student.")
16
- responder = MAIAI.Agent(model=model, temperature=0.5, role=f"""You are a {language} {persona}. Respond to the user's text in {language} and give {native_language} translation. Refer to Chat History for context. Keep the conversation going. {casual}""")
 
17
 
18
  feedback_task = MAIAI.Task(
19
  agent=teacher,
20
  goal=f"""Text: {text}
21
 
22
- Correct any linguistic error in the text and give example driven feedback on how to improve the text in {language}.
23
- Translate any non-{language} from the text into {language}.
24
  You MUST give your feedback in {native_language}.
25
  {casual}
26
  """
@@ -30,18 +223,28 @@ You MUST give your feedback in {native_language}.
30
  agent=responder,
31
  goal=f"""{text}
32
 
33
- Respond to the text above in {language} and give {native_language} translation for all your response.
34
  Refer to Chat History for context.
35
  Chat History: {history}"""
36
  )
37
 
38
  feedback = feedback_task.execute()
39
-
40
- yield f"""Feedback:
41
- {feedback}"""
42
 
43
  response = respond_task.execute()
44
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45
  output = f"""
46
  ***Feedback:***
47
 
@@ -52,9 +255,12 @@ You MUST give your feedback in {native_language}.
52
  ***{persona}:***
53
 
54
  {response}
 
55
  """
56
-
57
- yield output
 
 
58
 
59
  # Sample Function Call ------------------------------------
60
 
@@ -66,15 +272,36 @@ You MUST give your feedback in {native_language}.
66
  # Reply: {response}
67
  # """)
68
 
69
- # Gradio ----------------------------------------------------
70
- chat_interface = gr.ChatInterface(
71
- fn=chat,
72
- additional_inputs=[
73
- gr.components.Dropdown(choices=["English","中文","Spanish"], value="English", allow_custom_value=True, label="I speak"),
74
- gr.components.Dropdown(choices=["English","中文","Spanish"], value="English", allow_custom_value=True, label="I want to learn"),
75
- gr.components.Textbox(value = "LinguAI Chatbot", label="I want to talk to"),
76
- gr.components.Dropdown(choices=["Casual","Formal"], value="Casual", label="Tone")],
77
- title="LinguAI"
78
- )
79
-
80
- chat_interface.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  from openai import OpenAI
2
  from dotenv import load_dotenv
 
3
  import MAIAI
4
 
5
+ # from deep_translator import GoogleTranslator
6
+
7
+ import speech_recognition as sr
8
+ # import assemblyai as aai
9
+
10
+ # import pyttsx3
11
+ from gtts import gTTS
12
+
13
+ import gradio as gr
14
+ from gradio.themes.base import Base
15
+
16
  # Load environment variables
17
  load_dotenv()
18
 
19
+ language_map = {
20
+ "Afrikaans": "af",
21
+ "Albanian": "sq",
22
+ "Amharic": "am",
23
+ "Arabic": "ar",
24
+ "Armenian": "hy",
25
+ "Azerbaijani": "az",
26
+ "Basque": "eu",
27
+ "Bengali": "bn",
28
+ "Bosnian": "bs",
29
+ "Bulgarian": "bg",
30
+ "Catalan": "ca",
31
+ "Cebuano": "ceb",
32
+ "中文" :"zh-CN",
33
+ "繁体中文": "zh-TW",
34
+ "简体中文": "zh-CN",
35
+ "Chinese" : "zh-CN",
36
+ "Chinese (Simplified)": "zh-CN",
37
+ "Chinese (Traditional)": "zh-TW",
38
+ "Corsican": "co",
39
+ "Croatian": "hr",
40
+ "Czech": "cs",
41
+ "Danish": "da",
42
+ "Dutch": "nl",
43
+ "English": "en",
44
+ "Esperanto": "eo",
45
+ "Estonian": "et",
46
+ "Filipino": "fil",
47
+ "Finnish": "fi",
48
+ "French": "fr",
49
+ "Frisian": "fy",
50
+ "Galician": "gl",
51
+ "Georgian": "ka",
52
+ "German": "de",
53
+ "Greek": "el",
54
+ "Gujarati": "gu",
55
+ "Haitian Creole": "ht",
56
+ "Hausa": "ha",
57
+ "Hawaiian": "haw",
58
+ "Hebrew": "he",
59
+ "Hindi": "hi",
60
+ "Hmong": "hmn",
61
+ "Hungarian": "hu",
62
+ "Icelandic": "is",
63
+ "Igbo": "ig",
64
+ "Indonesian": "id",
65
+ "Irish": "ga",
66
+ "Italian": "it",
67
+ "Japanese": "ja",
68
+ "Javanese": "jv",
69
+ "Kannada": "kn",
70
+ "Kazakh": "kk",
71
+ "Khmer": "km",
72
+ "Kinyarwanda": "rw",
73
+ "Korean": "ko",
74
+ "Kurdish": "ku",
75
+ "Kyrgyz": "ky",
76
+ "Lao": "lo",
77
+ "Latin": "la",
78
+ "Latvian": "lv",
79
+ "Lithuanian": "lt",
80
+ "Luxembourgish": "lb",
81
+ "Macedonian": "mk",
82
+ "Malagasy": "mg",
83
+ "Malay": "ms",
84
+ "Malayalam": "ml",
85
+ "Maltese": "mt",
86
+ "Maori": "mi",
87
+ "Marathi": "mr",
88
+ "Mongolian": "mn",
89
+ "Myanmar (Burmese)": "my",
90
+ "Nepali": "ne",
91
+ "Norwegian": "no",
92
+ "Nyanja (Chichewa)": "ny",
93
+ "Odia (Oriya)": "or",
94
+ "Pashto": "ps",
95
+ "Persian": "fa",
96
+ "Polish": "pl",
97
+ "Portuguese": "pt",
98
+ "Punjabi": "pa",
99
+ "Romanian": "ro",
100
+ "Russian": "ru",
101
+ "Samoan": "sm",
102
+ "Scots Gaelic": "gd",
103
+ "Serbian": "sr",
104
+ "Sesotho": "st",
105
+ "Shona": "sn",
106
+ "Sindhi": "sd",
107
+ "Sinhala (Sinhalese)": "si",
108
+ "Slovak": "sk",
109
+ "Slovenian": "sl",
110
+ "Somali": "so",
111
+ "Spanish": "es",
112
+ "Sundanese": "su",
113
+ "Swahili": "sw",
114
+ "Swedish": "sv",
115
+ "Tajik": "tg",
116
+ "Tamil": "ta",
117
+ "Tatar": "tt",
118
+ "Telugu": "te",
119
+ "Thai": "th",
120
+ "Turkish": "tr",
121
+ "Turkmen": "tk",
122
+ "Ukrainian": "uk",
123
+ "Urdu": "ur",
124
+ "Uyghur": "ug",
125
+ "Uzbek": "uz",
126
+ "Vietnamese": "vi",
127
+ "Welsh": "cy",
128
+ "Xhosa": "xh",
129
+ "Yiddish": "yi",
130
+ "Yoruba": "yo",
131
+ "Zulu": "zu"
132
+ }
133
+
134
+ # def pytts(input_text):
135
+ # if input_text:
136
+ # engine = pyttsx3.init()
137
+
138
+ # # # Get available voices and print them out
139
+ # # voices = engine.getProperty('voices')
140
+ # # for index, voice in enumerate(voices):
141
+ # # print(f"Voice {index}: {voice.id} - {voice.languages} - {voice.gender} - {voice.name}")
142
+
143
+ # # # Set voice (change index based on what is available on your system)
144
+ # # engine.setProperty('voice', voices[1].id) # Change the index to switch voices
145
+
146
+ # # # Set speech rate
147
+ # # rate = engine.getProperty('rate')
148
+ # # engine.setProperty('rate', rate - 50) # Decrease rate; increase to make it faster
149
+
150
+ # # # Set volume
151
+ # # volume = engine.getProperty('volume')
152
+ # # engine.setProperty('volume', volume + 0.25) # Increase volume; decrease to lower the volume
153
+
154
+ # # Speak text
155
+ # engine.say(input_text)
156
+ # engine.runAndWait()
157
+
158
+ def gtts(input_text,language='English'):
159
+ if input_text:
160
+ # Map the user-friendly language name to the IETF tag
161
+ lang = language_map.get(language, 'en') # Default to 'en' if language not found
162
+ tts = gTTS(text=input_text, lang=lang, slow=False)
163
+ audio_file = "output.mp3"
164
+ tts.save(audio_file)
165
+ return audio_file
166
+
167
+ # def assembly_speech_to_text(audio_file_path):
168
+ # aai.settings.api_key = "e00881b941ff47ea914594c40f6dbc20"
169
+ # transcriber = aai.Transcriber()
170
+ # transcript = transcriber.transcribe(audio_file_path)
171
+ # return transcript.text
172
+
173
+ def google_speech_to_text(audio_file_path):
174
+ if audio_file_path:
175
+ recognizer = sr.Recognizer()
176
+ with sr.AudioFile(audio_file_path) as source:
177
+ audio_data = recognizer.record(source)
178
+ try:
179
+ text = recognizer.recognize_google(audio_data)
180
+ return text
181
+ except sr.UnknownValueError:
182
+ return "Google Speech Recognition could not understand audio"
183
+ except sr.RequestError as e:
184
+ return f"Could not request results from Google Speech Recognition service; {e}"
185
+
186
+
187
+ # def openai_speech_to_text(audio_file_path):
188
+ # if audio_file_path:
189
+ # client = OpenAI()
190
+ # audio_file= open(audio_file_path, "rb")
191
+ # transcription = client.audio.transcriptions.create(
192
+ # model="whisper-1",
193
+ # response_format="text",
194
+ # file=audio_file
195
+ # )
196
+ # return transcription
197
+
198
  def chat(text, history, native_language, language, persona, tone = "Casual", model = "gpt-4o-mini"):
199
 
200
  print(tone, native_language, language, persona)
201
 
202
+ # if audio != None:
203
+ # text = speechtotext(audio)
204
+
205
  casual = "This is in a casual, internet texting context, use of local slangs is encouraged." if tone == "Casual" else ""
206
 
207
  teacher = MAIAI.Agent(model=model, temperature=0.5, role=f"You are a {language} teacher teaching {native_language} speaking student.")
208
+ 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}""")
209
+ translator = MAIAI.Agent(model=model, temperature=0.5, role=f"You are a language translator")
210
 
211
  feedback_task = MAIAI.Task(
212
  agent=teacher,
213
  goal=f"""Text: {text}
214
 
215
+ Point out and translate any non-{language} from the text into {language}.
216
+ Correct any linguistic error in the text and give example driven feedback on how to improve the text.
217
  You MUST give your feedback in {native_language}.
218
  {casual}
219
  """
 
223
  agent=responder,
224
  goal=f"""{text}
225
 
226
+ Respond to the text above in {language}.
227
  Refer to Chat History for context.
228
  Chat History: {history}"""
229
  )
230
 
231
  feedback = feedback_task.execute()
 
 
 
232
 
233
  response = respond_task.execute()
234
 
235
+ translate_task = MAIAI.Task(
236
+ agent=translator,
237
+ goal=f"translate {response} from {language} to {native_language}"
238
+ )
239
+
240
+ translation = translate_task.execute()
241
+
242
+ # # Get IETF tags for target and native languages
243
+ # native_lang = language_map.get(native_language, 'en')
244
+
245
+ # # Translate the response to the target language
246
+ # translated_response = GoogleTranslator(source='auto', target=native_lang).translate(response)
247
+
248
  output = f"""
249
  ***Feedback:***
250
 
 
255
  ***{persona}:***
256
 
257
  {response}
258
+ ({translation})
259
  """
260
+
261
+ history.append((text, output))
262
+
263
+ return "", history, response
264
 
265
  # Sample Function Call ------------------------------------
266
 
 
272
  # Reply: {response}
273
  # """)
274
 
275
+ # Gradio Custom Chatbot -------------------------------------------------------
276
+
277
+ with gr.Blocks(fill_height=True) as demo:
278
+
279
+ chatbot = gr.Chatbot(
280
+ elem_id="chatbot",
281
+ bubble_full_width=False,
282
+ scale=1,
283
+ )
284
+
285
+ with gr.Row():
286
+
287
+ chat_input = gr.Textbox(interactive=True, scale=8,
288
+ inputs=[gr.components.Audio(sources="microphone", type="filepath", label="Speak or upload audio")],
289
+ value=openai_speech_to_text)
290
+ submit_button = gr.Button("Submit", scale=1)
291
+
292
+ with gr.Row():
293
+ response = gr.Textbox(visible=False, label="Read out Chat Response")
294
+ output_audio = gr.Audio(label="Reply Audio", type="filepath", scale = 9)
295
+ read_out_loud = gr.Button("Read Reply", scale = 1)
296
+
297
+ with gr.Accordion(label = "Settings"):
298
+ native_language = gr.components.Dropdown(choices=["English","中文","Spanish"], value="English", allow_custom_value=True, label="I speak")
299
+ language = gr.components.Dropdown(choices=["English","中文","Spanish"], value="English", allow_custom_value=True, label="I want to learn")
300
+ persona = gr.components.Textbox(value = "LinguAI Chatbot", label="I want to talk to")
301
+ tone = gr.components.Dropdown(choices=["Casual","Formal"], value="Casual", label="Tone")
302
+
303
+ chat_input.submit(chat, [chat_input, chatbot, native_language, language, persona, tone], [chat_input, chatbot, response])
304
+ submit_button.click(chat, [chat_input, chatbot, native_language, language, persona, tone], [chat_input, chatbot, response])
305
+ read_out_loud.click(gtts,[response,language],output_audio)
306
+
307
+ demo.launch()