Spaces:
Runtime error
Runtime error
import gradio as gr | |
import random | |
import time | |
import bardapi | |
# Set your Secure-1PSID value to token | |
token = 'cQjDrFOQjkhOHHHCW54Tc7IKyphnqos-A-IpmgjVa9p3_ZFFa5Ilet-heMO8Xm3TM1Y5vw.' | |
# Initialize the BARD API client | |
bard = bardapi.core.Bard(token) | |
# Create a chatbot instance | |
chatbot = gr.Chatbot() | |
# Define the function to respond to user input | |
def respond(message, chat_history): | |
# Send the user input to BARD API for a response | |
response = bard.get_answer(message) | |
bot_message = response['answer'] | |
# Append user input and bot response to chat history | |
chat_history.append((message, bot_message)) | |
# Pause for a few seconds to simulate processing time | |
time.sleep(2) | |
return "", chat_history | |
# Create a text input box and a send button | |
msg = gr.InputBox(lines=1, label="User Input") | |
send_button = gr.Button(text="Send") | |
# Create a chat output box | |
chat_output = gr.Textbox(readonly=True, label="Chat History") | |
# When the send button is clicked, trigger the response function | |
def on_button_click(): | |
message = msg.value | |
chat_history = chat_output.value | |
new_chat_history, _ = respond(message, chat_history) | |
chat_output.update(new_chat_history) | |
send_button.onclick(on_button_click) | |
# Create a layout combining the input box, send button, and chat output box | |
layout = gr.Grid([msg, send_button], [chat_output], n_rows=2, n_cols=1) | |
# Create the interface | |
iface = gr.Interface(fn=None, inputs=layout, outputs="text") | |
# Launch the interface | |
iface.launch() |