Initial commit for Imuru
Browse files- README.md +7 -2
- app.py +194 -4
- requirements.txt +16 -0
- sample/sample.png +0 -0
README.md
CHANGED
|
@@ -1,13 +1,18 @@
|
|
| 1 |
---
|
| 2 |
title: Imuru
|
| 3 |
-
emoji:
|
| 4 |
colorFrom: purple
|
| 5 |
colorTo: gray
|
| 6 |
sdk: gradio
|
| 7 |
sdk_version: 6.1.0
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
-
license:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
short_description: 'Image-only Emuru: Autoregressive Handwriting Generation'
|
| 12 |
---
|
| 13 |
|
|
|
|
| 1 |
---
|
| 2 |
title: Imuru
|
| 3 |
+
emoji: π
|
| 4 |
colorFrom: purple
|
| 5 |
colorTo: gray
|
| 6 |
sdk: gradio
|
| 7 |
sdk_version: 6.1.0
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
+
license: apache-2.0
|
| 11 |
+
tags:
|
| 12 |
+
- handwriting-generation
|
| 13 |
+
- styled-text
|
| 14 |
+
- image-to-image
|
| 15 |
+
- autoregressive
|
| 16 |
short_description: 'Image-only Emuru: Autoregressive Handwriting Generation'
|
| 17 |
---
|
| 18 |
|
app.py
CHANGED
|
@@ -1,7 +1,197 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
|
| 4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import spaces
|
| 3 |
+
import torch
|
| 4 |
+
import json
|
| 5 |
+
from pathlib import Path
|
| 6 |
+
from PIL import Image
|
| 7 |
+
import numpy as np
|
| 8 |
|
| 9 |
+
# Global model variable
|
| 10 |
+
model_zoo = {
|
| 11 |
+
"imuru_t5_small": {
|
| 12 |
+
"repo_id": "Ruian7P/emuru_result",
|
| 13 |
+
"model_name": "imuru_t5_small_2e-5_ech5"
|
| 14 |
+
},
|
| 15 |
+
"emuru_t5_small": {
|
| 16 |
+
"repo_id": "Ruian7P/emuru_result",
|
| 17 |
+
"model_name": "emuru_t5_small_2e-5_ech5"
|
| 18 |
+
}
|
| 19 |
+
}
|
| 20 |
|
| 21 |
+
model = None
|
| 22 |
+
|
| 23 |
+
def load_model(model_name="imuru_t5_small"):
|
| 24 |
+
global model
|
| 25 |
+
|
| 26 |
+
if model is None:
|
| 27 |
+
print(f"Loading model {model_name}...")
|
| 28 |
+
from transformers import AutoModel
|
| 29 |
+
|
| 30 |
+
model = AutoModel.from_pretrained(
|
| 31 |
+
model_zoo[model_name]["repo_id"],
|
| 32 |
+
subfolder=model_zoo[model_name]["model_name"],
|
| 33 |
+
trust_remote_code=True
|
| 34 |
+
)
|
| 35 |
+
model.eval()
|
| 36 |
+
print("β
Model loaded")
|
| 37 |
+
|
| 38 |
+
return model
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
def load_examples():
|
| 42 |
+
"""Load example samples."""
|
| 43 |
+
examples = []
|
| 44 |
+
examples.append([
|
| 45 |
+
"sample/sample.png", "Ruian7P"
|
| 46 |
+
])
|
| 47 |
+
return examples
|
| 48 |
+
|
| 49 |
+
def process_image(img):
|
| 50 |
+
from torchvision.transforms import functional as F
|
| 51 |
+
img = img.convert("RGB")
|
| 52 |
+
img = img.resize((img.width * 64 // img.height, 64))
|
| 53 |
+
img = F.to_tensor(img)
|
| 54 |
+
img = F.normalize(img, [0.5], [0.5])
|
| 55 |
+
return img
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
@spaces.GPU
|
| 59 |
+
def generate_handwriting(style_image, gen_text, model_name="imuru_t5_small"):
|
| 60 |
+
"""Generate handwriting in the style of the input image."""
|
| 61 |
+
if not gen_text or gen_text.strip() == "":
|
| 62 |
+
return None, "β Please provide text to generate"
|
| 63 |
+
|
| 64 |
+
if style_image is None:
|
| 65 |
+
return None, "β Please upload a style image"
|
| 66 |
+
|
| 67 |
+
try:
|
| 68 |
+
# Convert numpy array to PIL Image if needed
|
| 69 |
+
if isinstance(style_image, np.ndarray):
|
| 70 |
+
style_image = Image.fromarray(style_image)
|
| 71 |
+
|
| 72 |
+
# Load and move model to GPU
|
| 73 |
+
loaded_model = load_model(model_name)
|
| 74 |
+
loaded_model.to("cuda")
|
| 75 |
+
|
| 76 |
+
# Preprocess style image
|
| 77 |
+
style_img = process_image(style_image).to("cuda")
|
| 78 |
+
|
| 79 |
+
# Generate
|
| 80 |
+
with torch.inference_mode():
|
| 81 |
+
result = loaded_model.generate(
|
| 82 |
+
style_image=style_img,
|
| 83 |
+
gen_text=gen_text,
|
| 84 |
+
max_new_tokens=512
|
| 85 |
+
)
|
| 86 |
+
|
| 87 |
+
return result, "β
Generation successful!"
|
| 88 |
+
|
| 89 |
+
except Exception as e:
|
| 90 |
+
import traceback
|
| 91 |
+
traceback.print_exc()
|
| 92 |
+
return None, f"β Error: {str(e)}"
|
| 93 |
+
|
| 94 |
+
|
| 95 |
+
# Custom CSS for better styling
|
| 96 |
+
custom_css = """
|
| 97 |
+
.gradio-container {
|
| 98 |
+
max-width: 1200px !important;
|
| 99 |
+
}
|
| 100 |
+
.header-text {
|
| 101 |
+
text-align: center;
|
| 102 |
+
margin-bottom: 1rem;
|
| 103 |
+
}
|
| 104 |
+
.feature-box {
|
| 105 |
+
background: linear-gradient(135deg, #1a1a2e 0%, #16213e 100%);
|
| 106 |
+
border-radius: 10px;
|
| 107 |
+
padding: 15px;
|
| 108 |
+
margin: 10px 0;
|
| 109 |
+
}
|
| 110 |
+
footer {
|
| 111 |
+
visibility: hidden;
|
| 112 |
+
}
|
| 113 |
+
"""
|
| 114 |
+
|
| 115 |
+
# Build the interface with gr.Blocks for better customization
|
| 116 |
+
with gr.Blocks(css=custom_css, title="Imuru") as demo:
|
| 117 |
+
|
| 118 |
+
# Header
|
| 119 |
+
gr.HTML("""
|
| 120 |
+
<div style="text-align: center; margin-bottom: 20px;">
|
| 121 |
+
<h1>π Imuru: Autoregressive Handwriting Generation</h1>
|
| 122 |
+
</div>
|
| 123 |
+
""")
|
| 124 |
+
|
| 125 |
+
with gr.Row():
|
| 126 |
+
with gr.Column(scale=1):
|
| 127 |
+
model_selector = gr.Dropdown(
|
| 128 |
+
label="π€ Select Model",
|
| 129 |
+
choices=list(model_zoo.keys()),
|
| 130 |
+
value="imuru_t5_small",
|
| 131 |
+
interactive=True
|
| 132 |
+
)
|
| 133 |
+
|
| 134 |
+
style_image_input = gr.Image(
|
| 135 |
+
label="πΌοΈ Style Image",
|
| 136 |
+
type="pil",
|
| 137 |
+
height=200
|
| 138 |
+
)
|
| 139 |
+
|
| 140 |
+
gen_text_input = gr.Textbox(
|
| 141 |
+
label="βοΈ Text to Generate",
|
| 142 |
+
placeholder="Enter the text you want to generate in the selected style",
|
| 143 |
+
lines=2,
|
| 144 |
+
value="Hello, I am Imuru!"
|
| 145 |
+
)
|
| 146 |
+
|
| 147 |
+
generate_btn = gr.Button("πΆ Generate", variant="primary", size="lg")
|
| 148 |
+
|
| 149 |
+
with gr.Column(scale=1):
|
| 150 |
+
output_image = gr.Image(
|
| 151 |
+
label="πΌοΈ Generated Output",
|
| 152 |
+
type="pil",
|
| 153 |
+
height=200
|
| 154 |
+
)
|
| 155 |
+
|
| 156 |
+
status_text = gr.Textbox(
|
| 157 |
+
label="π§ Status",
|
| 158 |
+
lines=1,
|
| 159 |
+
interactive=False
|
| 160 |
+
)
|
| 161 |
+
|
| 162 |
+
# Examples
|
| 163 |
+
examples = load_examples()
|
| 164 |
+
if examples:
|
| 165 |
+
gr.Examples(
|
| 166 |
+
examples=examples,
|
| 167 |
+
inputs=[style_image_input, gen_text_input],
|
| 168 |
+
label="π‘ Examples",
|
| 169 |
+
examples_per_page=4
|
| 170 |
+
)
|
| 171 |
+
|
| 172 |
+
# Connect events
|
| 173 |
+
generate_btn.click(
|
| 174 |
+
fn=generate_handwriting,
|
| 175 |
+
inputs=[model_selector, style_image_input, gen_text_input],
|
| 176 |
+
outputs=[output_image, status_text]
|
| 177 |
+
)
|
| 178 |
+
|
| 179 |
+
gen_text_input.submit(
|
| 180 |
+
fn=generate_handwriting,
|
| 181 |
+
inputs=[model_selector, style_image_input, gen_text_input],
|
| 182 |
+
outputs=[output_image, status_text]
|
| 183 |
+
)
|
| 184 |
+
|
| 185 |
+
# How to use section
|
| 186 |
+
gr.Markdown("""
|
| 187 |
+
---
|
| 188 |
+
### π§ How to Use
|
| 189 |
+
|
| 190 |
+
1. **Upload a style image**: A handwritten sample to extract style from
|
| 191 |
+
2. **Type generation text**: The text you want to generate in the style of the image
|
| 192 |
+
3. **Click Generate**: Imuru will create the handwritten text image for you!
|
| 193 |
+
""")
|
| 194 |
+
|
| 195 |
+
|
| 196 |
+
if __name__ == "__main__":
|
| 197 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Pin exact versions to avoid the API schema bug
|
| 2 |
+
gradio==4.44.1
|
| 3 |
+
gradio_client==1.3.0
|
| 4 |
+
|
| 5 |
+
# HuggingFace Spaces
|
| 6 |
+
spaces>=0.30.0
|
| 7 |
+
|
| 8 |
+
# Core dependencies
|
| 9 |
+
torch==2.7.1
|
| 10 |
+
torchvision==0.22.1
|
| 11 |
+
transformers==4.54.0
|
| 12 |
+
diffusers==0.34.0
|
| 13 |
+
einops==0.8.1
|
| 14 |
+
pillow==11.3.0
|
| 15 |
+
numpy==2.2.6
|
| 16 |
+
accelerate==1.9.0
|
sample/sample.png
ADDED
|