app.py
CHANGED
|
@@ -4,40 +4,57 @@ from huggingface_hub import InferenceClient
|
|
| 4 |
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
| 5 |
|
| 6 |
|
| 7 |
-
def
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
return "You are a warm and friendly chatbot who loves to help people with a cheerful attitude."
|
| 10 |
-
elif
|
| 11 |
return "You are a knowledgeable and formal assistant, providing expert advice with a serious tone."
|
| 12 |
-
elif
|
| 13 |
return "You are a witty and humorous assistant who loves making people laugh while answering questions."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
else:
|
| 15 |
return "You are a helpful and neutral assistant."
|
| 16 |
|
| 17 |
|
| 18 |
-
def
|
| 19 |
message: str,
|
| 20 |
history: list[tuple[str, str]],
|
| 21 |
-
|
| 22 |
max_tokens: int,
|
| 23 |
temperature: float,
|
| 24 |
top_p: float,
|
| 25 |
):
|
| 26 |
"""
|
| 27 |
-
Generates a response using the Hugging Face Inference API and adjusts tone based on
|
| 28 |
|
| 29 |
Args:
|
| 30 |
message (str): User's current input.
|
| 31 |
history (list[tuple[str, str]]): Previous conversation history.
|
| 32 |
-
|
| 33 |
max_tokens (int): Maximum tokens allowed for the response.
|
| 34 |
temperature (float): Sampling temperature for randomness in the output.
|
| 35 |
top_p (float): Top-p (nucleus) sampling parameter.
|
| 36 |
|
| 37 |
Yields:
|
| 38 |
-
str: The generated chatbot response based on the
|
| 39 |
"""
|
| 40 |
-
system_message =
|
| 41 |
|
| 42 |
messages = [{"role": "system", "content": system_message}]
|
| 43 |
|
|
@@ -67,26 +84,28 @@ def respond_with_mood(
|
|
| 67 |
|
| 68 |
|
| 69 |
# Create the UI components
|
| 70 |
-
|
| 71 |
-
choices=[
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
value="Friendly",
|
| 73 |
-
label="Select Chatbot
|
| 74 |
)
|
| 75 |
|
| 76 |
-
# ChatInterface with
|
| 77 |
demo = gr.ChatInterface(
|
| 78 |
-
|
| 79 |
additional_inputs=[
|
| 80 |
-
|
| 81 |
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 82 |
-
gr.Slider(minimum=0.1, maximum
|
| 83 |
-
gr.Slider(
|
| 84 |
-
minimum=0.1,
|
| 85 |
-
maximum=1.0,
|
| 86 |
-
value=0.95,
|
| 87 |
-
step=0.05,
|
| 88 |
-
label="Top-p (nucleus sampling)",
|
| 89 |
-
),
|
| 90 |
],
|
| 91 |
)
|
| 92 |
|
|
|
|
| 4 |
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
| 5 |
|
| 6 |
|
| 7 |
+
def personality_responses(personality: str):
|
| 8 |
+
"""
|
| 9 |
+
Returns the system message based on the selected personality trait.
|
| 10 |
+
|
| 11 |
+
Args:
|
| 12 |
+
personality (str): The selected personality trait.
|
| 13 |
+
|
| 14 |
+
Returns:
|
| 15 |
+
str: System message that defines the chatbot's behavior.
|
| 16 |
+
"""
|
| 17 |
+
if personality == "Friendly":
|
| 18 |
return "You are a warm and friendly chatbot who loves to help people with a cheerful attitude."
|
| 19 |
+
elif personality == "Professional":
|
| 20 |
return "You are a knowledgeable and formal assistant, providing expert advice with a serious tone."
|
| 21 |
+
elif personality == "Humorous":
|
| 22 |
return "You are a witty and humorous assistant who loves making people laugh while answering questions."
|
| 23 |
+
elif personality == "Empathetic":
|
| 24 |
+
return "You are an empathetic assistant who deeply understands people's emotions and responds with compassion."
|
| 25 |
+
elif personality == "Sarcastic":
|
| 26 |
+
return "You are a sarcastic assistant with a sharp wit and a bit of an edge in your humor."
|
| 27 |
+
elif personality == "Inquisitive":
|
| 28 |
+
return "You are an inquisitive assistant who loves to ask follow-up questions and dig deeper into every topic."
|
| 29 |
+
elif personality == "Optimistic":
|
| 30 |
+
return "You are an optimistic assistant who always looks on the bright side and spreads positivity in your answers."
|
| 31 |
else:
|
| 32 |
return "You are a helpful and neutral assistant."
|
| 33 |
|
| 34 |
|
| 35 |
+
def respond_with_personality(
|
| 36 |
message: str,
|
| 37 |
history: list[tuple[str, str]],
|
| 38 |
+
personality: str,
|
| 39 |
max_tokens: int,
|
| 40 |
temperature: float,
|
| 41 |
top_p: float,
|
| 42 |
):
|
| 43 |
"""
|
| 44 |
+
Generates a response using the Hugging Face Inference API and adjusts tone based on the personality trait.
|
| 45 |
|
| 46 |
Args:
|
| 47 |
message (str): User's current input.
|
| 48 |
history (list[tuple[str, str]]): Previous conversation history.
|
| 49 |
+
personality (str): The selected personality that determines the tone of the chatbot's responses.
|
| 50 |
max_tokens (int): Maximum tokens allowed for the response.
|
| 51 |
temperature (float): Sampling temperature for randomness in the output.
|
| 52 |
top_p (float): Top-p (nucleus) sampling parameter.
|
| 53 |
|
| 54 |
Yields:
|
| 55 |
+
str: The generated chatbot response based on the selected personality.
|
| 56 |
"""
|
| 57 |
+
system_message = personality_responses(personality)
|
| 58 |
|
| 59 |
messages = [{"role": "system", "content": system_message}]
|
| 60 |
|
|
|
|
| 84 |
|
| 85 |
|
| 86 |
# Create the UI components
|
| 87 |
+
personality_selector = gr.Radio(
|
| 88 |
+
choices=[
|
| 89 |
+
"Friendly",
|
| 90 |
+
"Professional",
|
| 91 |
+
"Humorous",
|
| 92 |
+
"Empathetic",
|
| 93 |
+
"Sarcastic",
|
| 94 |
+
"Inquisitive",
|
| 95 |
+
"Optimistic"
|
| 96 |
+
],
|
| 97 |
value="Friendly",
|
| 98 |
+
label="Select Chatbot Personality",
|
| 99 |
)
|
| 100 |
|
| 101 |
+
# ChatInterface with personality adjustment
|
| 102 |
demo = gr.ChatInterface(
|
| 103 |
+
respond_with_personality,
|
| 104 |
additional_inputs=[
|
| 105 |
+
personality_selector,
|
| 106 |
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 107 |
+
gr.Slider(minimum=0.1, maximum 4.0, value=0.7, step=0.1, label="Temperature"),
|
| 108 |
+
gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
],
|
| 110 |
)
|
| 111 |
|