Update app.py
Browse files
app.py
CHANGED
|
@@ -1,90 +1,60 @@
|
|
| 1 |
-
import
|
| 2 |
-
from unittest.mock import MagicMock
|
| 3 |
-
|
| 4 |
import torch
|
| 5 |
-
|
| 6 |
-
torch.float8_e8m0fnu = torch.float16
|
| 7 |
-
|
| 8 |
import io
|
| 9 |
from PIL import Image
|
| 10 |
from huggingface_hub import InferenceClient
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
self.client = InferenceClient()
|
| 15 |
-
self.model_id = "simianluo/lcm_dreamshaper_v7"
|
| 16 |
-
self.scheduler = MagicMock()
|
| 17 |
-
|
| 18 |
-
def to(self, device): return self
|
| 19 |
-
def enable_attention_slicing(self): pass
|
| 20 |
-
def enable_vae_slicing(self): pass
|
| 21 |
-
|
| 22 |
-
def __call__(self, prompt, image=None, strength=0.5, **kwargs):
|
| 23 |
-
if image is None:
|
| 24 |
-
img = self.client.text_to_image(prompt, model=self.model_id)
|
| 25 |
-
else:
|
| 26 |
-
img_byte_arr = io.BytesIO()
|
| 27 |
-
image.save(img_byte_arr, format='PNG')
|
| 28 |
-
img_bytes = img_byte_arr.getvalue()
|
| 29 |
-
img = self.client.image_to_image(img_bytes, prompt=prompt, model=self.model_id, strength=strength)
|
| 30 |
-
|
| 31 |
-
class DiffusersOutput:
|
| 32 |
-
def __init__(self, images): self.images = images
|
| 33 |
-
return DiffusersOutput(images=img)
|
| 34 |
-
|
| 35 |
-
mock_pipe = APIPipelineMock()
|
| 36 |
-
|
| 37 |
-
class MockDiffusionPipeline:
|
| 38 |
-
@classmethod
|
| 39 |
-
def from_pretrained(cls, *args, **kwargs): return mock_pipe
|
| 40 |
-
|
| 41 |
-
class MockAutoPipeline:
|
| 42 |
-
@classmethod
|
| 43 |
-
def from_pipe(cls, *args, **kwargs): return mock_pipe
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
import diffusers
|
| 47 |
-
diffusers.DiffusionPipeline = MockDiffusionPipeline
|
| 48 |
-
diffusers.AutoPipelineForImage2Image = MockAutoPipeline
|
| 49 |
-
|
| 50 |
|
| 51 |
-
import gradio as gr
|
| 52 |
-
import time
|
| 53 |
from LLM_pipeline import smart_generate
|
| 54 |
from model_loading import GenerationSession
|
| 55 |
|
| 56 |
model_id = "simianluo/lcm_dreamshaper_v7"
|
| 57 |
session = GenerationSession(model_id)
|
| 58 |
|
| 59 |
-
|
| 60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
def ui_handler(user_prompt):
|
| 63 |
start_time = time.time()
|
|
|
|
| 64 |
|
| 65 |
-
if
|
| 66 |
-
|
| 67 |
-
final_image = raw_output.images
|
| 68 |
else:
|
| 69 |
-
|
| 70 |
-
final_image = raw_output.images
|
| 71 |
|
| 72 |
-
session.current_image = final_image
|
| 73 |
-
session.current_prompt = user_prompt
|
| 74 |
-
enhanced_text = user_prompt
|
| 75 |
-
|
| 76 |
end_time = time.time()
|
| 77 |
return final_image, enhanced_text, f"Total generation time: {end_time - start_time:.2f}s"
|
| 78 |
|
| 79 |
def ui_reset():
|
| 80 |
session.reset()
|
| 81 |
-
return None, "Session cleared.", "Session reset."
|
| 82 |
|
| 83 |
with gr.Blocks(title="Active Image Generator", theme=gr.Theme.from_hub("Respair/Shiki")) as demo:
|
| 84 |
gr.Markdown("## Active Image Generator\n\nEnter a prompt to generate or modify an image.")
|
| 85 |
|
| 86 |
with gr.Row():
|
| 87 |
-
prompt_input = gr.Textbox(label="Enter your prompt", placeholder="Describe the image...")
|
| 88 |
generate_button = gr.Button("Generate", variant="primary")
|
| 89 |
reset_button = gr.Button("Reset Session", variant="secondary")
|
| 90 |
|
|
|
|
| 1 |
+
import gradio as gr
|
|
|
|
|
|
|
| 2 |
import torch
|
| 3 |
+
import time
|
|
|
|
|
|
|
| 4 |
import io
|
| 5 |
from PIL import Image
|
| 6 |
from huggingface_hub import InferenceClient
|
| 7 |
|
| 8 |
+
if not hasattr(torch, 'float8_e8m0fnu'):
|
| 9 |
+
torch.float8_e8m0fnu = torch.float16
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
|
|
|
|
|
|
| 11 |
from LLM_pipeline import smart_generate
|
| 12 |
from model_loading import GenerationSession
|
| 13 |
|
| 14 |
model_id = "simianluo/lcm_dreamshaper_v7"
|
| 15 |
session = GenerationSession(model_id)
|
| 16 |
|
| 17 |
+
api_client = InferenceClient()
|
| 18 |
+
target_model = "simianluo/lcm_dreamshaper_v7"
|
| 19 |
+
|
| 20 |
+
def dynamic_cloud_call(prompt, image=None, strength=0.45, **kwargs):
|
| 21 |
+
if image is None:
|
| 22 |
+
output_image = api_client.text_to_image(prompt, model=target_model)
|
| 23 |
+
else:
|
| 24 |
+
img_byte_arr = io.BytesIO()
|
| 25 |
+
image.save(img_byte_arr, format='PNG')
|
| 26 |
+
img_bytes = img_byte_arr.getvalue()
|
| 27 |
+
output_image = api_client.image_to_image(img_bytes, prompt=prompt, model=target_model, strength=strength)
|
| 28 |
+
|
| 29 |
+
class DiffusersOutput:
|
| 30 |
+
def __init__(self, images):
|
| 31 |
+
self.images = [images] if not isinstance(images, list) else images
|
| 32 |
+
return DiffusersOutput(images=output_image)
|
| 33 |
+
|
| 34 |
+
if session.txt2img_pipeline is not None:
|
| 35 |
+
session.txt2img_pipeline.__class__.__call__ = dynamic_cloud_call
|
| 36 |
|
| 37 |
def ui_handler(user_prompt):
|
| 38 |
start_time = time.time()
|
| 39 |
+
image_list, enhanced_text = smart_generate(user_prompt, session, strength=0.45)
|
| 40 |
|
| 41 |
+
if isinstance(image_list, list) and len(image_list) > 0:
|
| 42 |
+
final_image = image_list
|
|
|
|
| 43 |
else:
|
| 44 |
+
final_image = image_list
|
|
|
|
| 45 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
end_time = time.time()
|
| 47 |
return final_image, enhanced_text, f"Total generation time: {end_time - start_time:.2f}s"
|
| 48 |
|
| 49 |
def ui_reset():
|
| 50 |
session.reset()
|
| 51 |
+
return None, "Session cleared. Next generation will be a brand new Base Image.", "Session reset."
|
| 52 |
|
| 53 |
with gr.Blocks(title="Active Image Generator", theme=gr.Theme.from_hub("Respair/Shiki")) as demo:
|
| 54 |
gr.Markdown("## Active Image Generator\n\nEnter a prompt to generate or modify an image.")
|
| 55 |
|
| 56 |
with gr.Row():
|
| 57 |
+
prompt_input = gr.Textbox(label="Enter your prompt", placeholder="Describe the image you want to create...")
|
| 58 |
generate_button = gr.Button("Generate", variant="primary")
|
| 59 |
reset_button = gr.Button("Reset Session", variant="secondary")
|
| 60 |
|