|
import gradio as gr |
|
from gradio_awsbr_mmchatbot import MultiModalChatbot |
|
from gradio.data_classes import FileData |
|
from bedrock_utils import MultimodalInputHandler |
|
|
|
|
|
|
|
async def get_response(text, file): |
|
|
|
|
|
try: |
|
userMsg = { |
|
"text": text, |
|
"files": [{"file": FileData(path=file)}] |
|
} |
|
except: |
|
userMsg = { |
|
"text": text, |
|
"files": [] |
|
} |
|
|
|
llmResponse = "" |
|
|
|
async for x in MultimodalInputHandler(text, file).handleInput(): |
|
llmResponse += x |
|
yield [[userMsg, {"text": llmResponse, "files": []}]] |
|
|
|
|
|
""" |
|
response = { |
|
"text": llmResponse, |
|
"files": [] |
|
} |
|
yield [[userMsg, response]] |
|
""" |
|
|
|
|
|
with gr.Blocks() as demo: |
|
|
|
gr.Markdown("## Gradio - MultiModal Chatbot") |
|
|
|
with gr.Tab(label="Chat"): |
|
with gr.Row(): |
|
with gr.Column(scale=3): |
|
|
|
chatBot = MultiModalChatbot(height=700, render_markdown=True, bubble_full_width=True) |
|
with gr.Row(): |
|
with gr.Column(scale=3): |
|
|
|
msg = gr.Textbox(placeholder='What is the meaning of life?', show_label=False) |
|
with gr.Column(scale=1): |
|
|
|
fileInput = gr.File(label="Upload Files") |
|
with gr.Column(scale=1): |
|
|
|
gr.Button('Submit', variant='primary').click(get_response, inputs=[msg,fileInput], outputs=chatBot) |
|
|
|
msg.submit(get_response, inputs=[msg, fileInput], outputs=chatBot) |
|
|
|
|
|
if __name__ == '__main__': |
|
demo.queue().launch(share=True) |