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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from huggingface_hub import InferenceClient
|
| 3 |
+
from transformers import pipeline
|
| 4 |
+
|
| 5 |
+
# Initialize the emotion classifier
|
| 6 |
+
classifier = pipeline("text-classification", model='bhadresh-savani/distilbert-base-uncased-emotion', return_all_scores=True)
|
| 7 |
+
|
| 8 |
+
# Define the function for emotion detection
|
| 9 |
+
def detect_emotions(emotion_input):
|
| 10 |
+
prediction = classifier(emotion_input)
|
| 11 |
+
output = {emotion["label"]: emotion["score"] for emotion in prediction[0]}
|
| 12 |
+
return output
|
| 13 |
+
|
| 14 |
+
# Examples for the emotion detector
|
| 15 |
+
examples = [["I am happy that I gifted my son a robot"], ["Sorry for being late"]]
|
| 16 |
+
|
| 17 |
+
# CSS to hide footer and customize button
|
| 18 |
+
css = """
|
| 19 |
+
footer {display:none !important}
|
| 20 |
+
.output-markdown{display:none !important}
|
| 21 |
+
|
| 22 |
+
.gr-button-primary {
|
| 23 |
+
z-index: 14;
|
| 24 |
+
height: 43px;
|
| 25 |
+
width: 130px;
|
| 26 |
+
left: 0px;
|
| 27 |
+
top: 0px;
|
| 28 |
+
padding: 0px;
|
| 29 |
+
cursor: pointer !important;
|
| 30 |
+
background: none rgb(17, 20, 45) !important;
|
| 31 |
+
border: none !important;
|
| 32 |
+
text-align: center !important;
|
| 33 |
+
font-family: Poppins !important;
|
| 34 |
+
font-size: 14px !important;
|
| 35 |
+
font-weight: 500 !important;
|
| 36 |
+
color: rgb(255, 255, 255) !important;
|
| 37 |
+
line-height: 1 !important;
|
| 38 |
+
border-radius: 12px !important;
|
| 39 |
+
transition: box-shadow 200ms ease 0s, background 200ms ease 0s !important;
|
| 40 |
+
box-shadow: none !important;
|
| 41 |
+
}
|
| 42 |
+
.gr-button-primary:hover {
|
| 43 |
+
z-index: 14;
|
| 44 |
+
height: 43px;
|
| 45 |
+
width: 130px;
|
| 46 |
+
left: 0px;
|
| 47 |
+
top: 0px;
|
| 48 |
+
padding: 0px;
|
| 49 |
+
cursor: pointer !important;
|
| 50 |
+
background: none rgb(66, 133, 244) !important;
|
| 51 |
+
border: none !important;
|
| 52 |
+
text-align: center !important;
|
| 53 |
+
font-family: Poppins !important;
|
| 54 |
+
font-size: 14px !important;
|
| 55 |
+
font-weight: 500 !important;
|
| 56 |
+
color: rgb(255, 255, 255) !important;
|
| 57 |
+
line-height: 1 !important;
|
| 58 |
+
border-radius: 12px !important;
|
| 59 |
+
transition: box-shadow 200ms ease 0s, background 200ms ease 0s !important;
|
| 60 |
+
box-shadow: rgb(0 0 0 / 23%) 0px 1px 7px 0px !important;
|
| 61 |
+
}
|
| 62 |
+
.hover\:bg-orange-50:hover {
|
| 63 |
+
--tw-bg-opacity: 1 !important;
|
| 64 |
+
background-color: rgb(229,225,255) !important;
|
| 65 |
+
}
|
| 66 |
+
|
| 67 |
+
.to-orange-200 {
|
| 68 |
+
--tw-gradient-to: rgb(37 56 133 / 37%) !important;
|
| 69 |
+
}
|
| 70 |
+
|
| 71 |
+
.from-orange-400 {
|
| 72 |
+
--tw-gradient-from: rgb(17, 20, 45) !important;
|
| 73 |
+
--tw-gradient-to: rgb(255 150 51 / 0);
|
| 74 |
+
--tw-gradient-stops: var(--tw-gradient-from), var(--tw-gradient-to) !important;
|
| 75 |
+
}
|
| 76 |
+
|
| 77 |
+
.group-hover\:from-orange-500 {
|
| 78 |
+
--tw-gradient-from:rgb(17, 20, 45) !important;
|
| 79 |
+
--tw-gradient-to: rgb(37 56 133 / 37%);
|
| 80 |
+
--tw-gradient-stops: var(--tw-gradient-from), var(--tw-gradient-to) !important;
|
| 81 |
+
}
|
| 82 |
+
|
| 83 |
+
.group:hover .group-hover\:text-orange-500 {
|
| 84 |
+
--tw-text-opacity: 1 !important;
|
| 85 |
+
color:rgb(37 56 133 / var(--tw-text-opacity)) !important;
|
| 86 |
+
}
|
| 87 |
+
"""
|
| 88 |
+
|
| 89 |
+
# Initialize the InferenceClient for chatbot
|
| 90 |
+
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
| 91 |
+
|
| 92 |
+
# Define the function for chatbot response
|
| 93 |
+
def respond(
|
| 94 |
+
message,
|
| 95 |
+
history,
|
| 96 |
+
system_message,
|
| 97 |
+
max_tokens,
|
| 98 |
+
temperature,
|
| 99 |
+
top_p,
|
| 100 |
+
):
|
| 101 |
+
messages = [{"role": "system", "content": system_message}]
|
| 102 |
+
|
| 103 |
+
for val in history:
|
| 104 |
+
if val[0]:
|
| 105 |
+
messages.append({"role": "user", "content": val[0]})
|
| 106 |
+
if val[1]:
|
| 107 |
+
messages.append({"role": "assistant", "content": val[1]})
|
| 108 |
+
|
| 109 |
+
messages.append({"role": "user", "content": message})
|
| 110 |
+
|
| 111 |
+
response = ""
|
| 112 |
+
|
| 113 |
+
for message in client.chat_completion(
|
| 114 |
+
messages,
|
| 115 |
+
max_tokens=max_tokens,
|
| 116 |
+
stream=True,
|
| 117 |
+
temperature=temperature,
|
| 118 |
+
top_p=top_p,
|
| 119 |
+
):
|
| 120 |
+
token = message.choices[0].delta.content
|
| 121 |
+
response += token
|
| 122 |
+
yield response
|
| 123 |
+
|
| 124 |
+
def send_message(message, history, system_message, max_tokens, temperature, top_p):
|
| 125 |
+
if message:
|
| 126 |
+
history.append((message, ""))
|
| 127 |
+
response = respond(
|
| 128 |
+
message=message,
|
| 129 |
+
history=history,
|
| 130 |
+
system_message=system_message,
|
| 131 |
+
max_tokens=max_tokens,
|
| 132 |
+
temperature=temperature,
|
| 133 |
+
top_p=top_p,
|
| 134 |
+
)
|
| 135 |
+
response_text = ""
|
| 136 |
+
for r in response:
|
| 137 |
+
response_text = r
|
| 138 |
+
history[-1] = (message, response_text)
|
| 139 |
+
return history, gr.update(value="")
|
| 140 |
+
|
| 141 |
+
# Description for the chatbot
|
| 142 |
+
description = """
|
| 143 |
+
Hello! I'm here to support you emotionally and answer any questions. How are you feeling today?
|
| 144 |
+
<div style='color: green;'>Developed by Hashir Ehtisham</div>
|
| 145 |
+
"""
|
| 146 |
+
|
| 147 |
+
# Define the Gradio Blocks interface
|
| 148 |
+
with gr.Blocks(css=css) as demo:
|
| 149 |
+
with gr.Tab("Emotional Support Chatbot"):
|
| 150 |
+
gr.Markdown("# Emotional Support Chatbot")
|
| 151 |
+
gr.Markdown(description)
|
| 152 |
+
|
| 153 |
+
system_message = gr.Textbox(value="You are a friendly Emotional Support Chatbot.", visible=False)
|
| 154 |
+
chatbot = gr.Chatbot()
|
| 155 |
+
msg = gr.Textbox(label="Your message")
|
| 156 |
+
clear = gr.Button("Clear")
|
| 157 |
+
|
| 158 |
+
with gr.Accordion("Additional Inputs", open=False):
|
| 159 |
+
max_tokens = gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens")
|
| 160 |
+
temperature = gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature")
|
| 161 |
+
top_p = gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)")
|
| 162 |
+
|
| 163 |
+
def respond_wrapper(message, chat_history, system_message_val, max_tokens_val, temperature_val, top_p_val):
|
| 164 |
+
chat_history, _ = send_message(
|
| 165 |
+
message=message,
|
| 166 |
+
history=chat_history,
|
| 167 |
+
system_message=system_message_val,
|
| 168 |
+
max_tokens=max_tokens_val,
|
| 169 |
+
temperature=temperature_val,
|
| 170 |
+
top_p=top_p_val,
|
| 171 |
+
)
|
| 172 |
+
return gr.update(value=""), chat_history
|
| 173 |
+
|
| 174 |
+
msg.submit(respond_wrapper, [msg, chatbot, system_message, max_tokens, temperature, top_p], [msg, chatbot])
|
| 175 |
+
clear.click(lambda: None, None, chatbot, queue=False)
|
| 176 |
+
|
| 177 |
+
with gr.Tab("Emotions Detector"):
|
| 178 |
+
gr.Interface(
|
| 179 |
+
fn=detect_emotions,
|
| 180 |
+
inputs=gr.Textbox(placeholder="Enter text here", label="Input"),
|
| 181 |
+
outputs=gr.Label(label="Emotion"),
|
| 182 |
+
title="Emotion Detector ",
|
| 183 |
+
examples=examples
|
| 184 |
+
)
|
| 185 |
+
|
| 186 |
+
if __name__ == "__main__":
|
| 187 |
+
demo.launch()
|