GradioChatBot / app.py
AmadouDiaV
message
8a64278
raw
history blame
4.76 kB
# -*- coding: utf-8 -*-
"""GradioApp - Final.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/13x0cApCbqR5GKWE2nrk7DV4rcgJcO_HF
"""
import subprocess
import sys
def install(package):
subprocess.check_call([sys.executable, "-m", "pip", "install", package])
install("typing-extensions")
install("gradio")
install("keras_nlp")
from tensorflow import keras
import keras_nlp
import gradio as gr
def generate_text(model, input_text, max_length=50):
return model.generate(input_text, max_length=max_length)
preprocessor = keras_nlp.models.GPT2CausalLMPreprocessor.from_preset(
"gpt2_medium_en",
sequence_length=128,
)
gpt2_lm = keras_nlp.models.GPT2CausalLM.from_preset(
"gpt2_medium_en",
preprocessor=preprocessor,
)
gpt2_lm.load_weights('./Checkpoints')
# Gradio app
# <a href="https://www.freepik.com/icon/user_456212#fromView=search&term=avatar&track=ais&page=1&position=22&uuid=48125587-eeb5-4fe3-9eb2-f9fe7330f4fe">Icon by Freepik</a>
# <a href="https://www.freepik.com/icon/ai_2814666#fromView=search&term=robot&track=ais&page=1&position=20&uuid=58780fb9-dab6-4fb1-9928-479b2926a242">Icon by Freepik</a>
theme = gr.themes.Soft().set(
background_fill_primary='white',
background_fill_primary_dark='white',
)
with gr.Blocks(theme=theme,css="""
.gradio-container {
background-color: white;
width: 70vw;
}
#chatbot{
background-image: url("https://png.pngtree.com/thumb_back/fh260/background/20201014/pngtree-breast-cancer-awareness-pink-ribbons-background-design-image_417234.jpg");
}
#chatbot .bubble-wrap::-webkit-scrollbar {
width: 20px;
}
#chatbot .bubble-wrap::-webkit-scrollbar-thumb {
background-color: whitesmoke;
border-radius: 20px;
border: 6px solid transparent;
background-clip: content-box;
}
#chatbot .bubble-wrap::-webkit-scrollbar-thumb:hover {
background-color: grey;
}
#chatbot .bubble-wrap::-webkit-scrollbar-track {
background-color: transparent;
}
#chatbot .message p{
text-align: start;
color: white;
}
h1, p {
text-align: center;
color: black;
}
body #footer_note {
text-align: center;
font-size: x-small;
font-weight:bold;
}
.label {
display:none;
}
textarea, .gallery-item, .gallery-item:hover {
color: black;
border: 1px black solid;
background-color: white;
}
.user {
background-color: #374151;
}
.user {
background-color: #111827;
}
.gallery-item:hover {
color: white;
border: 1px black solid;
background-color: black;
}
body gradio-app {
background-color: white;
}
""") as demo:
gr.HTML(f"""
<html>
<body>
<h1>Welcome, I'm CancerBot πŸ€–</h1>
<p>Here you can ask all questions about cancer</p>
</body>
</html>
""")
def return_message(message, history, model=gpt2_lm, max_length=128):
if len(message) <= 1:
gr.Warning('Please enter a message with more than one character.')
elif len(message) > max_length:
gr.Warning(f"Input should not exceed {max_length} characters.")
else:
cancer_answer = generate_text(model, message)
message = "**You**\n" + message
history.append([message, f"**CancerBot**\n{cancer_answer}"])
return "", history
chatbot = gr.Chatbot(
height="60vh",
bubble_full_width=True,
avatar_images=(["/content/drive/MyDrive/Data/avatar.png", "/content/drive/MyDrive/Data/robot.png"]),
show_copy_button=True,
likeable=True,
layout='bubble',
elem_id='chatbot',
show_label=False,
)
with gr.Row():
input_box = gr.Textbox(placeholder="Message CancerBot...", container=False, scale=9)
submit_btn = gr.Button(value="⬆", scale=1)
submit_btn.click(return_message, [input_box, chatbot],[input_box, chatbot])
examples = gr.Examples(examples=["What is a thyroid cancer ?", "How can I know that I have a lung cancer ?",
"How many types of cancer ?"], inputs=[input_box], label="")
input_box.submit(return_message, [input_box, chatbot],[input_box, chatbot])
gr.HTML(f"""
<html>
<body>
<p id="footer_note">CancerBot is based on cancer documents. Consider checking important information.</p>
</body>
</html>
""")
demo.queue(default_concurrency_limit=34) # 32 students, 2 teachers
demo.launch(share=True,favicon_path="/content/drive/MyDrive/Data/robot.png")