File size: 2,797 Bytes
1fa7a3d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
from gradio_awsbr_mmchatbot import MultiModalChatbot
from gradio.data_classes import FileData
from bedrock_utils import MultimodalInputHandler


# A function to call the multi-modal input for Anthropic Claude v3 sonnet using Bedrock boto3
async def get_response(text, file):
    # If there is a file uploaded, then we will send it to Anthropic Claude v3 sonnet.
    # If there is no file uploaded, then we will send the text to Anthropic Claude v3 sonnet.
    try:
        userMsg = {
            "text": text,
            "files": [{"file": FileData(path=file)}]
        }
    except:
        userMsg = {
            "text": text,
            "files": []
        }
    # Define a variable to store the response from the Anthropic Claude v3 sonnet
    llmResponse = ""
    handler = MultimodalInputHandler(text, file)
    # Loop through the response from Anthropic Claude v3 sonnet, and append it to our llmResponse variable.
    async for x in handler.handleInput():
        llmResponse += x
        yield [[userMsg, {"text": llmResponse, "files": []}]]
    # Yield the response from Anthropic Claude v3 sonnet. This is unecessary as we can just yield the llmResponse variable in an iterative fashion as above.
    # But just in case.... let's yield the entire response object as well and overwrite the messages in the Chatbot.
    response = {
        "text": llmResponse,
        "files": []
    }
    yield [[userMsg, response]]

# Defining Gradio Interface using Blocks Structure
with gr.Blocks() as demo:
    # Give it a Title
    gr.Markdown("## Gradio - MultiModal Chatbot")
    # Define the Chat Tab
    with gr.Tab(label="Chat"):
        with gr.Row():
            with gr.Column(scale=3):
                # Set a variable equal to our MultiModalChatBot class
                chatBot = MultiModalChatbot(height=700, render_markdown=True, bubble_full_width=True)
        with gr.Row():
            with gr.Column(scale=3):
                # Set a variable equal to our user message
                msg = gr.Textbox(placeholder='What is the meaning of life?', show_label=False)
            with gr.Column(scale=1):
                # Set a variable equal to our file upload
                fileInput = gr.File(label="Upload Files")
            with gr.Column(scale=1):
                # Define our submit button and invoke our 'get_response' function when it's clicked.
                gr.Button('Submit', variant='primary').click(get_response, inputs=[msg,fileInput], outputs=chatBot)
                # Same function as above, but with the 'enter' key being pressed inside the gr.Textbox() component instead of the 'submit' button.
                msg.submit(get_response, inputs=[msg, fileInput], outputs=chatBot)


if __name__ == '__main__':
    demo.queue().launch()