CyberRohith commited on
Commit
1040cdc
·
verified ·
1 Parent(s): 33dd53f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -46
app.py CHANGED
@@ -1,74 +1,59 @@
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
 
9
- class CloudFastSession:
10
- def __init__(self):
11
- self.client = InferenceClient()
12
- self.model_id = "simianluo/lcm_dreamshaper_v7"
13
- self.current_image = None
14
- self.current_prompt = None
15
 
16
- def ui_handler(self, user_prompt):
17
- start_time = time.time()
18
-
19
- try:
20
- if self.current_image is None:
21
- image = self.client.text_to_image(user_prompt, model=self.model_id)
22
- else:
23
- img_byte_arr = io.BytesIO()
24
- self.current_image.save(img_byte_arr, format='PNG')
25
- img_bytes = img_byte_arr.getvalue()
26
-
27
- image = self.client.image_to_image(img_bytes, prompt=user_prompt, model=self.model_id, strength=0.45)
28
-
29
- self.current_image = image
30
- self.current_prompt = user_prompt
31
- enhanced_text = user_prompt
32
-
33
- except Exception as e:
34
- print(f"API Error: {e}. Falling back to a clean text-to-image call.")
35
- image = self.client.text_to_image(user_prompt, model=self.model_id)
36
- self.current_image = image
37
- self.current_prompt = user_prompt
38
- enhanced_text = user_prompt
39
-
40
- end_time = time.time()
41
- return self.current_image, enhanced_text, f"Total generation time: {end_time - start_time:.2f}s"
42
-
43
- def ui_reset(self):
44
- self.current_image = None
45
- self.current_prompt = None
46
- return None, "Session cleared.", "Session reset. Next generation will be a brand new Base Image."
47
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
48
 
49
- session_manager = CloudFastSession()
 
 
50
 
51
  with gr.Blocks(title="Active Image Generator", theme=gr.Theme.from_hub("Respair/Shiki")) as demo:
52
- gr.Markdown("## Active Image Generator\n\nEnter a prompt to generate or modify an image. Each new prompt will build upon the previous image, creating a dynamic and evolving visual experience. Use the reset button to start fresh with a new base image.")
53
 
54
  with gr.Row():
55
- prompt_input = gr.Textbox(label="Enter your prompt", placeholder="Describe the image you want to create or modify...")
56
  generate_button = gr.Button("Generate", variant="primary")
57
  reset_button = gr.Button("Reset Session", variant="secondary")
58
 
59
  with gr.Column():
60
- output_image = gr.Image(label="Generated Image", type="pil")
61
  enhanced_prompt = gr.Textbox(label="Enhanced Prompt", interactive=False)
62
  time_output = gr.Textbox(label="Generation Time", interactive=False)
63
 
64
  generate_button.click(
65
- fn=session_manager.ui_handler,
66
  inputs=prompt_input,
67
  outputs=[output_image, enhanced_prompt, time_output]
68
  )
69
 
70
  reset_button.click(
71
- fn=session_manager.ui_reset,
72
  inputs=None,
73
  outputs=[output_image, enhanced_prompt, time_output]
74
  )
 
1
  import gradio as gr
2
  import torch
3
  import time
 
 
 
4
 
5
+ if not hasattr(torch, 'float8_e8m0fnu'):
6
+ torch.float8_e8m0fnu = torch.float16
7
 
8
+ from LLM_pipeline import smart_generate
9
+ from model_loading import GenerationSession
 
 
 
 
10
 
11
+ model_id = "simianluo/lcm_dreamshaper_v7"
12
+ session = GenerationSession(model_id)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
 
14
+ if session.device == "cpu":
15
+ print("Applying High-Quality CPU sequential offload patches...")
16
+ try:
17
+
18
+ session.txt2img_pipeline.enable_sequential_cpu_offload()
19
+ session.img2img_pipeline.enable_sequential_cpu_offload()
20
+ except Exception as e:
21
+ print(f"Note: Multi-pipeline offload optimized via sub-modules: {e}")
22
+
23
+ def ui_handler(user_prompt):
24
+ start_time = time.time()
25
+ image_list, enhanced_text = smart_generate(user_prompt, session, strength=0.45)
26
+ final_image = image_list if isinstance(image_list, list) else image_list
27
+ end_time = time.time()
28
+ print(f"Image generation time: {end_time:.2f}s")
29
+
30
+ return final_image, enhanced_text, f"Total generation time: {end_time - start_time:.2f}s"
31
 
32
+ def ui_reset():
33
+ session.reset()
34
+ return None, "Session cleared.", "Session reset."
35
 
36
  with gr.Blocks(title="Active Image Generator", theme=gr.Theme.from_hub("Respair/Shiki")) as demo:
37
+ gr.Markdown("## Active Image Generator\n\nEnter a prompt to generate or modify an image.")
38
 
39
  with gr.Row():
40
+ prompt_input = gr.Textbox(label="Enter your prompt", placeholder="Describe the image...")
41
  generate_button = gr.Button("Generate", variant="primary")
42
  reset_button = gr.Button("Reset Session", variant="secondary")
43
 
44
  with gr.Column():
45
+ output_image = gr.Image(label="Generated Image")
46
  enhanced_prompt = gr.Textbox(label="Enhanced Prompt", interactive=False)
47
  time_output = gr.Textbox(label="Generation Time", interactive=False)
48
 
49
  generate_button.click(
50
+ fn=ui_handler,
51
  inputs=prompt_input,
52
  outputs=[output_image, enhanced_prompt, time_output]
53
  )
54
 
55
  reset_button.click(
56
+ fn=ui_reset,
57
  inputs=None,
58
  outputs=[output_image, enhanced_prompt, time_output]
59
  )