File size: 12,959 Bytes
ee85d5a 0ade2f2 ee85d5a 0ade2f2 ee85d5a 4b32f85 68582b0 ee85d5a d974188 ee85d5a 88855a8 ee85d5a 88855a8 8ff0a94 ee85d5a 9f0f392 88855a8 ee85d5a 88855a8 14c1943 26397ac 0e71381 ee85d5a 88855a8 ee85d5a 8ff0a94 ee85d5a 14c1943 4b32f85 0e71381 ee85d5a 88855a8 ee85d5a 88855a8 0683564 910cf6e 88855a8 9f94b69 21d31a6 68582b0 910cf6e 88855a8 9f94b69 88855a8 ee85d5a 8c18340 3515435 4399c07 |
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 |
import gradio as gr
from transformers import pipeline
import requests
import json
import edge_tts
from edge_tts import VoicesManager
import asyncio
import random
import tempfile
import os
import inflect
from huggingface_hub import InferenceClient
import re
import time
from streaming_stt_nemo import Model
language_dict = {
'English-Jenny (Female)': 'en-US-JennyNeural',
'English-Guy (Male)': 'en-US-GuyNeural',
'English-Ana (Female)': 'en-US-AnaNeural',
'English-Aria (Female)': 'en-US-AriaNeural',
'English-Christopher (Male)': 'en-US-ChristopherNeural',
'English-Eric (Male)': 'en-US-EricNeural',
'English-Michelle (Female)': 'en-US-MichelleNeural',
'English-Roger (Male)': 'en-US-RogerNeural',
'Spanish (Mexican)-Dalia (Female)': 'es-MX-DaliaNeural',
'Spanish (Mexican)-Jorge- (Male)': 'es-MX-JorgeNeural',
'Korean-Sun-Hi- (Female)': 'ko-KR-SunHiNeural',
'Korean-InJoon- (Male)': 'ko-KR-InJoonNeural',
'Thai-Premwadee- (Female)': 'th-TH-PremwadeeNeural',
'Thai-Niwat- (Male)': 'th-TH-NiwatNeural',
'Vietnamese-HoaiMy- (Female)': 'vi-VN-HoaiMyNeural',
'Vietnamese-NamMinh- (Male)': 'vi-VN-NamMinhNeural',
'Japanese-Nanami- (Female)': 'ja-JP-NanamiNeural',
'Japanese-Keita- (Male)': 'ja-JP-KeitaNeural',
'French-Denise- (Female)': 'fr-FR-DeniseNeural',
'French-Eloise- (Female)': 'fr-FR-EloiseNeural',
'French-Henri- (Male)': 'fr-FR-HenriNeural',
'Brazilian-Francisca- (Female)': 'pt-BR-FranciscaNeural',
'Brazilian-Antonio- (Male)': 'pt-BR-AntonioNeural',
'Hebrew-Avri- (Male)': 'he-IL-AvriNeural',
'Hebrew-Hila- (Female)': 'he-IL-HilaNeural',
'Italian-Isabella- (Female)': 'it-IT-IsabellaNeural',
'Italian-Diego- (Male)': 'it-IT-DiegoNeural',
'Italian-Elsa- (Female)': 'it-IT-ElsaNeural',
'Dutch-Colette- (Female)': 'nl-NL-ColetteNeural',
'Dutch-Fenna- (Female)': 'nl-NL-FennaNeural',
'Dutch-Maarten- (Male)': 'nl-NL-MaartenNeural',
'Norwegian-Pernille- (Female)': 'nb-NO-PernilleNeural',
'Norwegian-Finn- (Male)': 'nb-NO-FinnNeural',
'Swedish-Sofie- (Female)': 'sv-SE-SofieNeural',
'Swedish-Mattias- (Male)': 'sv-SE-MattiasNeural',
'Arabic-Hamed- (Male)': 'ar-SA-HamedNeural',
'Arabic-Zariyah- (Female)': 'ar-SA-ZariyahNeural',
'Greek-Athina- (Female)': 'el-GR-AthinaNeural',
'Greek-Nestoras- (Male)': 'el-GR-NestorasNeural',
'German-Katja- (Female)': 'de-DE-KatjaNeural',
'German-Amala- (Female)': 'de-DE-AmalaNeural',
'German-Conrad- (Male)': 'de-DE-ConradNeural',
'German-Killian- (Male)': 'de-DE-KillianNeural',
'Danish (Denmark)-Christel- (Female)': 'da-DK-ChristelNeural',
'Danish (Denmark)-Jeppe- (Male)': 'da-DK-JeppeNeural',
'German (Austria)-Ingrid- (Female)': 'de-AT-IngridNeural',
'German (Austria)-Jonas- (Male)': 'de-AT-JonasNeural',
'German (Switzerland)-Jan- (Male)': 'de-CH-JanNeural',
'German (Switzerland)-Leni- (Female)': 'de-CH-LeniNeural',
'English (Australia)-Natasha- (Female)': 'en-AU-NatashaNeural',
'English (Australia)-William- (Male)': 'en-AU-WilliamNeural',
'English (Canada)-Clara- (Female)': 'en-CA-ClaraNeural',
'English (Canada)-Liam- (Male)': 'en-CA-LiamNeural',
'English (UK)-Libby- (Female)': 'en-GB-LibbyNeural',
'English (UK)-Maisie- (Female)': 'en-GB-MaisieNeural',
'English (UK)-Ryan- (Male)': 'en-GB-RyanNeural',
'English (UK)-Sonia- (Female)': 'en-GB-SoniaNeural',
'English (UK)-Thomas- (Male)': 'en-GB-ThomasNeural',
'English (Hong Kong)-Sam- (Male)': 'en-HK-SamNeural',
'English (Hong Kong)-Yan- (Female)': 'en-HK-YanNeural',
'English (Ireland)-Connor- (Male)': 'en-IE-ConnorNeural',
'English (Ireland)-Emily- (Female)': 'en-IE-EmilyNeural',
'English (India)-Neerja- (Female)': 'en-IN-NeerjaNeural',
'English (India)-Prabhat- (Male)': 'en-IN-PrabhatNeural',
'English (Kenya)-Asilia- (Female)': 'en-KE-AsiliaNeural',
'English (Kenya)-Chilemba- (Male)': 'en-KE-ChilembaNeural',
'English (Nigeria)-Abeo- (Male)': 'en-NG-AbeoNeural',
'English (Nigeria)-Ezinne- (Female)': 'en-NG-EzinneNeural',
'English (New Zealand)-Mitchell- (Male)': 'en-NZ-MitchellNeural',
'English (Philippines)-James- (Male)': 'en-PH-JamesNeural',
'English (Philippines)-Rosa- (Female)': 'en-PH-RosaNeural',
'English (Singapore)-Luna- (Female)': 'en-SG-LunaNeural',
'English (Singapore)-Wayne- (Male)': 'en-SG-WayneNeural',
'English (Tanzania)-Elimu- (Male)': 'en-TZ-ElimuNeural',
'English (Tanzania)-Imani- (Female)': 'en-TZ-ImaniNeural',
'English (South Africa)-Leah- (Female)': 'en-ZA-LeahNeural',
'English (South Africa)-Luke- (Male)': 'en-ZA-LukeNeural',
'Spanish (Argentina)-Elena- (Female)': 'es-AR-ElenaNeural',
'Spanish (Argentina)-Tomas- (Male)': 'es-AR-TomasNeural',
'Spanish (Bolivia)-Marcelo- (Male)': 'es-BO-MarceloNeural',
'Spanish (Bolivia)-Sofia- (Female)': 'es-BO-SofiaNeural',
'Spanish (Colombia)-Gonzalo- (Male)': 'es-CO-GonzaloNeural',
'Spanish (Colombia)-Salome- (Female)': 'es-CO-SalomeNeural',
'Spanish (Costa Rica)-Juan- (Male)': 'es-CR-JuanNeural',
'Spanish (Costa Rica)-Maria- (Female)': 'es-CR-MariaNeural',
'Spanish (Cuba)-Belkys- (Female)': 'es-CU-BelkysNeural',
'Spanish (Dominican Republic)-Emilio- (Male)': 'es-DO-EmilioNeural',
'Spanish (Dominican Republic)-Ramona- (Female)': 'es-DO-RamonaNeural',
'Spanish (Ecuador)-Andrea- (Female)': 'es-EC-AndreaNeural',
'Spanish (Ecuador)-Luis- (Male)': 'es-EC-LuisNeural',
'Spanish (Spain)-Alvaro- (Male)': 'es-ES-AlvaroNeural',
'Spanish (Spain)-Elvira- (Female)': 'es-ES-ElviraNeural',
'Spanish (Equatorial Guinea)-Teresa- (Female)': 'es-GQ-TeresaNeural',
'Spanish (Guatemala)-Andres- (Male)': 'es-GT-AndresNeural',
'Spanish (Guatemala)-Marta- (Female)': 'es-GT-MartaNeural',
'Spanish (Honduras)-Carlos- (Male)': 'es-HN-CarlosNeural',
'Spanish (Honduras)-Karla- (Female)': 'es-HN-KarlaNeural',
'Spanish (Nicaragua)-Federico- (Male)': 'es-NI-FedericoNeural',
'Spanish (Nicaragua)-Yolanda- (Female)': 'es-NI-YolandaNeural',
'Spanish (Panama)-Margarita- (Female)': 'es-PA-MargaritaNeural',
'Spanish (Panama)-Roberto- (Male)': 'es-PA-RobertoNeural',
'Spanish (Peru)-Alex- (Male)': 'es-PE-AlexNeural',
'Spanish (Peru)-Camila- (Female)': 'es-PE-CamilaNeural',
'Spanish (Puerto Rico)-Karina- (Female)': 'es-PR-KarinaNeural',
'Spanish (Puerto Rico)-Victor- (Male)': 'es-PR-VictorNeural',
'Spanish (Paraguay)-Mario- (Male)': 'es-PY-MarioNeural',
'Spanish (Paraguay)-Tania- (Female)': 'es-PY-TaniaNeural',
'Spanish (El Salvador)-Lorena- (Female)': 'es-SV-LorenaNeural',
'Spanish (El Salvador)-Rodrigo- (Male)': 'es-SV-RodrigoNeural',
'Spanish (United States)-Alonso- (Male)': 'es-US-AlonsoNeural',
'Spanish (United States)-Paloma- (Female)': 'es-US-PalomaNeural',
'Spanish (Uruguay)-Mateo- (Male)': 'es-UY-MateoNeural',
'Spanish (Uruguay)-Valentina- (Female)': 'es-UY-ValentinaNeural',
'Spanish (Venezuela)-Paola- (Female)': 'es-VE-PaolaNeural',
'Spanish (Venezuela)-Sebastian- (Male)': 'es-VE-SebastianNeural',
'Estonian (Estonia)-Anu- (Female)': 'et-EE-AnuNeural',
'Estonian (Estonia)-Kert- (Male)': 'et-EE-KertNeural',
'Persian (Iran)-Dilara- (Female)': 'fa-IR-DilaraNeural',
'Persian (Iran)-Farid- (Male)': 'fa-IR-FaridNeural',
'Finnish (Finland)-Harri- (Male)': 'fi-FI-HarriNeural',
'Finnish (Finland)-Noora- (Female)': 'fi-FI-NooraNeural',
'French (Belgium)-Charline- (Female)': 'fr-BE-CharlineNeural',
'French (Belgium)-Gerard- (Male)': 'fr-BE-GerardNeural',
'French (Canada)-Sylvie- (Female)': 'fr-CA-SylvieNeural',
'French (Canada)-Antoine- (Male)': 'fr-CA-AntoineNeural',
'French (Canada)-Jean- (Male)': 'fr-CA-JeanNeural',
'French (Switzerland)-Ariane- (Female)': 'fr-CH-ArianeNeural',
'French (Switzerland)-Fabrice- (Male)': 'fr-CH-FabriceNeural',
'Irish (Ireland)-Colm- (Male)': 'ga-IE-ColmNeural',
'Irish (Ireland)-Orla- (Female)': 'ga-IE-OrlaNeural',
}
Male_language_dict = {
'English-Guy (Male)': 'en-US-GuyNeural',
'English-Christopher (Male)': 'en-US-ChristopherNeural',
'English-Eric (Male)': 'en-US-EricNeural',
'English-Roger (Male)': 'en-US-RogerNeural',
'English (Australia)-William- (Male)': 'en-AU-WilliamNeural',
'English (Canada)-Liam- (Male)': 'en-CA-LiamNeural',
'English (UK)-Ryan- (Male)': 'en-GB-RyanNeural',
'English (UK)-Thomas- (Male)': 'en-GB-ThomasNeural',
'English (Ireland)-Connor- (Male)': 'en-IE-ConnorNeural',
}
default_lang = "en"
engines = { default_lang: Model(default_lang) }
client1 = InferenceClient("mistralai/Mixtral-8x7B-Instruct-v0.1")
system_instructions1 = "<s>[SYSTEM] YOU must Output only plain text. Do not use **bold**, *italic*, ### headings, **number** or any other markdown-specific formatting in content. Answer as Dr. Watson's Famous Sherlock Holmes Assistant, with a male voice and London slang and accent. Remember you are the character and Assistant Dr. Watson, from Sherlock Holmes Novels, Keep the conversation short, clear, friendly, and concise. You will refer to the USER alternatively as: 'DEAR FRIEND' or 'MY HELPMATE' or 'MY BIOGRAPHER' or 'MY COMPANION'. The text provided is a request for a specific type of response from you, the virtual assistant. The request asks you to provide friendly responses as if You are the character Dr. Watson, from the Sherlock Holmes Novels. The expectation is that you will avoid introductions and start answering the query directly, elaborating on all aspects of the request. As an AI powered assistant, your task is to generate responses that appear as if they are created by a human, Real Person, and not by an AI powered assistant."
def transcribe(audio):
lang = "en"
model = engines[lang]
text = model.stt_file(audio)[0]
return text
def model(text):
generate_kwargs = dict(
temperature=0.7,
max_new_tokens=512,
top_p=0.95,
repetition_penalty=1,
do_sample=True,
seed=42,
)
formatted_prompt = system_instructions1 + text + "[WATSON]"
stream = client1.text_generation(
formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=False)
output = ""
for response in stream:
if not response.token.text == "</s>":
output += response.token.text
return output
async def respond(language_code, audio):
user = transcribe(audio)
reply = model(user)
voice = Male_language_dict.get(language_code, "default_voice")
communicate = edge_tts.Communicate(reply, voice)
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmp_file:
tmp_path = tmp_file.name
await communicate.save(tmp_path)
yield tmp_path
async def generate1(language_code, prompt):
generate_kwargs = dict(
temperature=0.7,
max_new_tokens=512,
top_p=0.95,
repetition_penalty=1,
do_sample=False,
)
formatted_prompt = system_instructions1 + prompt + "[WATSON]"
stream = client1.text_generation(
formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=True)
output = ""
for response in stream:
if not response.token.text == "</s>":
output += response.token.text
voice = Male_language_dict.get(language_code, "default_voice")
communicate = edge_tts.Communicate(output, voice)
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmp_file:
tmp_path = tmp_file.name
await communicate.save(tmp_path)
yield tmp_path
# css="style.css"
with gr.Blocks(gr.themes.Soft()) as demo:
gr.HTML(""" <img src='https://huggingface.co/spaces/Isidorophp/Talk-to-Dr.Watson/resolve/main/logo.png' alt='RJP DEV STUDIO logo' style='height:60px;'> """
""" <center><h1> DR. Watson 🤖 🧠 🧬</h1></center> """
""" <center><h3> I suggest, you ask me for a mystery: </h3></center> """)
with gr.Tab("Talk to Dr. WATSON"):
with gr.Group():
user_voice = gr.Dropdown(choices=list(Male_language_dict.keys()), value="English (UK)-Ryan- (Male)" , label="Select Voice for Dr. WATSON")
us_input = gr.Audio(label="Your Voice Chat", type="filepath", interactive=True, sources="microphone", waveform_options=None)
us_output = gr.Audio(label="WATSON", type="filepath", interactive=False, autoplay=True, elem_classes="audio")
gr.Interface(fn=respond, inputs=[user_voice, us_input], outputs=us_output, live=False)
with gr.Tab("Write to Dr. WATSON"):
with gr.Group():
user_voice = gr.Dropdown(choices=list(Male_language_dict.keys()), value="English (UK)-Ryan- (Male)" , label="Select Voice for Dr. WATSON")
user_input = gr.TextArea(label="Your Question", value="Dr. Watson, can you summarize your adventures with Sherlock Holmes?")
output_audio = gr.Audio(label="WATSON", type="filepath", interactive=False, autoplay=True, elem_classes="audio")
gr.Interface(fn=generate1, inputs=[user_voice, user_input], outputs=output_audio, live=False)
if __name__ == "__main__":
demo.queue(max_size=200, api_open=False).launch()
|