Playground / app.py
NotYuSheng's picture
Update app.py
2125ad2 verified
import gradio as gr
import os
from multimodal_ai import MultimodalAI
examples = [
{"text": "What is this image?", "files": ["sample/ducky.jpg"]},
{"text": "Can you summarize and translate this?", "files": ["sample/sermon.mp4"]}
]
# Function to print the like/dislike data
def print_like_dislike(x: gr.LikeData):
print(x.index, x.value, x.liked)
# Function to add a message to the chat history
def add_message(history, message):
for x in message["files"]:
history.append(((x,), None))
if message["text"] is not None:
history.append((message["text"], None))
return history, gr.MultimodalTextbox(value=None, interactive=False), message["text"]
# Function to generate the bot's response
def bot(history, user_input):
response = ai_assistant.generate_response(user_input)
if history:
history[-1][1] = response
return history
def main():
# Define the Gradio interface using Blocks
with gr.Blocks(fill_height=True) as webApp:
chatbot = gr.Chatbot(
elem_id="chatbot",
bubble_full_width=False,
scale=1,
)
with gr.Column(scale=8):
chat_input = gr.MultimodalTextbox(interactive=True,
#file_count="multiple",
placeholder="Enter message or upload file...",
show_label=False)
user_input = gr.State()
chat_msg = chat_input.submit(add_message, [chatbot, chat_input], [chatbot, chat_input, user_input])
bot_msg = chat_msg.then(bot, [chatbot, user_input], chatbot, api_name="bot_response")
bot_msg.then(lambda: gr.MultimodalTextbox(interactive=True), None, [chat_input])
chatbot.like(print_like_dislike, None, None)
with gr.Column(scale=3):
# Add examples
gr.Examples(
examples=examples,
inputs=chat_input,
label="Click on an example to try it"
)
# Start the queue and launch the interface
webApp.queue()
webApp.launch(share=True)
if __name__ == "__main__":
# Initialize llm
ai_assistant = MultimodalAI()
main()