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(""" """) # 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)
© 2023 Gemma Capabilities Demo | Made with ❤️ using Gradio