File size: 1,470 Bytes
16e855a
 
721ea1b
16e855a
 
 
dbe4aa6
16e855a
dbe4aa6
 
 
16e855a
dbe4aa6
16e855a
 
 
 
 
 
 
dbe4aa6
 
 
16e855a
 
 
dbe4aa6
 
 
 
 
 
16e855a
dbe4aa6
 
feea886
dbe4aa6
16e855a
dbe4aa6
16e855a
 
dbe4aa6
16e855a
 
dbe4aa6
16e855a
 
 
6811c82
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
import gradio as gr
from PIL import Image
import torch
import torchvision.transforms as transforms
from model import load_generator

# Load the trained generator
model = load_generator("model.pth")
model.eval()  # Ensure inference mode
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model.to(device)

# Preprocessing: Resize, convert to grayscale, normalize
transform = transforms.Compose([
    transforms.Resize((128, 128)),
    transforms.Grayscale(),
    transforms.ToTensor(),
    transforms.Normalize((0.5,), (0.5,))
])

# Postprocessing: Denormalize and convert back to PIL
to_pil = transforms.ToPILImage()

def style_transfer(img):
    if img is None:
        return None

    if img.mode != "L":  # Force grayscale input
        img = img.convert("L")

    input_tensor = transform(img).unsqueeze(0).to(device)

    with torch.no_grad():
        output_tensor = model(input_tensor).cpu()

    output_tensor = (output_tensor.squeeze(0) * 0.5 + 0.5).clamp(0, 1)
    return to_pil(output_tensor)

# Gradio interface
demo = gr.Interface(
    fn=style_transfer,
    inputs=gr.Image(type="pil", image_mode="L", label="Upload character image in Arial (grayscale)"),
    outputs=gr.Image(label="Converted to Times New Roman"),
    title="Font Style Transfer (CycleGAN)",
    description="Upload a grayscale character image in Arial style. This model converts it to Times New Roman using a CycleGAN."
)

if __name__ == "__main__":
    demo.launch()