Spaces:
Sleeping
Sleeping
Download app.py from Armyman91/FontStyleTransfer: direct link, hf CLI and curl.
- Browser
- Download file 1.47 kB
-
https://huggingface.co/spaces/Armyman91/FontStyleTransfer/resolve/main/app.py
- Command line
-
hf download hf://spaces/Armyman91/FontStyleTransfer/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Armyman91/FontStyleTransfer/resolve/main/app.py
1.47 kB
| 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() |