Spaces:
Runtime error
Runtime error
File size: 4,770 Bytes
8a64278 7ba91c7 8a64278 7ba91c7 8a64278 51bd313 8a64278 51bd313 8a64278 51bd313 8a64278 7ba91c7 8a64278 7ba91c7 53d8312 7ba91c7 8a64278 7ba91c7 8a64278 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# -*- 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('./ouais/weight.ckpt')
# 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") |