anycoder-a6e7b2d3 / utils.py
AiCoderv2's picture
Upload folder using huggingface_hub
76c14bf verified
"""
Utility functions for the AI Chatbot application.
"""
def format_chat_message(role: str, content: str) -> dict:
"""Format a chat message for the chatbot component."""
return {"role": role, "content": content}
def validate_message(message: str) -> bool:
"""Validate that a message is safe and appropriate."""
# Simple validation - in production you'd want more robust checks
forbidden_words = ["hate", "violence", "harm"] # Basic content filtering
return not any(word in message.lower() for word in forbidden_words)
def get_timestamp() -> str:
"""Get current timestamp for logging."""
from datetime import datetime
return datetime.now().strftime("%Y-%m-%d %H:%M:%S")
}
This AI chatbot application features:
## 🎨 Modern Design
- **Custom Soft theme** with indigo primary colors
- **Google Inter font** for clean typography
- **Responsive layout** that works on different screen sizes
- **Professional UI** with proper spacing and sizing
## πŸ€– Core Features
- **Interactive chat interface** with message history
- **Streaming responses** for natural conversation flow
- **Message actions**: Like and Retry functionality
- **Chat examples** for users to try
- **Clear chat functionality**
## πŸš€ Gradio 6 Best Practices
- βœ… **Theme in demo.launch()** - Correct Gradio 6 syntax
- βœ… **Proper event handling** with api_visibility parameter
- βœ… **Responsive components** with appropriate sizing
- βœ… **Error handling** and user feedback
- βœ… **Clean code structure** with proper documentation
## πŸ“± User Experience
- **Intuitive input** with placeholder text
- **Visual feedback** through info/warning messages
- **Copy and share** buttons for chat messages
- **Accessibility** with proper labels and descriptions
The chatbot uses simulated AI responses with pattern matching, but you can easily replace this with actual AI model integrations (OpenAI, Hugging Face, etc.).