Upload folder using huggingface_hub
Browse files- README.md +16 -6
- app.py +72 -0
- requirements.txt +4 -0
README.md
CHANGED
|
@@ -1,13 +1,23 @@
|
|
| 1 |
---
|
| 2 |
-
title:
|
| 3 |
-
emoji:
|
| 4 |
-
colorFrom:
|
| 5 |
-
colorTo:
|
| 6 |
sdk: gradio
|
| 7 |
sdk_version: 6.17.3
|
| 8 |
-
python_version: '3.13'
|
| 9 |
app_file: app.py
|
| 10 |
pinned: false
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
---
|
| 12 |
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
---
|
| 2 |
+
title: DiffusionGemma 26B Chat
|
| 3 |
+
emoji: ๐
|
| 4 |
+
colorFrom: blue
|
| 5 |
+
colorTo: purple
|
| 6 |
sdk: gradio
|
| 7 |
sdk_version: 6.17.3
|
|
|
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
+
license: apache-2.0
|
| 11 |
+
suggested_hardware: zero-a10g
|
| 12 |
+
models:
|
| 13 |
+
- google/diffusiongemma-26B-A4B-it
|
| 14 |
---
|
| 15 |
|
| 16 |
+
# DiffusionGemma 26B Chat
|
| 17 |
+
|
| 18 |
+
Chat demo for [google/diffusiongemma-26B-A4B-it](https://huggingface.co/google/diffusiongemma-26B-A4B-it),
|
| 19 |
+
a 26B Mixture-of-Experts (3.8B active) multimodal model that generates text via
|
| 20 |
+
discrete block diffusion โ denoising 256-token blocks in parallel instead of
|
| 21 |
+
generating token by token.
|
| 22 |
+
|
| 23 |
+
Supports text and image inputs. Runs on ZeroGPU.
|
app.py
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import spaces
|
| 3 |
+
import torch
|
| 4 |
+
from transformers import AutoProcessor, DiffusionGemmaForBlockDiffusion
|
| 5 |
+
|
| 6 |
+
MODEL_ID = "google/diffusiongemma-26B-A4B-it"
|
| 7 |
+
|
| 8 |
+
processor = AutoProcessor.from_pretrained(MODEL_ID)
|
| 9 |
+
model = DiffusionGemmaForBlockDiffusion.from_pretrained(
|
| 10 |
+
MODEL_ID,
|
| 11 |
+
dtype="auto",
|
| 12 |
+
device_map="auto",
|
| 13 |
+
)
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
def build_messages(message, history):
|
| 17 |
+
messages = []
|
| 18 |
+
for turn in history:
|
| 19 |
+
content = turn["content"]
|
| 20 |
+
if isinstance(content, str):
|
| 21 |
+
messages.append(
|
| 22 |
+
{"role": turn["role"], "content": [{"type": "text", "text": content}]}
|
| 23 |
+
)
|
| 24 |
+
else:
|
| 25 |
+
# Gradio stores uploaded files in history as (filepath,) tuples
|
| 26 |
+
for path in content:
|
| 27 |
+
messages.append(
|
| 28 |
+
{"role": turn["role"], "content": [{"type": "image", "image": path}]}
|
| 29 |
+
)
|
| 30 |
+
|
| 31 |
+
user_content = [{"type": "image", "image": f} for f in message.get("files", [])]
|
| 32 |
+
user_content.append({"type": "text", "text": message["text"]})
|
| 33 |
+
messages.append({"role": "user", "content": user_content})
|
| 34 |
+
return messages
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
@spaces.GPU(duration=120)
|
| 38 |
+
def respond(message, history, max_new_tokens):
|
| 39 |
+
messages = build_messages(message, history)
|
| 40 |
+
inputs = processor.apply_chat_template(
|
| 41 |
+
messages,
|
| 42 |
+
tokenize=True,
|
| 43 |
+
add_generation_prompt=True,
|
| 44 |
+
return_dict=True,
|
| 45 |
+
return_tensors="pt",
|
| 46 |
+
).to(model.device)
|
| 47 |
+
input_len = inputs["input_ids"].shape[-1]
|
| 48 |
+
|
| 49 |
+
with torch.inference_mode():
|
| 50 |
+
output = model.generate(**inputs, max_new_tokens=max_new_tokens)
|
| 51 |
+
|
| 52 |
+
return processor.decode(output[0][input_len:], skip_special_tokens=True)
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
demo = gr.ChatInterface(
|
| 56 |
+
respond,
|
| 57 |
+
type="messages",
|
| 58 |
+
multimodal=True,
|
| 59 |
+
title="DiffusionGemma 26B-A4B-it",
|
| 60 |
+
description=(
|
| 61 |
+
"Chat with [google/diffusiongemma-26B-A4B-it]"
|
| 62 |
+
"(https://huggingface.co/google/diffusiongemma-26B-A4B-it) โ a 26B MoE "
|
| 63 |
+
"(3.8B active) model that generates text via discrete block diffusion. "
|
| 64 |
+
"Supports image input."
|
| 65 |
+
),
|
| 66 |
+
additional_inputs=[
|
| 67 |
+
gr.Slider(minimum=256, maximum=2048, value=512, step=256, label="Max new tokens"),
|
| 68 |
+
],
|
| 69 |
+
)
|
| 70 |
+
|
| 71 |
+
if __name__ == "__main__":
|
| 72 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
git+https://github.com/huggingface/transformers.git
|
| 2 |
+
torch
|
| 3 |
+
accelerate
|
| 4 |
+
spaces
|