Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,8 +1,7 @@
|
|
| 1 |
-
# app.py
|
| 2 |
import torch
|
| 3 |
import torch.nn as nn
|
| 4 |
import gradio as gr
|
| 5 |
-
import
|
| 6 |
|
| 7 |
# --------------------------
|
| 8 |
# تنظیمات
|
|
@@ -12,7 +11,7 @@ latent_dim = 100
|
|
| 12 |
model_path = "generator.pth" # فایل مدل Generator آموزش دیده
|
| 13 |
|
| 14 |
# --------------------------
|
| 15 |
-
# تعریف Generator
|
| 16 |
# --------------------------
|
| 17 |
class Generator(nn.Module):
|
| 18 |
def __init__(self):
|
|
@@ -44,7 +43,8 @@ def generate_image(seed=42):
|
|
| 44 |
torch.manual_seed(seed)
|
| 45 |
z = torch.randn(1, latent_dim).to(device)
|
| 46 |
img = G(z).detach().cpu().squeeze().numpy()
|
| 47 |
-
img = (img + 1) / 2 # تبدیل
|
|
|
|
| 48 |
return img
|
| 49 |
|
| 50 |
# --------------------------
|
|
@@ -53,7 +53,7 @@ def generate_image(seed=42):
|
|
| 53 |
iface = gr.Interface(
|
| 54 |
fn=generate_image,
|
| 55 |
inputs=gr.Slider(0, 10000, value=42, label="Seed"),
|
| 56 |
-
outputs=gr.Image(
|
| 57 |
title="MNIST GAN Generator",
|
| 58 |
description="یک مدل GAN ساده برای تولید تصاویر اعداد دستنویس MNIST"
|
| 59 |
)
|
|
|
|
|
|
|
| 1 |
import torch
|
| 2 |
import torch.nn as nn
|
| 3 |
import gradio as gr
|
| 4 |
+
import numpy as np
|
| 5 |
|
| 6 |
# --------------------------
|
| 7 |
# تنظیمات
|
|
|
|
| 11 |
model_path = "generator.pth" # فایل مدل Generator آموزش دیده
|
| 12 |
|
| 13 |
# --------------------------
|
| 14 |
+
# تعریف Generator
|
| 15 |
# --------------------------
|
| 16 |
class Generator(nn.Module):
|
| 17 |
def __init__(self):
|
|
|
|
| 43 |
torch.manual_seed(seed)
|
| 44 |
z = torch.randn(1, latent_dim).to(device)
|
| 45 |
img = G(z).detach().cpu().squeeze().numpy()
|
| 46 |
+
img = (img + 1) / 2 # تبدیل [-1,1] به [0,1]
|
| 47 |
+
img = (img * 255).astype(np.uint8) # تبدیل به uint8
|
| 48 |
return img
|
| 49 |
|
| 50 |
# --------------------------
|
|
|
|
| 53 |
iface = gr.Interface(
|
| 54 |
fn=generate_image,
|
| 55 |
inputs=gr.Slider(0, 10000, value=42, label="Seed"),
|
| 56 |
+
outputs=gr.Image(type="numpy"), # مهم: از type="numpy" استفاده کنید
|
| 57 |
title="MNIST GAN Generator",
|
| 58 |
description="یک مدل GAN ساده برای تولید تصاویر اعداد دستنویس MNIST"
|
| 59 |
)
|