File size: 7,312 Bytes
f664cc2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import gradio as gr
import torch
import os
from transformers import AutoTokenizer, AutoModelForCausalLM
from gradio.themes.utils import colors, sizes
from gradio.themes import Soft

# Define custom themes
class GemmaLightTheme(Soft):
    def __init__(self):
        super().__init__(
            primary_hue=colors.blue,
            secondary_hue=colors.indigo,
            neutral_hue=colors.gray,
            spacing_size=sizes.spacing_md,
            radius_size=sizes.radius_md,
            text_size=sizes.text_md,
        )

class GemmaDarkTheme(Soft):
    def __init__(self):
        super().__init__(
            primary_hue=colors.blue,
            secondary_hue=colors.indigo,
            neutral_hue=colors.gray,
            spacing_size=sizes.spacing_md,
            radius_size=sizes.radius_md,
            text_size=sizes.text_md,
        )
        self.name = "gemma_dark"
        self.background_fill_primary = "#1F1F2E"
        self.background_fill_secondary = "#2A2A3C"
        self.border_color_primary = "#3A3A4C"
        self.border_color_secondary = "#4A4A5C"
        self.color_accent_soft = "#3B5FA3"
        self.color_accent = "#4B82C4"
        self.text_color = "#FFFFFF"
        self.text_color_subdued = "#CCCCCC"
        self.shadow_spread = "8px"
        self.shadow_inset = "0px 1px 2px 0px rgba(0, 0, 0, 0.1) inset"

# [Previous code for helper functions, model loading, etc., remains unchanged]

# Create Gradio interface
with gr.Blocks(theme=GemmaLightTheme()) as demo:
    # Header with theme toggle
    with gr.Row(equal_height=True):
        with gr.Column(scale=6):
            gr.Markdown(
                """
                # 🤖 Gemma Capabilities Demo
                
                This interactive demo showcases Google's Gemma model capabilities across different tasks.
                """
            )
        with gr.Column(scale=1, min_width=150):
            theme_toggle = gr.Radio(
                ["Light", "Dark"],
                value="Light",
                label="Theme",
                info="Switch between light and dark mode",
                elem_id="theme_toggle"
            )

    # Add CSS and JavaScript for themes
    gr.HTML("""
    <style>
    /* Base styles for better UI */
    .gr-group {
        box-shadow: 0 2px 5px rgba(0,0,0,0.1);
        border-radius: 10px;
        padding: 20px;
        background-color: var(--block-background-fill);
    }
    .gr-tabs {
        box-shadow: 0 2px 5px rgba(0,0,0,0.1);
        border-radius: 10px;
        overflow: hidden;
    }
    button.primary {
        transition: transform 0.2s, box-shadow 0.2s;
    }
    button.primary:hover {
        transform: translateY(-2px);
        box-shadow: 0 4px 8px rgba(0,0,0,0.2);
    }
    textarea, .input-box {
        font-size: 16px !important;
    }
    
    /* Dark theme overrides using Gradio variables */
    .dark-theme {
        --body-background-fill: #1F1F2E !important;
        --block-background-fill: #2A2A3C !important;
        --input-background-fill: #3A3A4C !important;
        --button-primary-background-fill: #4B82C4 !important;
        --button-secondary-background-fill: #3A3A4C !important;
        --border-color-primary: #4A4A5C !important;
        --body-text-color: #FFFFFF !important;
        --block-label-text-color: #CCCCCC !important;
        --input-text-color: #FFFFFF !important;
        --button-primary-text-color: #FFFFFF !important;
        --button-secondary-text-color: #FFFFFF !important;
        --shadow: 0 1px 2px rgba(0,0,0,0.5) !important;
    }
    .dark-theme .code {
        background-color: #2A2A3C !important;
        color: #FFFFFF !important;
    }
    </style>
    
    <script>
    (function() {
        function applyTheme() {
            const themeToggle = document.getElementById('theme_toggle');
            if (!themeToggle) return;
            const inputs = themeToggle.querySelectorAll('input');
            for (let input of inputs) {
                if (input.checked && input.value === 'Dark') {
                    document.body.classList.add('dark-theme');
                    return;
                }
            }
            document.body.classList.remove('dark-theme');
        }
        
        function setupObserver() {
            const themeToggle = document.getElementById('theme_toggle');
            if (!themeToggle) {
                setTimeout(setupObserver, 100);
                return;
            }
            applyTheme();
            const observer = new MutationObserver((mutations) => {
                mutations.forEach((mutation) => {
                    if (mutation.type === 'attributes' || mutation.type === 'childList') {
                        applyTheme();
                    }
                });
            });
            observer.observe(themeToggle, { attributes: true, childList: true, subtree: true });
            const inputs = themeToggle.querySelectorAll('input');
            inputs.forEach(input => input.addEventListener('change', applyTheme));
        }
        
        if (document.readyState === 'loading') {
            document.addEventListener('DOMContentLoaded', setupObserver);
        } else {
            setupObserver();
        }
    })();
    </script>
    """)

    # Authentication Section
    with gr.Group(elem_id="auth_box"):
        gr.Markdown("## 🔑 Authentication")
        with gr.Row(equal_height=True):
            with gr.Column(scale=3):
                hf_token = gr.Textbox(
                    label="Hugging Face Token",
                    placeholder="Enter your token here...",
                    type="password",
                    value=DEFAULT_HF_TOKEN,
                    info="Get your token from https://huggingface.co./settings/tokens"
                )
            with gr.Column(scale=1):
                auth_button = gr.Button("Authenticate", variant="primary")
        auth_status = gr.Markdown("Please authenticate to use the model.")
        
        def authenticate(token):
            return "⏳ Loading model... Please wait, this may take a minute."
        
        def auth_complete(token):
            return load_model(token)
        
        auth_button.click(fn=authenticate, inputs=[hf_token], outputs=[auth_status], queue=False).then(
            fn=auth_complete, inputs=[hf_token], outputs=[auth_status]
        )
        gr.Markdown(
            """
            ### How to get a token:
            1. Go to [Hugging Face Token Settings](https://huggingface.co./settings/tokens)
            2. Create a new token with read access
            3. Accept the [Gemma model license](https://huggingface.co./google/gemma-3-4b-pt)
            """
        )

    # [Remaining tabs and components remain unchanged for brevity]

    # Footer
    with gr.Group(elem_id="footer"):
        gr.Markdown(
            """
            ## About Gemma
            Gemma is a family of lightweight, state-of-the-art open models from Google...
            [Learn more about Gemma](https://huggingface.co./google/gemma-3-4b-pt)
            <div style="text-align: center; margin-top: 20px; padding: 10px;">
                <p>© 2023 Gemma Capabilities Demo | Made with ❤️ using Gradio</p>
            </div>
            """
        )

# Launch the app
demo.launch(share=False)