Spaces:
Runtime error
Runtime error
Commit
·
70138aa
1
Parent(s):
ac36dce
Delete app.py
Browse files
app.py
DELETED
@@ -1,142 +0,0 @@
|
|
1 |
-
# -*- coding: utf-8 -*-
|
2 |
-
"""Untitled3.ipynb
|
3 |
-
|
4 |
-
Automatically generated by Colaboratory.
|
5 |
-
|
6 |
-
Original file is located at
|
7 |
-
https://colab.research.google.com/drive/1BltKPv_n-glCuuIIYSBA6GHK-tmwbl20
|
8 |
-
"""
|
9 |
-
|
10 |
-
import torch
|
11 |
-
from PIL import Image
|
12 |
-
from torchvision import transforms
|
13 |
-
import gradio as gr
|
14 |
-
import json
|
15 |
-
import urllib, urllib.request
|
16 |
-
from diffusers import DiffusionPipeline
|
17 |
-
from transformers import pipeline
|
18 |
-
|
19 |
-
# First Page
|
20 |
-
def demo_tab(image):
|
21 |
-
# The demo tab simply returns the same image as input
|
22 |
-
return image
|
23 |
-
|
24 |
-
# Secibd Page
|
25 |
-
def generate_image(Prompt,Negative_prompt,Steps):
|
26 |
-
# load both base & refiner
|
27 |
-
base = DiffusionPipeline.from_pretrained(
|
28 |
-
"stabilityai/stable-diffusion-xl-base-1.0", torch_dtype=torch.float16, variant="fp16", use_safetensors=True
|
29 |
-
)
|
30 |
-
base.to("cuda")
|
31 |
-
refiner = DiffusionPipeline.from_pretrained(
|
32 |
-
"stabilityai/stable-diffusion-xl-refiner-1.0",
|
33 |
-
text_encoder_2=base.text_encoder_2,
|
34 |
-
vae=base.vae,
|
35 |
-
torch_dtype=torch.float16,
|
36 |
-
use_safetensors=True,
|
37 |
-
variant="fp16",
|
38 |
-
)
|
39 |
-
refiner.to("cuda")
|
40 |
-
|
41 |
-
# Define how many steps and what % of steps to be run on each experts (80/20) here
|
42 |
-
high_noise_frac = 0.8
|
43 |
-
prompt = Prompt
|
44 |
-
negative_prompt = Negative_prompt
|
45 |
-
n_steps = Steps
|
46 |
-
# run both experts
|
47 |
-
image = base(
|
48 |
-
prompt=prompt,
|
49 |
-
negative_prompt=negative_prompt,
|
50 |
-
num_inference_steps=n_steps,
|
51 |
-
denoising_end=high_noise_frac,
|
52 |
-
output_type="latent",
|
53 |
-
).images
|
54 |
-
image = refiner(
|
55 |
-
prompt=prompt,
|
56 |
-
num_inference_steps=n_steps,
|
57 |
-
denoising_start=high_noise_frac,
|
58 |
-
image=image,
|
59 |
-
).images[0]
|
60 |
-
return image
|
61 |
-
|
62 |
-
def predict(input_image):
|
63 |
-
model = torch.hub.load('RF5/danbooru-pretrained', 'resnet50')
|
64 |
-
model.eval()
|
65 |
-
|
66 |
-
# Load JSON file from github as Label
|
67 |
-
with urllib.request.urlopen("https://github.com/RF5/danbooru-pretrained/raw/master/config/class_names_6000.json") as url:
|
68 |
-
labels = json.loads(url.read().decode())
|
69 |
-
#Convert input image from array to PIL Image
|
70 |
-
input_image = Image.fromarray(input_image.astype('uint8'), 'RGB')
|
71 |
-
#Preprocess the input image
|
72 |
-
preprocess = transforms.Compose([
|
73 |
-
transforms.Resize(360),
|
74 |
-
transforms.ToTensor(),
|
75 |
-
transforms.Normalize(mean=[0.7137, 0.6628, 0.6519], std=[0.2970, 0.3017, 0.2979]),
|
76 |
-
])
|
77 |
-
input_tensor = preprocess(input_image)
|
78 |
-
input_batch = input_tensor.unsqueeze(0)
|
79 |
-
|
80 |
-
# Use CUDA if available
|
81 |
-
if torch.cuda.is_available():
|
82 |
-
input_batch = input_batch.to('cuda')
|
83 |
-
model.to('cuda')
|
84 |
-
|
85 |
-
# Make prediction
|
86 |
-
with torch.no_grad():
|
87 |
-
output = model(input_batch)
|
88 |
-
|
89 |
-
# Get probabilities
|
90 |
-
probs = torch.sigmoid(output[0])
|
91 |
-
|
92 |
-
# Convert tensor to Python list of floats
|
93 |
-
probs = probs.cpu().numpy().tolist()
|
94 |
-
|
95 |
-
# Sort labels with probabilities and return top 10
|
96 |
-
sorted_labels_with_probs = sorted(list(zip(labels, probs)), key=lambda x: x[1], reverse=True)[:10]
|
97 |
-
|
98 |
-
# Convert list of tuples to dictionary and convert numpy floats to Python floats
|
99 |
-
sorted_labels_with_probs_dict = {label: float(prob) for label, prob in sorted_labels_with_probs}
|
100 |
-
return sorted_labels_with_probs_dict
|
101 |
-
|
102 |
-
def image_classify(input_image, model):
|
103 |
-
model_mapping= {
|
104 |
-
"Resnet 50": "microsoft/resnet-50",
|
105 |
-
"Vit Base Patch16-224": "google/vit-base-patch16-224",
|
106 |
-
"NSFW Image Detection": "Falconsai/nsfw_image_detection",
|
107 |
-
"Vit Age Classifier": "nateraw/vit-age-classifier"
|
108 |
-
}
|
109 |
-
classifier = pipeline("image-classification", model=model_mapping[model])
|
110 |
-
img = input_image
|
111 |
-
result = classifier(img)
|
112 |
-
#Sort the perccentage confident from highest to lowest
|
113 |
-
highest_confidence_result = sorted(result, key=lambda x: x['score'], reverse=True)[0]
|
114 |
-
# Format the score as a percentage and combine it with the label
|
115 |
-
output = f"{highest_confidence_result['score']*100:.2f}% confident : {highest_confidence_result['label']}"
|
116 |
-
return output
|
117 |
-
|
118 |
-
# Define the demo tab
|
119 |
-
with gr.Blocks() as demo:
|
120 |
-
with gr.Tab("Demo"):
|
121 |
-
image_input = gr.Image(type='pil')
|
122 |
-
image_output = gr.Image()
|
123 |
-
demo_button = gr.Button("Generate")
|
124 |
-
|
125 |
-
with gr.Tab("Text2Image"):
|
126 |
-
SD_text_input = gr.Textbox(lines=5, label="Prompt")
|
127 |
-
SD_text2_input = gr.Textbox(lines=5, label="Negative Prompt")
|
128 |
-
Slider_input = gr.Slider(0, 100, label="Strength")
|
129 |
-
SD_output = gr.Image()
|
130 |
-
SD_button = gr.Button("Generate")
|
131 |
-
|
132 |
-
with gr.Tab("Text2Image"):
|
133 |
-
option_input = gr.Dropdown(["resnet50", "vit-base-patch16-224", "vit-age-classifier", "nsfw image classification"], label="Model")
|
134 |
-
t2i_input = gr.Image(label="Image")
|
135 |
-
t2i_output = gr.Image()
|
136 |
-
t2i_button = gr.Button("Generate")
|
137 |
-
|
138 |
-
demo_button.click(demo_tab, inputs=image_input, outputs=image_output)
|
139 |
-
SD_button.click(generate_image, inputs=[SD_text_input,SD_text2_input,Slider_input], outputs=SD_output)
|
140 |
-
t2i_button.click(image_classify, inputs=[option_input,t2i_input], outputs=t2i_button)
|
141 |
-
|
142 |
-
demo.launch(debug=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|