Pijush2023 commited on
Commit
936c67a
·
verified ·
1 Parent(s): 6df654f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +175 -64
app.py CHANGED
@@ -1481,59 +1481,123 @@ files_list = [
1481
  "178041_000197000852_V2.pdf"
1482
  ]
1483
 
 
 
 
 
 
 
 
 
 
 
 
 
1484
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1485
 
 
 
 
1486
 
1487
 
1488
- with gr.Blocks(theme='gradio/soft') as demo:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1489
 
1490
 
1491
- with gr.Row():
1492
- with gr.Column():
1493
- state = gr.State()
1494
 
1495
- # File dropdown
1496
- # file_dropdown = gr.Dropdown(label="Select Document", choices=files_list, value=None)
1497
 
1498
- chatbot = gr.Chatbot([], elem_id="RADAR:Channel 94.1", bubble_full_width=False)
1499
- choice = gr.Radio(label="Select Style", choices=["Details", "Conversational"], value="Conversational",interactive=False,visible=False)
1500
- retrieval_mode = gr.Radio(label="Retrieval Mode", choices=["VDB", "KGF"], value="VDB",interactive=False,visible=False)
1501
- model_choice = gr.Dropdown(label="Choose Model", choices=["LM-2"], value="LM-2")
1502
 
1503
- # Link the dropdown change to handle_model_choice_change
1504
- model_choice.change(fn=handle_model_choice_change, inputs=model_choice, outputs=[retrieval_mode, choice, choice])
1505
 
1506
- # gr.Markdown("<h1 style='color: red;'>Talk to RADAR</h1>", elem_id="voice-markdown")
1507
 
1508
- chat_input = gr.Textbox(show_copy_button=True, interactive=True, show_label=False, label="ASK Radar !!!")
1509
- tts_choice = gr.Radio(label="Select TTS System", choices=["Alpha", "Beta"], value="Alpha")
1510
 
1511
- retriever_button = gr.Button("Retriever")
1512
 
1513
- clear_button = gr.Button("Clear")
1514
- clear_button.click(lambda: [None, None], outputs=[chat_input, state])
1515
 
1516
- # gr.Markdown("<h1 style='color: red;'>Radar Map</h1>", elem_id="Map-Radar")
1517
- # location_output = gr.HTML()
1518
- audio_output = gr.Audio(interactive=False, autoplay=True)
1519
 
1520
- def stop_audio():
1521
- audio_output.stop()
1522
- return None
1523
 
1524
 
1525
 
1526
 
1527
 
1528
- retriever_sequence = (
1529
- retriever_button.click(fn=stop_audio, inputs=[], outputs=[audio_output], api_name="api_stop_audio_recording")
1530
- .then(fn=add_message, inputs=[chatbot, chat_input], outputs=[chatbot, chat_input], api_name="api_addprompt_chathistory")
1531
- # First, generate the bot response
1532
- .then(fn=generate_bot_response, inputs=[chatbot, choice, retrieval_mode, model_choice], outputs=[chatbot], api_name="api_generate_bot_response")
1533
- # Then, generate the TTS response based on the bot's response
1534
- .then(fn=generate_tts_response, inputs=[chatbot, tts_choice], outputs=[audio_output], api_name="api_generate_tts_response")
1535
- .then(fn=clear_textbox, inputs=[], outputs=[chat_input], api_name="api_clear_textbox")
1536
- )
1537
 
1538
 
1539
 
@@ -1542,17 +1606,17 @@ with gr.Blocks(theme='gradio/soft') as demo:
1542
 
1543
 
1544
 
1545
- chat_input.submit(fn=stop_audio, inputs=[], outputs=[audio_output], api_name="api_stop_audio_recording").then(
1546
- fn=add_message, inputs=[chatbot, chat_input], outputs=[chatbot, chat_input], api_name="api_addprompt_chathistory"
1547
- ).then(
1548
- # First, generate the bot response
1549
- fn=generate_bot_response, inputs=[chatbot, choice, retrieval_mode, model_choice], outputs=[chatbot], api_name="api_generate_bot_response"
1550
- ).then(
1551
- # Then, generate the TTS response based on the bot's response
1552
- fn=generate_tts_response, inputs=[chatbot, tts_choice], outputs=[audio_output], api_name="api_generate_tts_response"
1553
- ).then(
1554
- fn=clear_textbox, inputs=[], outputs=[chat_input], api_name="api_clear_textbox"
1555
- )
1556
 
1557
 
1558
 
@@ -1560,31 +1624,31 @@ with gr.Blocks(theme='gradio/soft') as demo:
1560
 
1561
 
1562
 
1563
- audio_input = gr.Audio(sources=["microphone"], streaming=True, type='numpy', every=0.1)
1564
- audio_input.stream(transcribe_function, inputs=[state, audio_input], outputs=[state, chat_input], api_name="api_voice_to_text")
1565
 
1566
- # gr.Markdown("<h1 style='color: red;'>Example Prompts</h1>", elem_id="Example-Prompts")
1567
- # gr.Examples(examples=examples, fn=insert_prompt,inputs=chat_input, outputs=chat_input)
1568
 
1569
- # with gr.Column():
1570
- # weather_output = gr.HTML(value=fetch_local_weather())
1571
- # news_output = gr.HTML(value=fetch_local_news())
1572
- # events_output = gr.HTML(value=fetch_local_events())
1573
 
1574
- # with gr.Column():
1575
 
1576
 
1577
- # # Call update_images during the initial load to display images when the interface appears
1578
- # initial_images = update_images()
1579
 
1580
- # # Displaying the images generated using Flux API directly
1581
- # image_output_1 = gr.Image(value=initial_images[0], label="Image 1", elem_id="flux_image_1", width=400, height=400)
1582
- # image_output_2 = gr.Image(value=initial_images[1], label="Image 2", elem_id="flux_image_2", width=400, height=400)
1583
- # image_output_3 = gr.Image(value=initial_images[2], label="Image 3", elem_id="flux_image_3", width=400, height=400)
1584
 
1585
- # # Refresh button to update images
1586
- # refresh_button = gr.Button("Refresh Images")
1587
- # refresh_button.click(fn=update_images, inputs=None, outputs=[image_output_1, image_output_2, image_output_3])
1588
 
1589
 
1590
 
@@ -1592,10 +1656,57 @@ with gr.Blocks(theme='gradio/soft') as demo:
1592
 
1593
 
1594
 
1595
- demo.queue()
1596
- demo.launch(show_error=True)
 
 
 
 
 
 
1597
 
 
 
 
 
 
 
 
 
 
 
1598
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1599
 
1600
 
1601
 
 
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
 
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
 
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
 
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
+ # HTML component for displaying the dynamic table
1684
+ table_output = gr.HTML()
1685
+
1686
+ # Audio output for TTS
1687
+ audio_output = gr.Audio(interactive=False, autoplay=True)
1688
+
1689
+ # Retriever sequence
1690
+ retriever_sequence = (
1691
+ retriever_button.click(fn=stop_audio, inputs=[], outputs=[audio_output], api_name="api_stop_audio_recording")
1692
+ .then(fn=add_message, inputs=[chatbot, chat_input], outputs=[chatbot, chat_input], api_name="api_addprompt_chathistory")
1693
+ .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")
1694
+ .then(fn=generate_tts_response, inputs=[chatbot, tts_choice], outputs=[audio_output], api_name="api_generate_tts_response")
1695
+ .then(fn=clear_textbox, inputs=[], outputs=[chat_input], api_name="api_clear_textbox")
1696
+ )
1697
+
1698
+ chat_input.submit(fn=stop_audio, inputs=[], outputs=[audio_output], api_name="api_stop_audio_recording").then(
1699
+ fn=add_message, inputs=[chatbot, chat_input], outputs=[chatbot, chat_input], api_name="api_addprompt_chathistory"
1700
+ ).then(
1701
+ 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"
1702
+ ).then(
1703
+ fn=generate_tts_response, inputs=[chatbot, tts_choice], outputs=[audio_output], api_name="api_generate_tts_response"
1704
+ ).then(
1705
+ fn=clear_textbox, inputs=[], outputs=[chat_input], api_name="api_clear_textbox"
1706
+ )
1707
+
1708
+ demo.queue()
1709
+ demo.launch(show_error=True)
1710
 
1711
 
1712