Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,442 +1,441 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
# Commented out IPython magic to ensure Python compatibility.
|
| 4 |
-
# %%shell
|
| 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 |
-
# pip3 install -
|
| 44 |
-
#
|
| 45 |
-
#
|
| 46 |
-
#
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
"""
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
#
|
| 53 |
-
#
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
#
|
| 57 |
-
#
|
| 58 |
-
#
|
| 59 |
-
#
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
from
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
import
|
| 74 |
-
from
|
| 75 |
-
from
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
from
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
pages_chunks
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
Result_with_score
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
genai.
|
| 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 |
-
batch =
|
| 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 |
-
audio, sr
|
| 300 |
-
|
| 301 |
-
|
| 302 |
-
|
| 303 |
-
|
| 304 |
-
|
| 305 |
-
|
| 306 |
-
"
|
| 307 |
-
"
|
| 308 |
-
"
|
| 309 |
-
"
|
| 310 |
-
"
|
| 311 |
-
"
|
| 312 |
-
"
|
| 313 |
-
"
|
| 314 |
-
"
|
| 315 |
-
|
| 316 |
-
|
| 317 |
-
|
| 318 |
-
|
| 319 |
-
|
| 320 |
-
|
| 321 |
-
|
| 322 |
-
|
| 323 |
-
|
| 324 |
-
|
| 325 |
-
|
| 326 |
-
|
| 327 |
-
|
| 328 |
-
|
| 329 |
-
|
| 330 |
-
|
| 331 |
-
|
| 332 |
-
|
| 333 |
-
|
| 334 |
-
|
| 335 |
-
|
| 336 |
-
|
| 337 |
-
|
| 338 |
-
|
| 339 |
-
|
| 340 |
-
|
| 341 |
-
the
|
| 342 |
-
|
| 343 |
-
|
| 344 |
-
|
| 345 |
-
|
| 346 |
-
|
| 347 |
-
|
| 348 |
-
|
| 349 |
-
|
| 350 |
-
|
| 351 |
-
|
| 352 |
-
|
| 353 |
-
|
| 354 |
-
|
| 355 |
-
|
| 356 |
-
|
| 357 |
-
|
| 358 |
-
|
| 359 |
-
|
| 360 |
-
|
| 361 |
-
|
| 362 |
-
|
| 363 |
-
|
| 364 |
-
|
| 365 |
-
|
| 366 |
-
|
| 367 |
-
|
| 368 |
-
# context
|
| 369 |
-
|
| 370 |
-
|
| 371 |
-
|
| 372 |
-
|
| 373 |
-
|
| 374 |
-
|
| 375 |
-
|
| 376 |
-
|
| 377 |
-
|
| 378 |
-
|
| 379 |
-
|
| 380 |
-
|
| 381 |
-
|
| 382 |
-
|
| 383 |
-
|
| 384 |
-
|
| 385 |
-
|
| 386 |
-
|
| 387 |
-
|
| 388 |
-
|
| 389 |
-
|
| 390 |
-
|
| 391 |
-
|
| 392 |
-
|
| 393 |
-
|
| 394 |
-
|
| 395 |
-
|
| 396 |
-
|
| 397 |
-
|
| 398 |
-
|
| 399 |
-
|
| 400 |
-
processed_doc_chunks =
|
| 401 |
-
|
| 402 |
-
|
| 403 |
-
|
| 404 |
-
|
| 405 |
-
|
| 406 |
-
en_to_indic_doc
|
| 407 |
-
|
| 408 |
-
|
| 409 |
-
|
| 410 |
-
|
| 411 |
-
|
| 412 |
-
|
| 413 |
-
|
| 414 |
-
|
| 415 |
-
|
| 416 |
-
|
| 417 |
-
|
| 418 |
-
|
| 419 |
-
|
| 420 |
-
|
| 421 |
-
|
| 422 |
-
|
| 423 |
-
|
| 424 |
-
|
| 425 |
-
|
| 426 |
-
|
| 427 |
-
|
| 428 |
-
|
| 429 |
-
|
| 430 |
-
|
| 431 |
-
gr.
|
| 432 |
-
|
| 433 |
-
],
|
| 434 |
-
|
| 435 |
-
|
| 436 |
-
|
| 437 |
-
|
| 438 |
-
|
| 439 |
-
|
| 440 |
-
|
| 441 |
-
|
| 442 |
launch_gradio_app(show_log=True)
|
|
|
|
| 1 |
+
|
| 2 |
+
|
| 3 |
+
# Commented out IPython magic to ensure Python compatibility.
|
| 4 |
+
# %%shell
|
| 5 |
+
# pip install -q langchain_community langchain_huggingface faiss-cpu gradio openai google-generativeai langchain-google-genai torch torchvision torchaudio youtokentome pypdf accelerate
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
# Commented out IPython magic to ensure Python compatibility.
|
| 10 |
+
# %%capture
|
| 11 |
+
# %%shell
|
| 12 |
+
#
|
| 13 |
+
# # Install the custom version of NeMo by AI4Bharat
|
| 14 |
+
# wget https://indic-asr-public.objectstore.e2enetworks.net/ai4b_nemo.zip
|
| 15 |
+
#
|
| 16 |
+
# unzip -q /content/ai4b_nemo.zip && cd NeMo
|
| 17 |
+
# bash reinstall.sh
|
| 18 |
+
#
|
| 19 |
+
# cd ..
|
| 20 |
+
#
|
| 21 |
+
|
| 22 |
+
# Commented out IPython magic to ensure Python compatibility.
|
| 23 |
+
# %%capture
|
| 24 |
+
# %%shell
|
| 25 |
+
#
|
| 26 |
+
# git clone -q https://github.com/VarunGumma/IndicTransTokenizer
|
| 27 |
+
# cd IndicTransTokenizer
|
| 28 |
+
# pip install -q --editable ./
|
| 29 |
+
# cd ..
|
| 30 |
+
#
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
# Commented out IPython magic to ensure Python compatibility.
|
| 34 |
+
# %%capture
|
| 35 |
+
# %%shell
|
| 36 |
+
#
|
| 37 |
+
# apt-get install libsndfile1-dev ffmpeg
|
| 38 |
+
#
|
| 39 |
+
# git clone https://github.com/gokulkarthik/TTS
|
| 40 |
+
# cd TTS
|
| 41 |
+
#
|
| 42 |
+
# pip3 install -e .[all]
|
| 43 |
+
# pip3 install -r requirements.txt
|
| 44 |
+
#
|
| 45 |
+
# cd ..
|
| 46 |
+
#
|
| 47 |
+
|
| 48 |
+
"""## **Restart session**
|
| 49 |
+
"""
|
| 50 |
+
|
| 51 |
+
# Commented out IPython magic to ensure Python compatibility.
|
| 52 |
+
# %%capture
|
| 53 |
+
# !pip install gradio
|
| 54 |
+
|
| 55 |
+
# Commented out IPython magic to ensure Python compatibility.
|
| 56 |
+
# %%capture
|
| 57 |
+
#
|
| 58 |
+
# # INFO: If you're unable to import these libraries, just rerun this cell again.
|
| 59 |
+
#
|
| 60 |
+
import gradio as gr
|
| 61 |
+
from torch import cuda, inference_mode
|
| 62 |
+
import nemo.collections.asr as nemo_asr
|
| 63 |
+
from IndicTransTokenizer import IndicProcessor
|
| 64 |
+
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
DEVICE = "cuda" if cuda.is_available() else "cpu"
|
| 68 |
+
|
| 69 |
+
print(f"Using device: {DEVICE}")
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
import os
|
| 73 |
+
from langchain_community.vectorstores import FAISS
|
| 74 |
+
from langchain_huggingface import HuggingFaceEmbeddings
|
| 75 |
+
from langchain_community.document_loaders import PyPDFLoader
|
| 76 |
+
|
| 77 |
+
"""### Load and convert PDF data into vectorDB"""
|
| 78 |
+
|
| 79 |
+
pm_kisan_doc = "/content/PM-KISANOperationalGuidelines(English).pdf"
|
| 80 |
+
|
| 81 |
+
from langchain_community.document_loaders import PyPDFLoader
|
| 82 |
+
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
| 83 |
+
|
| 84 |
+
text_splitter = RecursiveCharacterTextSplitter(
|
| 85 |
+
chunk_size=600,
|
| 86 |
+
chunk_overlap=100
|
| 87 |
+
)
|
| 88 |
+
|
| 89 |
+
loader = PyPDFLoader(pm_kisan_doc)
|
| 90 |
+
pages = loader.load_and_split(text_splitter=text_splitter)
|
| 91 |
+
|
| 92 |
+
pages_chunks = [page.page_content for page in pages]
|
| 93 |
+
print(f"Generated {len(pages_chunks)} chunks of {pm_kisan_doc}")
|
| 94 |
+
|
| 95 |
+
pages_chunks[8]
|
| 96 |
+
|
| 97 |
+
embeddings = HuggingFaceEmbeddings(model_name="all-MiniLM-L6-v2")
|
| 98 |
+
|
| 99 |
+
faiss = FAISS.from_texts(pages_chunks, embeddings)
|
| 100 |
+
|
| 101 |
+
"""### Querying the vectorDB"""
|
| 102 |
+
|
| 103 |
+
# Test query
|
| 104 |
+
result = faiss.similarity_search("what are the benefits of PM kisan yojna", k=3)
|
| 105 |
+
|
| 106 |
+
# This returns the most relevant doc similar to the query
|
| 107 |
+
|
| 108 |
+
print(result[0].page_content)
|
| 109 |
+
|
| 110 |
+
Result_with_score = faiss.similarity_search_with_score("what are the benefits of PM kisan yojna", k=3)
|
| 111 |
+
Result_with_score[0]
|
| 112 |
+
|
| 113 |
+
os.environ['GEMINI_API_KEY'] = userdata.get('GEMINI_API_KEY')
|
| 114 |
+
|
| 115 |
+
import google.generativeai as genai
|
| 116 |
+
|
| 117 |
+
def get_gemini_output(prompt, temperature=0.6):
|
| 118 |
+
|
| 119 |
+
genai.configure(api_key= os.environ['GEMINI_API_KEY'])
|
| 120 |
+
model = genai.GenerativeModel(model_name='gemini-pro')
|
| 121 |
+
answer = model.generate_content(prompt,
|
| 122 |
+
generation_config=genai.types.GenerationConfig(
|
| 123 |
+
temperature=0.6))
|
| 124 |
+
|
| 125 |
+
return answer.text
|
| 126 |
+
|
| 127 |
+
"""## Build an end-to-end RAG powered Voice Assistant
|
| 128 |
+
"""
|
| 129 |
+
|
| 130 |
+
ip = IndicProcessor(inference=True)
|
| 131 |
+
|
| 132 |
+
# Commented out IPython magic to ensure Python compatibility.
|
| 133 |
+
# # %%capture
|
| 134 |
+
|
| 135 |
+
en2indic_tokenizer = AutoTokenizer.from_pretrained("ai4bharat/indictrans2-en-indic-dist-200M", trust_remote_code=True)
|
| 136 |
+
en2indic_model = AutoModelForSeq2SeqLM.from_pretrained("ai4bharat/indictrans2-en-indic-dist-200M", trust_remote_code=True)
|
| 137 |
+
|
| 138 |
+
|
| 139 |
+
# Commented out IPython magic to ensure Python compatibility.
|
| 140 |
+
# # %%capture
|
| 141 |
+
|
| 142 |
+
indic2en_tokenizer = AutoTokenizer.from_pretrained("ai4bharat/indictrans2-indic-en-dist-200M", trust_remote_code=True)
|
| 143 |
+
indic2en_model = AutoModelForSeq2SeqLM.from_pretrained("ai4bharat/indictrans2-indic-en-dist-200M", trust_remote_code=True)
|
| 144 |
+
|
| 145 |
+
|
| 146 |
+
model_tokenizer_config = {
|
| 147 |
+
"en2indic": {
|
| 148 |
+
"tokenizer": en2indic_tokenizer,
|
| 149 |
+
"model": en2indic_model,
|
| 150 |
+
},
|
| 151 |
+
"indic2en": {
|
| 152 |
+
"tokenizer": indic2en_tokenizer,
|
| 153 |
+
"model": indic2en_model,
|
| 154 |
+
}
|
| 155 |
+
}
|
| 156 |
+
|
| 157 |
+
def indic_translate(src_lang: str, tgt_lang: str, sents_to_translate: list):
|
| 158 |
+
|
| 159 |
+
lang_map = {
|
| 160 |
+
"punjabi": "pan_Guru",
|
| 161 |
+
"bengali": "ben_Beng",
|
| 162 |
+
"malayalam": "mal_Mlym",
|
| 163 |
+
"marathi": "mar_Deva",
|
| 164 |
+
"tamil": "tam_Taml",
|
| 165 |
+
"gujarati": "guj_Gujr",
|
| 166 |
+
"telugu": "tel_Telu",
|
| 167 |
+
"hindi": "hin_Deva",
|
| 168 |
+
"kannada": "kan_Knda",
|
| 169 |
+
"odia": "ory_Orya",
|
| 170 |
+
"english": "eng_Latn"
|
| 171 |
+
}
|
| 172 |
+
|
| 173 |
+
src_lang = lang_map[src_lang]
|
| 174 |
+
tgt_lang = lang_map[tgt_lang]
|
| 175 |
+
|
| 176 |
+
if src_lang == "eng_Latn":
|
| 177 |
+
tokenizer = model_tokenizer_config["en2indic"]["tokenizer"]
|
| 178 |
+
model = model_tokenizer_config["en2indic"]["model"]
|
| 179 |
+
|
| 180 |
+
print(f"Using en2indic, src_lang: {src_lang}, tgt_lang: {tgt_lang}")
|
| 181 |
+
|
| 182 |
+
else:
|
| 183 |
+
tokenizer = model_tokenizer_config["indic2en"]["tokenizer"]
|
| 184 |
+
model = model_tokenizer_config["indic2en"]["model"]
|
| 185 |
+
|
| 186 |
+
print(f"Using indic2en, src_lang: {src_lang}, tgt_lang: {tgt_lang}")
|
| 187 |
+
|
| 188 |
+
|
| 189 |
+
batch = ip.preprocess_batch(sents_to_translate, src_lang=src_lang, tgt_lang=tgt_lang, show_progress_bar=False)
|
| 190 |
+
batch = tokenizer(batch, padding="longest", truncation=True, max_length=256, return_tensors="pt")
|
| 191 |
+
|
| 192 |
+
with inference_mode():
|
| 193 |
+
print("Generating...")
|
| 194 |
+
outputs = model.generate(**batch, num_beams=5, num_return_sequences=1, max_length=256)
|
| 195 |
+
|
| 196 |
+
with tokenizer.as_target_tokenizer():
|
| 197 |
+
outputs = tokenizer.batch_decode(outputs, skip_special_tokens=True, clean_up_tokenization_spaces=True)
|
| 198 |
+
|
| 199 |
+
if tgt_lang != "en_Latn":
|
| 200 |
+
print(f"Postprocessing for {tgt_lang}")
|
| 201 |
+
outputs = ip.postprocess_batch(outputs, lang=tgt_lang)
|
| 202 |
+
|
| 203 |
+
|
| 204 |
+
return outputs
|
| 205 |
+
|
| 206 |
+
def download_ai4b_tts_model(lang: str):
|
| 207 |
+
|
| 208 |
+
lang_map = {
|
| 209 |
+
"odia": "or",
|
| 210 |
+
"hindi": "hi",
|
| 211 |
+
"tamil": "ta",
|
| 212 |
+
"telugu": "te",
|
| 213 |
+
"punjabi": "pa",
|
| 214 |
+
"kannada": "kn",
|
| 215 |
+
"bengali": "bn",
|
| 216 |
+
"marathi": "mr",
|
| 217 |
+
"gujarati": "gu",
|
| 218 |
+
"malayalam": "ml",
|
| 219 |
+
}
|
| 220 |
+
|
| 221 |
+
selected_lang = lang_map[lang]
|
| 222 |
+
|
| 223 |
+
download_path = f"/{selected_lang}.zip"
|
| 224 |
+
|
| 225 |
+
if os.path.exists(download_path):
|
| 226 |
+
print(f"IndicTTS Model for {lang} already exists.")
|
| 227 |
+
|
| 228 |
+
def run_tts(text, tts_lang):
|
| 229 |
+
|
| 230 |
+
lang_map = {
|
| 231 |
+
"odia": "or",
|
| 232 |
+
"hindi": "hi",
|
| 233 |
+
"tamil": "ta",
|
| 234 |
+
"telugu": "te",
|
| 235 |
+
"punjabi": "pa",
|
| 236 |
+
"kannada": "kn",
|
| 237 |
+
"bengali": "bn",
|
| 238 |
+
"marathi": "mr",
|
| 239 |
+
"gujarati": "gu",
|
| 240 |
+
"malayalam": "ml",
|
| 241 |
+
}
|
| 242 |
+
|
| 243 |
+
download_ai4b_tts_model(lang=tts_lang)
|
| 244 |
+
|
| 245 |
+
tts_lang = lang_map[tts_lang]
|
| 246 |
+
print(f"Lang code: {tts_lang}")
|
| 247 |
+
|
| 248 |
+
|
| 249 |
+
tts_command = f'python3 -m TTS.bin.synthesize --text "{text}" \
|
| 250 |
+
--model_path /models/v1/{tts_lang}/fastpitch/best_model.pth \
|
| 251 |
+
--config_path /models/v1/{tts_lang}/fastpitch/config.json \
|
| 252 |
+
--vocoder_path /models/v1/{tts_lang}/hifigan/best_model.pth \
|
| 253 |
+
--vocoder_config_path /models/v1/{tts_lang}/hifigan/config.json \
|
| 254 |
+
--speakers_file_path /models/v1/{tts_lang}/fastpitch/speakers.pth \
|
| 255 |
+
--out_path /tts_output.wav \
|
| 256 |
+
--speaker_idx male'
|
| 257 |
+
|
| 258 |
+
if DEVICE == "cuda":
|
| 259 |
+
tts_command += " --use_cuda True"
|
| 260 |
+
print(f"Running IndicTTS on GPU")
|
| 261 |
+
|
| 262 |
+
else:
|
| 263 |
+
print(f"Running IndicTTS on CPU")
|
| 264 |
+
|
| 265 |
+
os.system(tts_command)
|
| 266 |
+
|
| 267 |
+
os.makedirs('/asr_models')
|
| 268 |
+
|
| 269 |
+
def download_ai4b_asr_model(lang: str):
|
| 270 |
+
|
| 271 |
+
available_langs = {
|
| 272 |
+
"odia": "or",
|
| 273 |
+
"hindi": "hi",
|
| 274 |
+
"tamil": "ta",
|
| 275 |
+
"telugu": "te",
|
| 276 |
+
"punjabi": "pa",
|
| 277 |
+
"kannada": "kn",
|
| 278 |
+
"bengali": "bn",
|
| 279 |
+
"marathi": "mr",
|
| 280 |
+
"gujarati": "gu",
|
| 281 |
+
"malayalam": "ml",
|
| 282 |
+
}
|
| 283 |
+
|
| 284 |
+
download_path = f"/asr_models/ai4b_indicConformer_{available_langs[lang]}.nemo"
|
| 285 |
+
print(f"Downloaded ASR model path: {download_path}")
|
| 286 |
+
|
| 287 |
+
if os.path.exists(download_path):
|
| 288 |
+
print(f"Model for {lang} already exists.")
|
| 289 |
+
|
| 290 |
+
elif lang not in available_langs:
|
| 291 |
+
raise ValueError(f"Invalid language code: {lang}")
|
| 292 |
+
|
| 293 |
+
return download_path
|
| 294 |
+
|
| 295 |
+
import librosa
|
| 296 |
+
|
| 297 |
+
def preprocess_audio(audio_path):
|
| 298 |
+
audio, sr = librosa.load(audio_path, sr=None, mono=True)
|
| 299 |
+
return audio, sr
|
| 300 |
+
|
| 301 |
+
def transcribe(audio: str, lang: str):
|
| 302 |
+
audio, sr = preprocess_audio(audio)
|
| 303 |
+
|
| 304 |
+
lang_map = {
|
| 305 |
+
"odia": "or",
|
| 306 |
+
"hindi": "hi",
|
| 307 |
+
"tamil": "ta",
|
| 308 |
+
"telugu": "te",
|
| 309 |
+
"punjabi": "pa",
|
| 310 |
+
"kannada": "kn",
|
| 311 |
+
"bengali": "bn",
|
| 312 |
+
"marathi": "mr",
|
| 313 |
+
"gujarati": "gu",
|
| 314 |
+
"malayalam": "ml",
|
| 315 |
+
}
|
| 316 |
+
|
| 317 |
+
download_path = download_ai4b_asr_model(lang=lang)
|
| 318 |
+
|
| 319 |
+
asr_model = nemo_asr.models.ASRModel.restore_from(
|
| 320 |
+
download_path, map_location=DEVICE
|
| 321 |
+
)
|
| 322 |
+
|
| 323 |
+
transcription = asr_model.transcribe(audio, batch_size=1, language_id=lang_map[lang])[0][0]
|
| 324 |
+
print(f"Transcription: {transcription}")
|
| 325 |
+
|
| 326 |
+
return transcription
|
| 327 |
+
|
| 328 |
+
def query_vector_db(query):
|
| 329 |
+
# Combine the top-3 similar documents from the vectorDB
|
| 330 |
+
result = " ".join([result.page_content for result in faiss.similarity_search(query, k=3)])
|
| 331 |
+
|
| 332 |
+
return result
|
| 333 |
+
|
| 334 |
+
from langchain_core.prompts import PromptTemplate
|
| 335 |
+
|
| 336 |
+
def process_user_query(user_query, retrieved_doc):
|
| 337 |
+
|
| 338 |
+
prompt_template = PromptTemplate.from_template(
|
| 339 |
+
"You are a chatbot , which provides information to user based on their queries, \
|
| 340 |
+
the user asks: {user_query}, The information from the related query is: {retrieved_doc}. \
|
| 341 |
+
Now give the output based on the query and relevant information that i provided, written in a structured, well-formatted and concise way. \
|
| 342 |
+
The length of the output should be no more than 70 words, must be in 5 lines."
|
| 343 |
+
)
|
| 344 |
+
|
| 345 |
+
prompt = prompt_template.format(user_query=user_query, retrieved_doc=retrieved_doc)
|
| 346 |
+
|
| 347 |
+
processed_doc = get_gemini_output(prompt)
|
| 348 |
+
print(processed_doc)
|
| 349 |
+
|
| 350 |
+
return processed_doc
|
| 351 |
+
|
| 352 |
+
#Context awareness
|
| 353 |
+
from collections import deque
|
| 354 |
+
|
| 355 |
+
class ContextManger:
|
| 356 |
+
def __init__(self,max_history=7):
|
| 357 |
+
self.history = deque(maxlen=max_history)
|
| 358 |
+
|
| 359 |
+
def add_interaction(self,query,response):
|
| 360 |
+
self.history.append((query,response))
|
| 361 |
+
|
| 362 |
+
def get_context(self):
|
| 363 |
+
return list(self.history)
|
| 364 |
+
|
| 365 |
+
context_manager = ContextManger()
|
| 366 |
+
|
| 367 |
+
# context = context_manager.get_context()
|
| 368 |
+
# contexulized_query = f"Previous context: {context} \n\nCurrent query: {indic_to_en}"
|
| 369 |
+
|
| 370 |
+
import traceback
|
| 371 |
+
|
| 372 |
+
def process_gradio_input(audio, user_lang):
|
| 373 |
+
try:
|
| 374 |
+
# Use IndicASR to transcribe the input audio
|
| 375 |
+
print(f"Transcribing...")
|
| 376 |
+
query_transcription = transcribe(audio, lang=user_lang)
|
| 377 |
+
|
| 378 |
+
# Convert the Indic text from transcription to English, so that GPT-3.5 can process it
|
| 379 |
+
print(f"Translating indic to en..")
|
| 380 |
+
indic_to_en = indic_translate(src_lang=user_lang, tgt_lang="english", sents_to_translate=[query_transcription])[0]
|
| 381 |
+
|
| 382 |
+
# context_manager = ContextManager()
|
| 383 |
+
|
| 384 |
+
context = context_manager.get_context()
|
| 385 |
+
contexulized_query = f"Previous context: {context} \n\nCurrent query: {indic_to_en}"
|
| 386 |
+
|
| 387 |
+
# Query the Vector DB to get the relevant document from the query
|
| 388 |
+
print(f"Querying vector db")
|
| 389 |
+
retrieved_doc = query_vector_db(contexulized_query)
|
| 390 |
+
|
| 391 |
+
# Extract relevant information from the retrieved document
|
| 392 |
+
print(f"Processing user query")
|
| 393 |
+
processed_doc = process_user_query(user_query=contexulized_query, retrieved_doc=retrieved_doc)
|
| 394 |
+
|
| 395 |
+
context_manager.add_interaction(indic_to_en, processed_doc)
|
| 396 |
+
|
| 397 |
+
# Break the document into chunks for faster batch processing
|
| 398 |
+
print(f"Breaking document into chunks..")
|
| 399 |
+
processed_doc_chunks = processed_doc.strip().split(". ")
|
| 400 |
+
processed_doc_chunks = [f"{chunk}." for chunk in processed_doc_chunks if chunk != ""]
|
| 401 |
+
|
| 402 |
+
# Translate the the extracted information back to Indic language
|
| 403 |
+
print(f"Translating en to indic..")
|
| 404 |
+
en_to_indic_chunks = indic_translate(src_lang="english", tgt_lang=user_lang, sents_to_translate=processed_doc_chunks)
|
| 405 |
+
en_to_indic_doc = " ".join(en_to_indic_chunks)
|
| 406 |
+
print(f"en_to_indic_doc: {en_to_indic_doc}")
|
| 407 |
+
|
| 408 |
+
# Run IndicTTS to generate audio
|
| 409 |
+
print(f"Running TTS to generate audio..")
|
| 410 |
+
run_tts(text=en_to_indic_doc, tts_lang=user_lang)
|
| 411 |
+
print("Finished running TTS")
|
| 412 |
+
|
| 413 |
+
audio_outfile_path = "/content/tts_output.wav"
|
| 414 |
+
|
| 415 |
+
|
| 416 |
+
return en_to_indic_doc, audio_outfile_path
|
| 417 |
+
|
| 418 |
+
except Exception as e:
|
| 419 |
+
error_message = f"An error occurred: {str(e)}\n\nTraceback:\n{traceback.format_exc()}"
|
| 420 |
+
print(error_message)
|
| 421 |
+
return error_message, None
|
| 422 |
+
|
| 423 |
+
def launch_gradio_app(show_log=False):
|
| 424 |
+
|
| 425 |
+
languages = ["hindi", "odia", "tamil", "telugu", "punjabi", "kannada", "bengali", "marathi", "gujarati", "malayalam"]
|
| 426 |
+
|
| 427 |
+
iface = gr.Interface(
|
| 428 |
+
fn=process_gradio_input,
|
| 429 |
+
inputs=[
|
| 430 |
+
gr.Audio(sources=['upload', 'microphone'], type="filepath", show_download_button=True), # Input audio
|
| 431 |
+
gr.Dropdown(languages, label="Language", value="hindi"), # Language selection
|
| 432 |
+
],
|
| 433 |
+
outputs=["text", "audio"],
|
| 434 |
+
allow_flagging="never",
|
| 435 |
+
title="Farmer's Voice Assistant 🧑🌾 Powered by AI4Bharat Tech",
|
| 436 |
+
description="Know about latest farming schemes, this system is powered by tools from AI4Bharat, like IndicASR, IndicTTS and IndicTrans",
|
| 437 |
+
)
|
| 438 |
+
|
| 439 |
+
iface.launch(debug=show_log)
|
| 440 |
+
|
|
|
|
| 441 |
launch_gradio_app(show_log=True)
|