NCTCMumbai commited on
Commit
1c9c5ca
·
verified ·
1 Parent(s): 5ddc5fd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +3 -372
app.py CHANGED
@@ -156,7 +156,8 @@ def bot(history, cross_encoder):
156
  # Create a new history entry instead of modifying the tuple directly
157
  new_history = history[:-1] + [ (query, "") ]
158
  for character in generate_fn(prompt, history[:-1]):
159
- new_history[-1] = (query, character) # Update the last tuple with new text
 
160
  yield new_history, prompt_html
161
  # history[-1][1] = ""
162
  # for character in generate_fn(prompt, history[:-1]):
@@ -196,34 +197,7 @@ def translate_text(selected_language,history):
196
  response_text = history[-1][1] if history else ''
197
  translation = bhashini_translate(response_text, to_code=to_code)
198
  return translation['translated_content']
199
- # iso_language_codes = {
200
- # "Hindi": "hi",
201
- # "Gom": "gom",
202
- # "Kannada": "kn",
203
- # "Dogri": "doi",
204
- # "Bodo": "brx",
205
- # "Urdu": "ur",
206
- # "Tamil": "ta",
207
- # "Kashmiri": "ks",
208
- # "Assamese": "as",
209
- # "Bengali": "bn",
210
- # "Marathi": "mr",
211
- # "Sindhi": "sd",
212
- # "Maithili": "mai",
213
- # "Punjabi": "pa",
214
- # "Malayalam": "ml",
215
- # "Manipuri": "mni",
216
- # "Telugu": "te",
217
- # "Sanskrit": "sa",
218
- # "Nepali": "ne",
219
- # "Santali": "sat",
220
- # "Gujarati": "gu",
221
- # "Odia": "or"
222
- # }
223
-
224
- # to_code = iso_language_codes[selected_language]
225
- # translation = bhashini_translate(response_text, to_code=to_code)
226
- # return translation['translated_content']
227
 
228
  # Gradio interface
229
  with gr.Blocks(theme='gradio/soft') as CHATBOT:
@@ -310,346 +284,3 @@ with gr.Blocks(theme='gradio/soft') as CHATBOT:
310
  # Launch the Gradio application
311
  CHATBOT.launch(share=True)
312
 
313
- # from ragatouille import RAGPretrainedModel
314
- # import subprocess
315
- # import json
316
- # import spaces
317
- # import firebase_admin
318
- # from firebase_admin import credentials, firestore
319
- # import logging
320
- # from pathlib import Path
321
- # from time import perf_counter
322
- # from datetime import datetime
323
- # import gradio as gr
324
- # from jinja2 import Environment, FileSystemLoader
325
- # import numpy as np
326
- # from sentence_transformers import CrossEncoder
327
- # from huggingface_hub import InferenceClient
328
- # from os import getenv
329
-
330
- # from backend.query_llm import generate_hf, generate_openai
331
- # from backend.semantic_search import table, retriever
332
- # from huggingface_hub import InferenceClient
333
-
334
-
335
- # VECTOR_COLUMN_NAME = "vector"
336
- # TEXT_COLUMN_NAME = "text"
337
- # HF_TOKEN = getenv("HUGGING_FACE_HUB_TOKEN")
338
- # proj_dir = Path(__file__).parent
339
- # # Setting up the logging
340
- # logging.basicConfig(level=logging.INFO)
341
- # logger = logging.getLogger(__name__)
342
- # client = InferenceClient("mistralai/Mixtral-8x7B-Instruct-v0.1",token=HF_TOKEN)
343
- # # Set up the template environment with the templates directory
344
- # env = Environment(loader=FileSystemLoader(proj_dir / 'templates'))
345
-
346
- # # Load the templates directly from the environment
347
- # template = env.get_template('template.j2')
348
- # template_html = env.get_template('template_html.j2')
349
-
350
-
351
- # def add_text(history, text):
352
- # history = [] if history is None else history
353
- # history = history + [(text, None)]
354
- # return history, gr.Textbox(value="", interactive=False)
355
-
356
-
357
- # def bot(history, cross_encoder):
358
- # top_rerank = 25
359
- # top_k_rank = 20
360
- # query = history[-1][0]
361
-
362
- # if not query:
363
- # gr.Warning("Please submit a non-empty string as a prompt")
364
- # raise ValueError("Empty string was submitted")
365
-
366
- # logger.warning('Retrieving documents...')
367
-
368
- # # if COLBERT RAGATATOUILLE PROCEDURE :
369
- # if cross_encoder=='(HIGH ACCURATE) ColBERT':
370
- # gr.Warning('Retrieving using ColBERT.. First time query will take a minute for model to load..pls wait')
371
- # RAG= RAGPretrainedModel.from_pretrained("colbert-ir/colbertv2.0")
372
- # RAG_db=RAG.from_index('.ragatouille/colbert/indexes/cbseclass10index')
373
- # documents_full=RAG_db.search(query,k=top_k_rank)
374
-
375
- # documents=[item['content'] for item in documents_full]
376
- # # Create Prompt
377
- # prompt = template.render(documents=documents, query=query)
378
- # prompt_html = template_html.render(documents=documents, query=query)
379
-
380
- # generate_fn = generate_hf
381
-
382
- # history[-1][1] = ""
383
- # for character in generate_fn(prompt, history[:-1]):
384
- # history[-1][1] = character
385
- # yield history, prompt_html
386
- # print('Final history is ',history)
387
- # #store_message(db,history[-1][0],history[-1][1],cross_encoder)
388
- # else:
389
- # # Retrieve documents relevant to query
390
- # document_start = perf_counter()
391
-
392
- # query_vec = retriever.encode(query)
393
- # logger.warning(f'Finished query vec')
394
- # doc1 = table.search(query_vec, vector_column_name=VECTOR_COLUMN_NAME).limit(top_k_rank)
395
-
396
-
397
-
398
- # logger.warning(f'Finished search')
399
- # documents = table.search(query_vec, vector_column_name=VECTOR_COLUMN_NAME).limit(top_rerank).to_list()
400
- # documents = [doc[TEXT_COLUMN_NAME] for doc in documents]
401
- # logger.warning(f'start cross encoder {len(documents)}')
402
- # # Retrieve documents relevant to query
403
- # query_doc_pair = [[query, doc] for doc in documents]
404
- # if cross_encoder=='(FAST) MiniLM-L6v2' :
405
- # cross_encoder1 = CrossEncoder('cross-encoder/ms-marco-MiniLM-L-6-v2')
406
- # elif cross_encoder=='(ACCURATE) BGE reranker':
407
- # cross_encoder1 = CrossEncoder('BAAI/bge-reranker-base')
408
-
409
- # cross_scores = cross_encoder1.predict(query_doc_pair)
410
- # sim_scores_argsort = list(reversed(np.argsort(cross_scores)))
411
- # logger.warning(f'Finished cross encoder {len(documents)}')
412
-
413
- # documents = [documents[idx] for idx in sim_scores_argsort[:top_k_rank]]
414
- # logger.warning(f'num documents {len(documents)}')
415
-
416
- # document_time = perf_counter() - document_start
417
- # logger.warning(f'Finished Retrieving documents in {round(document_time, 2)} seconds...')
418
-
419
- # # Create Prompt
420
- # prompt = template.render(documents=documents, query=query)
421
- # prompt_html = template_html.render(documents=documents, query=query)
422
-
423
- # generate_fn = generate_hf
424
-
425
- # history[-1][1] = ""
426
- # for character in generate_fn(prompt, history[:-1]):
427
- # history[-1][1] = character
428
- # yield history, prompt_html
429
- # print('Final history is ',history)
430
- # #store_message(db,history[-1][0],history[-1][1],cross_encoder)
431
-
432
- # # def system_instructions(question_difficulty, topic,documents_str):
433
- # # return f"""<s> [INST] Your are a great teacher and your task is to create 10 questions with 4 choices with a {question_difficulty} difficulty about topic request " {topic} " only from the below given documents, {documents_str} then create an answers. Index in JSON format, the questions as "Q#":"" to "Q#":"", the four choices as "Q#:C1":"" to "Q#:C4":"", and the answers as "A#":"Q#:C#" to "A#":"Q#:C#". [/INST]"""
434
-
435
- # RAG_db = gr.State()
436
-
437
- # # def load_model():
438
- # # try:
439
- # # # Initialize the model
440
- # # RAG = RAGPretrainedModel.from_pretrained("colbert-ir/colbertv2.0")
441
- # # # Load the RAG database
442
- # # RAG_db.value = RAG.from_index('.ragatouille/colbert/indexes/cbseclass10index')
443
- # # return 'Ready to Go!!'
444
- # # except Exception as e:
445
- # # return f"Error loading model: {e}"
446
-
447
-
448
- # # def generate_quiz(question_difficulty, topic):
449
- # # if not topic.strip():
450
- # # return ['Please enter a valid topic.'] + [gr.Radio(visible=False) for _ in range(10)]
451
-
452
- # # top_k_rank = 10
453
- # # # Load the model and database within the generate_quiz function
454
- # # try:
455
- # # RAG = RAGPretrainedModel.from_pretrained("colbert-ir/colbertv2.0")
456
- # # RAG_db_ = RAG.from_index('.ragatouille/colbert/indexes/cbseclass10index')
457
- # # gr.Warning('Model loaded!')
458
- # # except Exception as e:
459
- # # return [f"Error loading model: {e}"] + [gr.Radio(visible=False) for _ in range(10)]
460
-
461
- # # RAG_db_ = RAG_db.value
462
- # # documents_full = RAG_db_.search(topic, k=top_k_rank)
463
-
464
- # # generate_kwargs = dict(
465
- # # temperature=0.2,
466
- # # max_new_tokens=4000,
467
- # # top_p=0.95,
468
- # # repetition_penalty=1.0,
469
- # # do_sample=True,
470
- # # seed=42,
471
- # # )
472
-
473
- # # question_radio_list = []
474
- # # count = 0
475
- # # while count <= 3:
476
- # # try:
477
- # # documents = [item['content'] for item in documents_full]
478
- # # document_summaries = [f"[DOCUMENT {i+1}]: {summary}{count}" for i, summary in enumerate(documents)]
479
- # # documents_str = '\n'.join(document_summaries)
480
- # # formatted_prompt = system_instructions(question_difficulty, topic, documents_str)
481
-
482
- # # pre_prompt = [
483
- # # {"role": "system", "content": formatted_prompt}
484
- # # ]
485
- # # response = client.text_generation(
486
- # # formatted_prompt, **generate_kwargs, stream=False, details=False, return_full_text=False,
487
- # # )
488
- # # output_json = json.loads(f"{response}")
489
-
490
- # # global quiz_data
491
- # # quiz_data = output_json
492
-
493
- # # for question_num in range(1, 11):
494
- # # question_key = f"Q{question_num}"
495
- # # answer_key = f"A{question_num}"
496
- # # question = quiz_data.get(question_key)
497
- # # answer = quiz_data.get(quiz_data.get(answer_key))
498
-
499
- # # if not question or not answer:
500
- # # continue
501
-
502
- # # choice_keys = [f"{question_key}:C{i}" for i in range(1, 5)]
503
- # # choice_list = [quiz_data.get(choice_key, "Choice not found") for choice_key in choice_keys]
504
-
505
- # # radio = gr.Radio(choices=choice_list, label=question, visible=True, interactive=True)
506
- # # question_radio_list.append(radio)
507
-
508
- # # if len(question_radio_list) == 10:
509
- # # break
510
- # # else:
511
- # # count += 1
512
- # # continue
513
- # # except Exception as e:
514
- # # count += 1
515
- # # if count == 3:
516
- # # return ['Sorry. Pls try with another topic!'] + [gr.Radio(visible=False) for _ in range(10)]
517
- # # continue
518
-
519
- # # return ['Quiz Generated!'] + question_radio_list
520
-
521
- # # def compare_answers(*user_answers):
522
- # # user_answer_list = user_answers
523
- # # answers_list = [quiz_data.get(quiz_data.get(f"A{question_num}")) for question_num in range(1, 11)]
524
-
525
- # # score = sum(1 for answer in user_answer_list if answer in answers_list)
526
-
527
- # # if score > 7:
528
- # # message = f"### Excellent! You got {score} out of 10!"
529
- # # elif score > 5:
530
- # # message = f"### Good! You got {score} out of 10!"
531
- # # else:
532
- # # message = f"### You got {score} out of 10! Don’t worry, you can prepare well and try better next time!"
533
-
534
- # # return message
535
-
536
- # #with gr.Blocks(theme='Insuz/SimpleIndigo') as demo:
537
- # with gr.Blocks(theme='NoCrypt/miku') as CHATBOT:
538
- # with gr.Row():
539
- # with gr.Column(scale=10):
540
- # # gr.Markdown(
541
- # # """
542
- # # # Theme preview: `paris`
543
- # # To use this theme, set `theme='earneleh/paris'` in `gr.Blocks()` or `gr.Interface()`.
544
- # # You can append an `@` and a semantic version expression, e.g. @>=1.0.0,<2.0.0 to pin to a given version
545
- # # of this theme.
546
- # # """
547
- # # )
548
- # gr.HTML(value="""<div style="color: #FF4500;"><h1>ADWITIYA-</h1> <h1><span style="color: #008000">Custom Manual Chatbot and Quizbot</span></h1>
549
- # </div>""", elem_id='heading')
550
-
551
- # gr.HTML(value=f"""
552
- # <p style="font-family: sans-serif; font-size: 16px;">
553
- # Using GenAI for CBIC Capacity Building - A free chat bot developed by National Customs Targeting Center using Open source LLMs for CBIC Officers
554
- # </p>
555
- # """, elem_id='Sub-heading')
556
- # #usage_count = get_and_increment_value_count(db,collection_name, field_name)
557
- # gr.HTML(value=f"""<p style="font-family: Arial, sans-serif; font-size: 14px;">Developed by NCTC,Mumbai . Suggestions may be sent to <a href="mailto:nctc-admin@gov.in" style="color: #00008B; font-style: italic;">ramyadevi1607@yahoo.com</a>.</p>""", elem_id='Sub-heading1 ')
558
-
559
- # with gr.Column(scale=3):
560
- # gr.Image(value='logo.png',height=200,width=200)
561
-
562
-
563
- # chatbot = gr.Chatbot(
564
- # [],
565
- # elem_id="chatbot",
566
- # avatar_images=('https://aui.atlassian.com/aui/8.8/docs/images/avatar-person.svg',
567
- # 'https://huggingface.co/datasets/huggingface/brand-assets/resolve/main/hf-logo.svg'),
568
- # bubble_full_width=False,
569
- # show_copy_button=True,
570
- # show_share_button=True,
571
- # )
572
-
573
- # with gr.Row():
574
- # txt = gr.Textbox(
575
- # scale=3,
576
- # show_label=False,
577
- # placeholder="Enter text and press enter",
578
- # container=False,
579
- # )
580
- # txt_btn = gr.Button(value="Submit text", scale=1)
581
-
582
- # cross_encoder = gr.Radio(choices=['(FAST) MiniLM-L6v2','(ACCURATE) BGE reranker','(HIGH ACCURATE) ColBERT'], value='(ACCURATE) BGE reranker',label="Embeddings", info="Only First query to Colbert may take litte time)")
583
-
584
- # prompt_html = gr.HTML()
585
- # # Turn off interactivity while generating if you click
586
- # txt_msg = txt_btn.click(add_text, [chatbot, txt], [chatbot, txt], queue=False).then(
587
- # bot, [chatbot, cross_encoder], [chatbot, prompt_html])#.then(update_count_html,[],[count_html])
588
-
589
- # # Turn it back on
590
- # txt_msg.then(lambda: gr.Textbox(interactive=True), None, [txt], queue=False)
591
-
592
- # # Turn off interactivity while generating if you hit enter
593
- # txt_msg = txt.submit(add_text, [chatbot, txt], [chatbot, txt], queue=False).then(
594
- # bot, [chatbot, cross_encoder], [chatbot, prompt_html])#.then(update_count_html,[],[count_html])
595
-
596
- # # Turn it back on
597
- # txt_msg.then(lambda: gr.Textbox(interactive=True), None, [txt], queue=False)
598
-
599
- # # Examples
600
- # gr.Examples(examples, txt)
601
-
602
-
603
-
604
-
605
- # # with gr.Blocks(title="Quiz Maker", theme=gr.themes.Default(primary_hue="green", secondary_hue="green"), css="style.css") as QUIZBOT:
606
- # # with gr.Column(scale=4):
607
- # # gr.HTML("""
608
- # # <center>
609
- # # <h1><span style="color: purple;">ADWITIYA</span> Customs Manual Quizbot</h1>
610
- # # <h2>Generative AI-powered Capacity building for Training Officers</h2>
611
- # # <i>⚠️ NACIN Faculties create quiz from any topic dynamically for classroom evaluation after their sessions! ⚠️</i>
612
- # # </center>
613
- # # """)
614
-
615
- # # with gr.Column(scale=2):
616
- # # gr.HTML("""
617
- # # <center>
618
-
619
- # # <h2>Ready!</h2>
620
-
621
- # # </center>
622
- # # """)
623
- # # # load_btn = gr.Button("Click to Load!🚀")
624
- # # # load_text = gr.Textbox()
625
- # # # load_btn.click(fn=load_model, outputs=load_text)
626
-
627
- # # topic = gr.Textbox(label="Enter the Topic for Quiz", placeholder="Write any topic/details from Customs Manual")
628
-
629
- # # with gr.Row():
630
- # # radio = gr.Radio(["easy", "average", "hard"], label="How difficult should the quiz be?")
631
-
632
- # # generate_quiz_btn = gr.Button("Generate Quiz!🚀")
633
- # # quiz_msg = gr.Textbox()
634
-
635
- # # question_radios = [gr.Radio(visible=False) for _ in range(10)]
636
-
637
- # # generate_quiz_btn.click(
638
- # # fn=generate_quiz,
639
- # # inputs=[radio, topic],
640
- # # outputs=[quiz_msg] + question_radios
641
- # # )
642
-
643
- # # check_button = gr.Button("Check Score")
644
- # # score_textbox = gr.Markdown()
645
-
646
- # # check_button.click(
647
- # # fn=compare_answers,
648
- # # inputs=question_radios,
649
- # # outputs=score_textbox
650
- # # )
651
-
652
- # #demo = gr.TabbedInterface([CHATBOT, QUIZBOT], ["AI ChatBot", "AI Quizbot"])
653
- # CHATBOT.queue()
654
- # CHATBOT.launch(debug=True)
655
-
 
156
  # Create a new history entry instead of modifying the tuple directly
157
  new_history = history[:-1] + [ (query, "") ]
158
  for character in generate_fn(prompt, history[:-1]):
159
+ new_history[-1] = (query, character)
160
+ print('Character',character)# Update the last tuple with new text
161
  yield new_history, prompt_html
162
  # history[-1][1] = ""
163
  # for character in generate_fn(prompt, history[:-1]):
 
197
  response_text = history[-1][1] if history else ''
198
  translation = bhashini_translate(response_text, to_code=to_code)
199
  return translation['translated_content']
200
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
201
 
202
  # Gradio interface
203
  with gr.Blocks(theme='gradio/soft') as CHATBOT:
 
284
  # Launch the Gradio application
285
  CHATBOT.launch(share=True)
286