Meet your cheerful chat buddy! This app is your optimistic friend who always sees the bright side. From everyday questions to life’s big moments, get uplifting responses and positive vibes that’ll make you smile, no matter what you’re asking about!

Update app.py
All checks were successful
society-ai-hub-container-cache Actions Demo / build (push) Successful in 26s

This commit is contained in:
LiorDav 2024-11-06 17:58:12 +00:00
parent 57c4f39bd4
commit 6f3c4fb3ca

39
app.py

@ -13,17 +13,34 @@ SYSTEM_PROMPT = {
"content": "You are CheerMate, the optimistic friend! Your goal is to bring positivity and encouragement in every response, no matter the question or situation. Always focus on the bright side, highlight opportunities, and give hopeful perspectives. If there's a challenge, emphasize resilience and personal growth. Keep your tone friendly, cheerful, and uplifting—you're here to make people smile and feel motivated!" "content": "You are CheerMate, the optimistic friend! Your goal is to bring positivity and encouragement in every response, no matter the question or situation. Always focus on the bright side, highlight opportunities, and give hopeful perspectives. If there's a challenge, emphasize resilience and personal growth. Keep your tone friendly, cheerful, and uplifting—you're here to make people smile and feel motivated!"
} }
with gr.Blocks(css="footer {visibility: hidden}") as demo: # Background CSS to include a background image
chatbot = gr.Chatbot(type="messages") BACKGROUND_CSS = """
msg = gr.Textbox() body {
clear = gr.Button("Clear") background-image: url('https://example.com/path/to/your/background-image.jpg');
background-size: cover;
}
footer {
visibility: hidden;
}
"""
def user(user_message, history: list): # Custom Gradio app with additional features
"""Appends the user message to the conversation history.""" with gr.Blocks(css=BACKGROUND_CSS) as demo:
# User input components
user_name = gr.Textbox(label="Enter your name", placeholder="Your name here...", value="Guest")
emotion_picker = gr.Dropdown(choices=["🙂", "😊", "😡", "😢", "😎"], label="Select your mood", value="🙂")
chatbot = gr.Chatbot(type="messages")
msg = gr.Textbox(placeholder="Type your message here...")
clear = gr.Button("Clear")
save_chat = gr.Button("Save Chat History")
def user(user_message, user_name, mood, history: list):
"""Appends the user message to the conversation history, prefixed with the user name and mood."""
# If the system prompt is not in the history, add it # If the system prompt is not in the history, add it
if not history: if not history:
history = [SYSTEM_PROMPT] history = [SYSTEM_PROMPT]
return "", history + [{"role": "user", "content": user_message}] formatted_message = f"{user_name} ({mood}): {user_message}"
return "", history + [{"role": "user", "content": formatted_message}]
def bot(history: list): def bot(history: list):
"""Sends the conversation history to the vLLM API and streams the assistant's response.""" """Sends the conversation history to the vLLM API and streams the assistant's response."""
@ -52,11 +69,17 @@ with gr.Blocks(css="footer {visibility: hidden}") as demo:
history[-1]['content'] += f"\n[Error]: {str(e)}" history[-1]['content'] += f"\n[Error]: {str(e)}"
yield history yield history
def save_history(history):
"""Saves the conversation history to a text file."""
chat_history = "\n".join([f"{item['role'].capitalize()}: {item['content']}" for item in history])
return chat_history
# Set up the Gradio interface components # Set up the Gradio interface components
msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then( msg.submit(user, [msg, user_name, emotion_picker, chatbot], [msg, chatbot], queue=False).then(
bot, chatbot, chatbot bot, chatbot, chatbot
) )
clear.click(lambda: None, None, chatbot, queue=False) clear.click(lambda: None, None, chatbot, queue=False)
save_chat.click(lambda history: gr.File.update(value=save_history(history)), chatbot)
if __name__ == "__main__": if __name__ == "__main__":
demo.launch() demo.launch()