bot / app.py
Antoine245's picture
Update app.py
c0cbfd7
raw
history blame
1.56 kB
import gradio as gr
import os
import time
import google.generativeai as palm
palm.configure(api_key=os.environ.get("palm_key"))
defaults = {
'model': 'models/chat-bison-001',
'temperature': 0.25,
'candidate_count': 1,
'top_k': 40,
'top_p': 0.95,
}
context = "Your IT assistant"
examples = [
[
"Hey my computer is broken",
"Hey, what is the issue with your computer?"
]
]
# user_message = ['']
history = ['']
with gr.Blocks(theme=gr.themes.Soft()) as demo:
chatbot = gr.Chatbot()
msg = gr.Textbox()
btn = gr.Button("Submit",variant="primary")
clear = gr.ClearButton([msg, chatbot])
def user(user_message, history):
return gr.update(value=""), history + [[user_message,None]]
# , interactive=False
def bot(history):
# Remove the second element of the last element of the list
# del history[-1][1]
bot_message = palm.chat(
context=context,
examples=examples,
messages=history[-1][0]
)
history[-1][1] = ""
for character in bot_message.last:
history[-1][1] += character
time.sleep(0.005)
yield history
response = msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
bot, chatbot, chatbot
)
response = btn.click(user, [msg, chatbot], [msg, chatbot], queue=False).then(
bot, chatbot, chatbot
)
# response.then(lambda: gr.update(interactive=True), None, [msg], queue=False)
demo.queue()
demo.launch()