Pijush2023 commited on
Commit
dd4345a
·
verified ·
1 Parent(s): 805734f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +64 -177
app.py CHANGED
@@ -1481,123 +1481,61 @@ files_list = [
1481
  "178041_000197000852_V2.pdf"
1482
  ]
1483
 
1484
- def generate_table_html(responses):
1485
- # HTML structure for the table
1486
- table_html = """
1487
- <h2>Top 3 Responses</h2>
1488
- <table style="width:100%; border-collapse: collapse; border: 1px solid black;">
1489
- <tr>
1490
- <th style="border: 1px solid black; padding: 8px;">Container (Document Name)</th>
1491
- <th style="border: 1px solid black; padding: 8px;">Page Number</th>
1492
- <th style="border: 1px solid black; padding: 8px;">Response</th>
1493
- <th style="border: 1px solid black; padding: 8px;">Metadata</th>
1494
- </tr>
1495
- """
1496
 
1497
- for response in responses[:3]: # Limiting to top 3 responses
1498
- document_name = response.get("document_name", "Unknown")
1499
- page_number = response.get("page_number", "Unknown")
1500
- content = response.get("content", "No content available")
1501
- metadata = response.get("metadata", "No metadata available")
1502
-
1503
- # Adding rows dynamically to the table
1504
- table_html += f"""
1505
- <tr>
1506
- <td style="border: 1px solid black; padding: 8px;">{document_name}</td>
1507
- <td style="border: 1px solid black; padding: 8px;">{page_number}</td>
1508
- <td style="border: 1px solid black; padding: 8px;">{content}</td>
1509
- <td style="border: 1px solid black; padding: 8px;">{metadata}</td>
1510
- </tr>
1511
- """
1512
 
1513
- # Closing the table
1514
- table_html += "</table>"
1515
- return table_html
1516
 
1517
 
1518
- def generate_bot_response_with_table(history, choice, retrieval_mode, model_choice):
1519
- if not history:
1520
- return
1521
 
1522
- # Select the model
1523
- selected_model = chat_model if model_choice == "LM-1" else (chat_model1 if model_choice == "LM-3" else phi_pipe)
1524
 
1525
- response, addresses = generate_answer(history[-1][0], choice, retrieval_mode, selected_model)
1526
-
1527
- # Extract relevant information from the response to create table content
1528
- responses = []
1529
- for doc in response[:3]: # Limiting to top 3 results
1530
- responses.append({
1531
- "document_name": doc.get("document_name", "Unknown"),
1532
- "page_number": doc.get("page_number", "Unknown"),
1533
- "content": doc.get("content", "No content available"),
1534
- "metadata": doc.get("metadata", "No metadata available")
1535
- })
1536
-
1537
- # Generate the HTML for the table
1538
- table_html = generate_table_html(responses)
1539
-
1540
- history[-1][1] = ""
1541
-
1542
- for character in response:
1543
- history[-1][1] += character
1544
- yield history, table_html # Yielding history and table HTML for real-time updates
1545
-
1546
- yield history, table_html # Final yield with the complete response and table
1547
-
1548
-
1549
-
1550
-
1551
-
1552
- # with gr.Blocks(theme='gradio/soft') as demo:
1553
 
1554
 
1555
- # with gr.Row():
1556
- # with gr.Column():
1557
- # state = gr.State()
1558
 
1559
- # # File dropdown
1560
- # # file_dropdown = gr.Dropdown(label="Select Document", choices=files_list, value=None)
1561
 
1562
- # chatbot = gr.Chatbot([], elem_id="RADAR:Channel 94.1", bubble_full_width=False)
1563
- # choice = gr.Radio(label="Select Style", choices=["Details", "Conversational"], value="Conversational",interactive=False,visible=False)
1564
- # retrieval_mode = gr.Radio(label="Retrieval Mode", choices=["VDB", "KGF"], value="VDB",interactive=False,visible=False)
1565
- # model_choice = gr.Dropdown(label="Choose Model", choices=["LM-2"], value="LM-2")
1566
 
1567
- # # Link the dropdown change to handle_model_choice_change
1568
- # model_choice.change(fn=handle_model_choice_change, inputs=model_choice, outputs=[retrieval_mode, choice, choice])
1569
 
1570
- # # gr.Markdown("<h1 style='color: red;'>Talk to RADAR</h1>", elem_id="voice-markdown")
1571
 
1572
- # chat_input = gr.Textbox(show_copy_button=True, interactive=True, show_label=False, label="ASK Radar !!!")
1573
- # tts_choice = gr.Radio(label="Select TTS System", choices=["Alpha", "Beta"], value="Alpha")
1574
 
1575
- # retriever_button = gr.Button("Retriever")
1576
 
1577
- # clear_button = gr.Button("Clear")
1578
- # clear_button.click(lambda: [None, None], outputs=[chat_input, state])
1579
 
1580
- # # gr.Markdown("<h1 style='color: red;'>Radar Map</h1>", elem_id="Map-Radar")
1581
- # # location_output = gr.HTML()
1582
- # audio_output = gr.Audio(interactive=False, autoplay=True)
1583
 
1584
- # def stop_audio():
1585
- # audio_output.stop()
1586
- # return None
1587
 
1588
 
1589
 
1590
 
1591
 
1592
- # retriever_sequence = (
1593
- # retriever_button.click(fn=stop_audio, inputs=[], outputs=[audio_output], api_name="api_stop_audio_recording")
1594
- # .then(fn=add_message, inputs=[chatbot, chat_input], outputs=[chatbot, chat_input], api_name="api_addprompt_chathistory")
1595
- # # First, generate the bot response
1596
- # .then(fn=generate_bot_response, inputs=[chatbot, choice, retrieval_mode, model_choice], outputs=[chatbot], api_name="api_generate_bot_response")
1597
- # # Then, generate the TTS response based on the bot's response
1598
- # .then(fn=generate_tts_response, inputs=[chatbot, tts_choice], outputs=[audio_output], api_name="api_generate_tts_response")
1599
- # .then(fn=clear_textbox, inputs=[], outputs=[chat_input], api_name="api_clear_textbox")
1600
- # )
1601
 
1602
 
1603
 
@@ -1606,17 +1544,17 @@ def generate_bot_response_with_table(history, choice, retrieval_mode, model_choi
1606
 
1607
 
1608
 
1609
- # chat_input.submit(fn=stop_audio, inputs=[], outputs=[audio_output], api_name="api_stop_audio_recording").then(
1610
- # fn=add_message, inputs=[chatbot, chat_input], outputs=[chatbot, chat_input], api_name="api_addprompt_chathistory"
1611
- # ).then(
1612
- # # First, generate the bot response
1613
- # fn=generate_bot_response, inputs=[chatbot, choice, retrieval_mode, model_choice], outputs=[chatbot], api_name="api_generate_bot_response"
1614
- # ).then(
1615
- # # Then, generate the TTS response based on the bot's response
1616
- # fn=generate_tts_response, inputs=[chatbot, tts_choice], outputs=[audio_output], api_name="api_generate_tts_response"
1617
- # ).then(
1618
- # fn=clear_textbox, inputs=[], outputs=[chat_input], api_name="api_clear_textbox"
1619
- # )
1620
 
1621
 
1622
 
@@ -1624,31 +1562,31 @@ def generate_bot_response_with_table(history, choice, retrieval_mode, model_choi
1624
 
1625
 
1626
 
1627
- # audio_input = gr.Audio(sources=["microphone"], streaming=True, type='numpy', every=0.1)
1628
- # audio_input.stream(transcribe_function, inputs=[state, audio_input], outputs=[state, chat_input], api_name="api_voice_to_text")
1629
 
1630
- # # gr.Markdown("<h1 style='color: red;'>Example Prompts</h1>", elem_id="Example-Prompts")
1631
- # # gr.Examples(examples=examples, fn=insert_prompt,inputs=chat_input, outputs=chat_input)
1632
 
1633
- # # with gr.Column():
1634
- # # weather_output = gr.HTML(value=fetch_local_weather())
1635
- # # news_output = gr.HTML(value=fetch_local_news())
1636
- # # events_output = gr.HTML(value=fetch_local_events())
1637
 
1638
- # # with gr.Column():
1639
 
1640
 
1641
- # # # Call update_images during the initial load to display images when the interface appears
1642
- # # initial_images = update_images()
1643
 
1644
- # # # Displaying the images generated using Flux API directly
1645
- # # image_output_1 = gr.Image(value=initial_images[0], label="Image 1", elem_id="flux_image_1", width=400, height=400)
1646
- # # image_output_2 = gr.Image(value=initial_images[1], label="Image 2", elem_id="flux_image_2", width=400, height=400)
1647
- # # image_output_3 = gr.Image(value=initial_images[2], label="Image 3", elem_id="flux_image_3", width=400, height=400)
1648
 
1649
- # # # Refresh button to update images
1650
- # # refresh_button = gr.Button("Refresh Images")
1651
- # # refresh_button.click(fn=update_images, inputs=None, outputs=[image_output_1, image_output_2, image_output_3])
1652
 
1653
 
1654
 
@@ -1656,62 +1594,11 @@ def generate_bot_response_with_table(history, choice, retrieval_mode, model_choi
1656
 
1657
 
1658
 
1659
- # demo.queue()
1660
- # demo.launch(show_error=True)
1661
-
1662
- with gr.Blocks(theme='gradio/soft') as demo:
1663
-
1664
- with gr.Row():
1665
- with gr.Column():
1666
- state = gr.State()
1667
-
1668
- chatbot = gr.Chatbot([], elem_id="RADAR:Channel 94.1", bubble_full_width=False)
1669
- choice = gr.Radio(label="Select Style", choices=["Details", "Conversational"], value="Conversational",interactive=False,visible=False)
1670
- retrieval_mode = gr.Radio(label="Retrieval Mode", choices=["VDB", "KGF"], value="VDB",interactive=False,visible=False)
1671
- model_choice = gr.Dropdown(label="Choose Model", choices=["LM-2"], value="LM-2")
1672
-
1673
- # Link the dropdown change to handle_model_choice_change
1674
- model_choice.change(fn=handle_model_choice_change, inputs=model_choice, outputs=[retrieval_mode, choice, choice])
1675
-
1676
- chat_input = gr.Textbox(show_copy_button=True, interactive=True, show_label=False, label="ASK Radar !!!")
1677
- tts_choice = gr.Radio(label="Select TTS System", choices=["Alpha", "Beta"], value="Alpha")
1678
-
1679
- retriever_button = gr.Button("Retriever")
1680
- clear_button = gr.Button("Clear")
1681
- clear_button.click(lambda: [None, None], outputs=[chat_input, state])
1682
-
1683
- def stop_audio():
1684
- audio_output.stop()
1685
- return None
1686
-
1687
- # HTML component for displaying the dynamic table
1688
- table_output = gr.HTML()
1689
-
1690
- # Audio output for TTS
1691
- audio_output = gr.Audio(interactive=False, autoplay=True)
1692
-
1693
- # Retriever sequence
1694
- retriever_sequence = (
1695
- retriever_button.click(fn=stop_audio, inputs=[], outputs=[audio_output], api_name="api_stop_audio_recording")
1696
- .then(fn=add_message, inputs=[chatbot, chat_input], outputs=[chatbot, chat_input], api_name="api_addprompt_chathistory")
1697
- .then(fn=generate_bot_response_with_table, inputs=[chatbot, choice, retrieval_mode, model_choice], outputs=[chatbot, table_output], api_name="api_generate_bot_response_with_table")
1698
- .then(fn=generate_tts_response, inputs=[chatbot, tts_choice], outputs=[audio_output], api_name="api_generate_tts_response")
1699
- .then(fn=clear_textbox, inputs=[], outputs=[chat_input], api_name="api_clear_textbox")
1700
- )
1701
-
1702
- chat_input.submit(fn=stop_audio, inputs=[], outputs=[audio_output], api_name="api_stop_audio_recording").then(
1703
- fn=add_message, inputs=[chatbot, chat_input], outputs=[chatbot, chat_input], api_name="api_addprompt_chathistory"
1704
- ).then(
1705
- fn=generate_bot_response_with_table, inputs=[chatbot, choice, retrieval_mode, model_choice], outputs=[chatbot, table_output], api_name="api_generate_bot_response_with_table"
1706
- ).then(
1707
- fn=generate_tts_response, inputs=[chatbot, tts_choice], outputs=[audio_output], api_name="api_generate_tts_response"
1708
- ).then(
1709
- fn=clear_textbox, inputs=[], outputs=[chat_input], api_name="api_clear_textbox"
1710
- )
1711
-
1712
  demo.queue()
1713
  demo.launch(show_error=True)
1714
 
1715
 
1716
 
1717
 
 
 
 
1481
  "178041_000197000852_V2.pdf"
1482
  ]
1483
 
 
 
 
 
 
 
 
 
 
 
 
 
1484
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1485
 
 
 
 
1486
 
1487
 
 
 
 
1488
 
 
 
1489
 
1490
+ with gr.Blocks(theme='gradio/soft') as demo:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1491
 
1492
 
1493
+ with gr.Row():
1494
+ with gr.Column():
1495
+ state = gr.State()
1496
 
1497
+ # File dropdown
1498
+ # file_dropdown = gr.Dropdown(label="Select Document", choices=files_list, value=None)
1499
 
1500
+ chatbot = gr.Chatbot([], elem_id="RADAR:Channel 94.1", bubble_full_width=False)
1501
+ choice = gr.Radio(label="Select Style", choices=["Details", "Conversational"], value="Conversational",interactive=False,visible=False)
1502
+ retrieval_mode = gr.Radio(label="Retrieval Mode", choices=["VDB", "KGF"], value="VDB",interactive=False,visible=False)
1503
+ model_choice = gr.Dropdown(label="Choose Model", choices=["LM-2"], value="LM-2")
1504
 
1505
+ # Link the dropdown change to handle_model_choice_change
1506
+ model_choice.change(fn=handle_model_choice_change, inputs=model_choice, outputs=[retrieval_mode, choice, choice])
1507
 
1508
+ # gr.Markdown("<h1 style='color: red;'>Talk to RADAR</h1>", elem_id="voice-markdown")
1509
 
1510
+ chat_input = gr.Textbox(show_copy_button=True, interactive=True, show_label=False, label="ASK Radar !!!")
1511
+ tts_choice = gr.Radio(label="Select TTS System", choices=["Alpha", "Beta"], value="Alpha")
1512
 
1513
+ retriever_button = gr.Button("Retriever")
1514
 
1515
+ clear_button = gr.Button("Clear")
1516
+ clear_button.click(lambda: [None, None], outputs=[chat_input, state])
1517
 
1518
+ # gr.Markdown("<h1 style='color: red;'>Radar Map</h1>", elem_id="Map-Radar")
1519
+ # location_output = gr.HTML()
1520
+ audio_output = gr.Audio(interactive=False, autoplay=True)
1521
 
1522
+ def stop_audio():
1523
+ audio_output.stop()
1524
+ return None
1525
 
1526
 
1527
 
1528
 
1529
 
1530
+ retriever_sequence = (
1531
+ retriever_button.click(fn=stop_audio, inputs=[], outputs=[audio_output], api_name="api_stop_audio_recording")
1532
+ .then(fn=add_message, inputs=[chatbot, chat_input], outputs=[chatbot, chat_input], api_name="api_addprompt_chathistory")
1533
+ # First, generate the bot response
1534
+ .then(fn=generate_bot_response, inputs=[chatbot, choice, retrieval_mode, model_choice], outputs=[chatbot], api_name="api_generate_bot_response")
1535
+ # Then, generate the TTS response based on the bot's response
1536
+ .then(fn=generate_tts_response, inputs=[chatbot, tts_choice], outputs=[audio_output], api_name="api_generate_tts_response")
1537
+ .then(fn=clear_textbox, inputs=[], outputs=[chat_input], api_name="api_clear_textbox")
1538
+ )
1539
 
1540
 
1541
 
 
1544
 
1545
 
1546
 
1547
+ chat_input.submit(fn=stop_audio, inputs=[], outputs=[audio_output], api_name="api_stop_audio_recording").then(
1548
+ fn=add_message, inputs=[chatbot, chat_input], outputs=[chatbot, chat_input], api_name="api_addprompt_chathistory"
1549
+ ).then(
1550
+ # First, generate the bot response
1551
+ fn=generate_bot_response, inputs=[chatbot, choice, retrieval_mode, model_choice], outputs=[chatbot], api_name="api_generate_bot_response"
1552
+ ).then(
1553
+ # Then, generate the TTS response based on the bot's response
1554
+ fn=generate_tts_response, inputs=[chatbot, tts_choice], outputs=[audio_output], api_name="api_generate_tts_response"
1555
+ ).then(
1556
+ fn=clear_textbox, inputs=[], outputs=[chat_input], api_name="api_clear_textbox"
1557
+ )
1558
 
1559
 
1560
 
 
1562
 
1563
 
1564
 
1565
+ audio_input = gr.Audio(sources=["microphone"], streaming=True, type='numpy', every=0.1)
1566
+ audio_input.stream(transcribe_function, inputs=[state, audio_input], outputs=[state, chat_input], api_name="api_voice_to_text")
1567
 
1568
+ # gr.Markdown("<h1 style='color: red;'>Example Prompts</h1>", elem_id="Example-Prompts")
1569
+ # gr.Examples(examples=examples, fn=insert_prompt,inputs=chat_input, outputs=chat_input)
1570
 
1571
+ # with gr.Column():
1572
+ # weather_output = gr.HTML(value=fetch_local_weather())
1573
+ # news_output = gr.HTML(value=fetch_local_news())
1574
+ # events_output = gr.HTML(value=fetch_local_events())
1575
 
1576
+ # with gr.Column():
1577
 
1578
 
1579
+ # # Call update_images during the initial load to display images when the interface appears
1580
+ # initial_images = update_images()
1581
 
1582
+ # # Displaying the images generated using Flux API directly
1583
+ # image_output_1 = gr.Image(value=initial_images[0], label="Image 1", elem_id="flux_image_1", width=400, height=400)
1584
+ # image_output_2 = gr.Image(value=initial_images[1], label="Image 2", elem_id="flux_image_2", width=400, height=400)
1585
+ # image_output_3 = gr.Image(value=initial_images[2], label="Image 3", elem_id="flux_image_3", width=400, height=400)
1586
 
1587
+ # # Refresh button to update images
1588
+ # refresh_button = gr.Button("Refresh Images")
1589
+ # refresh_button.click(fn=update_images, inputs=None, outputs=[image_output_1, image_output_2, image_output_3])
1590
 
1591
 
1592
 
 
1594
 
1595
 
1596
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1597
  demo.queue()
1598
  demo.launch(show_error=True)
1599
 
1600
 
1601
 
1602
 
1603
+
1604
+