Update app.py
Browse files2 unmodified lines345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455563 unmodified lines60616263646566672 unmodified lines# Function to generate text based on the modeldef chatbot(user_input, history):# Load the selected model (using a hard-coded model for now)model = pipeline("text-generation", model="google/mt5-base")# Get the model's responseresponse = model(user_input, max_length=50, temperature=1.0)[0]["generated_text"]# Append the user input and model's response to the historyhistory.append({"role": "user", "content": user_input})history.append({"role": "assistant", "content": response})return history # Return the updated conversation history# Main interface layout with small input and large outputdef create_chat_interface():with gr.Blocks() as demo:gr.Markdown("# Chatbot Interface")# Define the chat interfacechatbot_component = gr.Chatbot(type="messages", label="Chat History", height=500)with gr.Row(elem_id="input_container"):# Input fielduser_input = gr.Textbox(placeholder="Type your message here...", label=None, lines=1, elem_id="user_input")# Send buttonsend_button = gr.Button("📤", elem_id="send_button")# Add CSS for styling the input container and placing the button insidedemo.css = """#input_container {display: flex;align-items: center;gap: 5px;}#user_input {flex-grow: 1;border: 1px solid #ccc;border-radius: 8px;padding: 10px;}#send_button {width: 40px;height: 40px;background-color: #007bff;color: white;border: none;border-radius: 50%;cursor: pointer;display: flex;justify-content: center;3 unmodified linesbackground-color: #0056b3;}"""# Connect the input to the chatbot function via the Send buttonsend_button.click(chatbot,inputs=[user_input, chatbot_component],2 unmodified lines3456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960613 unmodified lines65666768697071722 unmodified lines# Function to generate text based on the modeldef chatbot(user_input, history):# Mock model response for demo purposesresponse = f"Echo: {user_input}"history.append({"role": "user", "content": user_input})history.append({"role": "assistant", "content": response})return history# Main interface layoutdef create_chat_interface():with gr.Blocks() as demo:gr.Markdown("# Chatbot Interface")# Chat displaychatbot_component = gr.Chatbot(type="messages", label="Chat History", height=500)# Input and button containerwith gr.Row(elem_id="input_container"):# Input field with the button insidewith gr.Box(elem_id="input_with_button"):send_button = gr.Button("📤", elem_id="send_button")user_input = gr.Textbox(placeholder="Type your message here...", label=None, lines=1, elem_id="user_input")# Custom CSS to position the send button inside the input boxdemo.css = """#input_container {display: flex;justify-content: center;margin-top: 10px;}#input_with_button {position: relative;width: 100%;max-width: 600px;}#user_input {width: 100%;padding-left: 40px; /* To make space for the button */border-radius: 8px;border: 1px solid #ccc;height: 40px;box-sizing: border-box;}#send_button {position: absolute;left: 5px;top: 5px;width: 30px;height: 30px;border-radius: 50%;background-color: #007bff;color: white;border: none;cursor: pointer;display: flex;justify-content: center;3 unmodified linesbackground-color: #0056b3;}"""# Link button click to the chatbot functionsend_button.click(chatbot,inputs=[user_input, chatbot_component],
app.py+35-30