Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| from huggingface_hub import InferenceClient | |
| import random | |
| client = InferenceClient("HuggingFaceH4/zephyr-7b-beta") | |
| # List of motivational quotes | |
| quotes = [ | |
| "When anger rises, think of the consequences.", | |
| "You cannot control the wind, but you can adjust your sails.", | |
| "Breathe in calm, breathe out tension.", | |
| "Peace begins with a single thought.", | |
| "Don't let a moment of anger become a lifetime of regret." | |
| ] | |
| quote_of_the_day = random.choice(quotes) | |
| def respond(message, history, system_message, max_tokens, temperature, top_p): | |
| lowered = message.lower() | |
| if "angry" in lowered: | |
| yield "I'm sorry you're feeling angry. Let's take a deep breath together. Would you like to try a calming exercise?" | |
| return | |
| elif "sad" in lowered: | |
| yield "It's okay to feel sad sometimes. I'm here to support you. π" | |
| return | |
| elif "stressed" in lowered: | |
| yield "Let's pause and take a deep breath. I'm here for you." | |
| return | |
| elif "calm" in lowered: | |
| yield "That's great to hear! Stay present and peaceful. πΏ" | |
| return | |
| messages = [{"role": "system", "content": system_message}] | |
| for user_msg, bot_msg in history: | |
| if user_msg: | |
| messages.append({"role": "user", "content": user_msg}) | |
| if bot_msg: | |
| messages.append({"role": "assistant", "content": bot_msg}) | |
| messages.append({"role": "user", "content": message}) | |
| response = "" | |
| for msg in client.chat_completion( | |
| messages, | |
| max_tokens=max_tokens, | |
| stream=True, | |
| temperature=temperature, | |
| top_p=top_p, | |
| ): | |
| token = msg.choices[0].delta.content | |
| response += token | |
| yield response | |
| # UI layout | |
| demo = gr.ChatInterface( | |
| respond, | |
| additional_inputs=[ | |
| gr.Textbox( | |
| value="You are a calming and supportive emotional wellness assistant named Aggro.", | |
| label="System message", | |
| visible=False | |
| ), | |
| gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"), | |
| gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"), | |
| gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"), | |
| ], | |
| title="π Meet Aggro - Your Emotion Companion", | |
| description=f"π¬ Quote of the Day: _{quote_of_the_day}_" | |
| ) | |
| if __name__ == "__main__": | |
| demo.launch() | |