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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import os
|
| 3 |
+
from pathlib import Path
|
| 4 |
+
|
| 5 |
+
def load_css():
|
| 6 |
+
"""Load CSS content for the application"""
|
| 7 |
+
return """
|
| 8 |
+
/* Main wrapper styling */
|
| 9 |
+
#main-wrapper {
|
| 10 |
+
min-height: 100vh;
|
| 11 |
+
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%;
|
| 12 |
+
position: relative;
|
| 13 |
+
}
|
| 14 |
+
|
| 15 |
+
/* Header row styling */
|
| 16 |
+
#header-row {
|
| 17 |
+
background: rgba(255, 255, 255, 0.1);
|
| 18 |
+
backdrop-filter: blur(10px);
|
| 19 |
+
border-bottom: 1px solid rgba(255, 255, 255, 0.2);
|
| 20 |
+
padding: 1rem;
|
| 21 |
+
border-radius: 0 0 15px 15px;
|
| 22 |
+
margin-bottom: 2rem;
|
| 23 |
+
}
|
| 24 |
+
|
| 25 |
+
/* Theme column styling */
|
| 26 |
+
#theme-col {
|
| 27 |
+
background: rgba(255, 255, 255, 0.05);
|
| 28 |
+
}
|
| 29 |
+
|
| 30 |
+
/* Theme button styling */
|
| 31 |
+
#theme-btn {
|
| 32 |
+
background: linear-gradient(45deg, #ff6b6b, #ee5a52);
|
| 33 |
+
border: none;
|
| 34 |
+
border-radius: 10px;
|
| 35 |
+
color: white;
|
| 36 |
+
font-weight: 600;
|
| 37 |
+
transition: all 0.3s ease;
|
| 38 |
+
}
|
| 39 |
+
|
| 40 |
+
#theme-btn:hover {
|
| 41 |
+
background: linear-gradient(45deg, #ee5a52, #ff6b6b);
|
| 42 |
+
transform: translateY(-2px);
|
| 43 |
+
box-shadow: 0 4px 15px rgba(255, 107, 107, 0.4);
|
| 44 |
+
}
|
| 45 |
+
|
| 46 |
+
/* Footer area styling */
|
| 47 |
+
#footer-area {
|
| 48 |
+
background: rgba(0, 0, 0, 0.1);
|
| 49 |
+
backdrop-filter: blur(10px);
|
| 50 |
+
padding: 0.5rem;
|
| 51 |
+
border-radius: 8px;
|
| 52 |
+
margin-top: 2rem;
|
| 53 |
+
}
|
| 54 |
+
|
| 55 |
+
/* Minecraft chat styling */
|
| 56 |
+
#minecraft-chat .message {
|
| 57 |
+
border-radius: 12px;
|
| 58 |
+
margin: 0.5rem 0;
|
| 59 |
+
padding: 0.75rem;
|
| 60 |
+
}
|
| 61 |
+
|
| 62 |
+
#minecraft-input {
|
| 63 |
+
background: rgba(255, 255, 255, 0.1);
|
| 64 |
+
}
|
| 65 |
+
|
| 66 |
+
/* Menu button styling */
|
| 67 |
+
.menu-btn {
|
| 68 |
+
background: rgba(255, 255, 255, 0.15);
|
| 69 |
+
border: 1px solid rgba(255, 255, 255, 0.25);
|
| 70 |
+
border-radius: 20px;
|
| 71 |
+
font-family: 'Minecraft', monospace;
|
| 72 |
+
color: white;
|
| 73 |
+
border: none;
|
| 74 |
+
transition: all 0.3s ease;
|
| 75 |
+
}
|
| 76 |
+
|
| 77 |
+
.menu-btn:hover {
|
| 78 |
+
background: rgba(255, 255, 255, 0.25);
|
| 79 |
+
}
|
| 80 |
+
|
| 81 |
+
#minecraft-send-btn {
|
| 82 |
+
background: linear-gradient(45deg, #4ecdc4, #44a08d);
|
| 83 |
+
transform: scale(1.05);
|
| 84 |
+
}
|
| 85 |
+
|
| 86 |
+
/* Crafting table styling */
|
| 87 |
+
#crafting-table {
|
| 88 |
+
max-width: 300px;
|
| 89 |
+
border-radius: 15px;
|
| 90 |
+
box-shadow: 0 8px 32px rgba(31, 38, 135, 0.1);
|
| 91 |
+
}
|
| 92 |
+
"""
|
| 93 |
+
|
| 94 |
+
def chat_wrapper(message, chat_history):
|
| 95 |
+
"""Wrapper function for chat interactions"""
|
| 96 |
+
if not message:
|
| 97 |
+
return message, chat_history
|
| 98 |
+
|
| 99 |
+
# Add user message
|
| 100 |
+
chat_history.append({"role": "user", "content": message})
|
| 101 |
+
|
| 102 |
+
# Simulate Steve's response
|
| 103 |
+
steve_response = f"Ah, {message}! Das klingt nach einem guten Projekt!"
|
| 104 |
+
|
| 105 |
+
# Add assistant message
|
| 106 |
+
chat_history.append({"role": "assistant", "content": steve_response})
|
| 107 |
+
|
| 108 |
+
return "", chat_history
|
| 109 |
+
|
| 110 |
+
def main():
|
| 111 |
+
css_content = load_css()
|
| 112 |
+
|
| 113 |
+
# Custom theme for Gradio 6
|
| 114 |
+
custom_theme = gr.themes.Soft(
|
| 115 |
+
primary_hue="blue",
|
| 116 |
+
secondary_hue="indigo",
|
| 117 |
+
neutral_hue="slate",
|
| 118 |
+
font=gr.themes.GoogleFont("Inter"),
|
| 119 |
+
text_size="lg",
|
| 120 |
+
spacing_size="lg",
|
| 121 |
+
radius_size="md"
|
| 122 |
+
).set(
|
| 123 |
+
button_primary_background_fill="*primary_600",
|
| 124 |
+
button_primary_background_fill_hover="*primary_700",
|
| 125 |
+
block_title_text_weight="600",
|
| 126 |
+
button_primary_text_color="white"
|
| 127 |
+
)
|
| 128 |
+
|
| 129 |
+
# JavaScript commands for theme switching
|
| 130 |
+
js_set_default = """
|
| 131 |
+
function(x) {
|
| 132 |
+
document.documentElement.setAttribute('data-theme', 'default');
|
| 133 |
+
document.body.style.setProperty('--gradio-theme', 'default');
|
| 134 |
+
}
|
| 135 |
+
|
| 136 |
+
js_set_night = """
|
| 137 |
+
function(x) {
|
| 138 |
+
document.documentElement.setAttribute('data-theme', 'night');
|
| 139 |
+
return x;
|
| 140 |
+
}
|
| 141 |
+
"""
|
| 142 |
+
|
| 143 |
+
js_set_forest = """
|
| 144 |
+
function(x) {
|
| 145 |
+
document.documentElement.setAttribute('data-theme', 'forest');
|
| 146 |
+
return x;
|
| 147 |
+
"""
|
| 148 |
+
|
| 149 |
+
# Gradio 6 - NO parameters in gr.Blocks() constructor!
|
| 150 |
+
with gr.Blocks() as demo:
|
| 151 |
+
# Status variable for menu (Open/Closed)
|
| 152 |
+
menu_visible = gr.State(False)
|
| 153 |
+
|
| 154 |
+
# Wrapper
|
| 155 |
+
with gr.Column(elem_id="main-wrapper"):
|
| 156 |
+
# Header Row
|
| 157 |
+
with gr.Row(elem_id="header-row"):
|
| 158 |
+
# Left column: Button + Menu
|
| 159 |
+
with gr.Column(scale=1, min_width=150, elem_id="theme-col"):
|
| 160 |
+
theme_btn = gr.Button("Themes", elem_id="theme-btn")
|
| 161 |
+
|
| 162 |
+
# The menu (Initially invisible)
|
| 163 |
+
with gr.Group(visible=False, elem_id="theme-menu-group") as theme_menu:
|
| 164 |
+
btn_default = gr.Button("Tag", size="sm", elem_classes=["menu-btn"])
|
| 165 |
+
btn_night = gr.Button("Nacht", size="sm", elem_classes=["menu-btn"])
|
| 166 |
+
btn_forest = gr.Button("Wald", size="sm", elem_classes=["menu-btn"])
|
| 167 |
+
|
| 168 |
+
with gr.Column(scale=4):
|
| 169 |
+
pass # Spacer
|
| 170 |
+
|
| 171 |
+
with gr.Column(scale=1, min_width=80, elem_id="crafting-col"):
|
| 172 |
+
gr.Image(
|
| 173 |
+
value="./chatbot/assets/crafting_table.png",
|
| 174 |
+
elem_id="crafting-table",
|
| 175 |
+
show_label=False,
|
| 176 |
+
show_download_button=False,
|
| 177 |
+
container=False,
|
| 178 |
+
interactive=False
|
| 179 |
+
)
|
| 180 |
+
|
| 181 |
+
# Chat Area
|
| 182 |
+
chatbot = gr.Chatbot(
|
| 183 |
+
value=[],
|
| 184 |
+
type="messages",
|
| 185 |
+
elem_id="minecraft-chat",
|
| 186 |
+
show_label=False,
|
| 187 |
+
avatar_images=("./chatbot/assets/avatar_images/human.png", "./chatbot/assets/avatar_images/steve.png"),
|
| 188 |
+
bubble_full_width=False,
|
| 189 |
+
)
|
| 190 |
+
|
| 191 |
+
# Footer
|
| 192 |
+
with gr.Group(elem_id="footer-area"):
|
| 193 |
+
with gr.Row(elem_id="input-row"):
|
| 194 |
+
msg_box = gr.Textbox(
|
| 195 |
+
placeholder="Was möchtest du craften?",
|
| 196 |
+
show_label=False,
|
| 197 |
+
container=False,
|
| 198 |
+
elem_id="minecraft-input",
|
| 199 |
+
autofocus=True,
|
| 200 |
+
scale=1
|
| 201 |
+
)
|
| 202 |
+
send_btn = gr.Button("Senden", elem_id="minecraft-send-btn", scale=0)
|
| 203 |
+
|
| 204 |
+
# --- EVENT LOGIK FOR THE MENU ---
|
| 205 |
+
|
| 206 |
+
# 1. Menu open/close
|
| 207 |
+
def toggle_menu(is_visible):
|
| 208 |
+
return not is_visible, gr.update(visible=not is_visible)
|
| 209 |
+
|
| 210 |
+
theme_btn.click(
|
| 211 |
+
fn=toggle_menu,
|
| 212 |
+
inputs=[menu_visible],
|
| 213 |
+
outputs=[menu_visible, theme_menu],
|
| 214 |
+
api_visibility="public"
|
| 215 |
+
)
|
| 216 |
+
|
| 217 |
+
# 2. Theme Buttons: Execute JS AND close the menu
|
| 218 |
+
btn_default.click(
|
| 219 |
+
fn=None,
|
| 220 |
+
inputs=None,
|
| 221 |
+
outputs=[menu_visible, theme_menu],
|
| 222 |
+
js=js_set_default,
|
| 223 |
+
api_visibility="public"
|
| 224 |
+
).then(
|
| 225 |
+
fn=lambda: (False, gr.update(visible=False)),
|
| 226 |
+
outputs=[menu_visible, theme_menu],
|
| 227 |
+
api_visibility="public"
|
| 228 |
+
)
|
| 229 |
+
|
| 230 |
+
btn_night.click(
|
| 231 |
+
fn=None,
|
| 232 |
+
inputs=None,
|
| 233 |
+
outputs=[menu_visible, theme_menu],
|
| 234 |
+
js=js_set_night,
|
| 235 |
+
api_visibility="public"
|
| 236 |
+
).then(
|
| 237 |
+
fn=lambda: (False, gr.update(visible=False)),
|
| 238 |
+
outputs=[menu_visible, theme_menu],
|
| 239 |
+
api_visibility="public"
|
| 240 |
+
)
|
| 241 |
+
|
| 242 |
+
btn_forest.click(
|
| 243 |
+
fn=None,
|
| 244 |
+
inputs=None,
|
| 245 |
+
outputs=[menu_visible, theme_menu],
|
| 246 |
+
api_visibility="public"
|
| 247 |
+
)
|
| 248 |
+
|
| 249 |
+
# --- CHAT EVENTS ---
|
| 250 |
+
msg_box.submit(
|
| 251 |
+
chat_wrapper,
|
| 252 |
+
inputs=[msg_box, chatbot],
|
| 253 |
+
outputs=[msg_box, chatbot],
|
| 254 |
+
api_visibility="public"
|
| 255 |
+
)
|
| 256 |
+
|
| 257 |
+
send_btn.click(
|
| 258 |
+
chat_wrapper,
|
| 259 |
+
inputs=[msg_box, chatbot],
|
| 260 |
+
outputs=[msg_box, chatbot],
|
| 261 |
+
api_visibility="public"
|
| 262 |
+
)
|
| 263 |
+
|
| 264 |
+
# Gradio 6 - ALL app parameters go in launch()!
|
| 265 |
+
demo.launch(
|
| 266 |
+
inbrowser=True,
|
| 267 |
+
allowed_paths=["./chatbot/assets"],
|
| 268 |
+
theme=custom_theme,
|
| 269 |
+
css=css_content,
|
| 270 |
+
footer_links=[{"label": "Built with anycoder", "url": "https://huggingface.co/spaces/akhaliq/anycoder"}]
|
| 271 |
+
)
|
| 272 |
+
|
| 273 |
+
if __name__ == "__main__":
|
| 274 |
+
main()
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio>=6.0
|
| 2 |
+
requests
|
| 3 |
+
Pillow
|
utils.py
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Utility functions for the Gradio application
|
| 3 |
+
"""
|
| 4 |
+
|
| 5 |
+
def load_css_file(file_path):
|
| 6 |
+
"""Load CSS from a file"""
|
| 7 |
+
try:
|
| 8 |
+
with open(file_path, 'r', encoding='utf-8') as f:
|
| 9 |
+
return f.read()
|
| 10 |
+
except FileNotFoundError:
|
| 11 |
+
return ""
|
| 12 |
+
|
| 13 |
+
def validate_theme(theme_name):
|
| 14 |
+
"""Validate if a theme name is supported"""
|
| 15 |
+
supported_themes = ["default", "night", "forest"]
|
| 16 |
+
return theme_name in supported_themes
|
| 17 |
+
|
| 18 |
+
def get_theme_colors(theme_name):
|
| 19 |
+
"""Get color scheme for a specific theme"""
|
| 20 |
+
theme_colors = {
|
| 21 |
+
"default": {"primary": "#667eea", "secondary": "#764ba2"},
|
| 22 |
+
"night": {"primary": "#2d3748", "secondary": "#4a5568"},
|
| 23 |
+
"forest": {"primary": "#276749", "secondary": "#38a169"},
|
| 24 |
+
}
|
| 25 |
+
return theme_colors.get(theme_name, {"primary": "#667eea", "secondary": "#764ba2"}},
|
| 26 |
+
|
| 27 |
+
return theme_colors.get(theme_name, {"primary": "#667eea", "secondary": "#764ba2"}
|
| 28 |
+
|
| 29 |
+
This Gradio 6 application features:
|
| 30 |
+
|
| 31 |
+
**Modern UI Design:**
|
| 32 |
+
- Custom Soft theme with blue primary colors
|
| 33 |
+
- Google Fonts integration (Inter)
|
| 34 |
+
- Custom CSS with glass morphism effects
|
| 35 |
+
- Theme switching functionality via JavaScript
|
| 36 |
+
- Minecraft-inspired chat interface
|
| 37 |
+
- Responsive layout with columns and rows
|
| 38 |
+
|
| 39 |
+
**Gradio 6 Features:**
|
| 40 |
+
- ✅ Proper Gradio 6 syntax with `gr.Blocks()` (no parameters)
|
| 41 |
+
- ✅ All app parameters in `demo.launch()`
|
| 42 |
+
- ✅ Theme customization with primary/secondary colors
|
| 43 |
+
- ✅ CSS integration for custom styling
|
| 44 |
+
- ✅ JavaScript execution for dynamic behavior
|
| 45 |
+
- ✅ Footer with "Built with anycoder" link
|
| 46 |
+
|
| 47 |
+
**Key Components:**
|
| 48 |
+
- Theme selector with multiple options
|
| 49 |
+
- Chatbot with custom avatars
|
| 50 |
+
- Minecraft crafting table image
|
| 51 |
+
- Theme switching with data attributes
|
| 52 |
+
- Proper event handling with api_visibility
|
| 53 |
+
|
| 54 |
+
The application will launch in the browser with a professional, modern interface that follows all Gradio 6 best practices.
|