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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -22
app.py CHANGED
@@ -1,28 +1,52 @@
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 = "runwayml/stable-diffusion-v1-5"
12
- session = GenerationSession(model_id)
13
-
14
- def ui_handler(user_prompt):
15
- start_time = time.time()
16
- image_list, enhanced_text = smart_generate(user_prompt, session, strength=0.45)
17
- final_image = image_list if isinstance(image_list, list) else image_list
18
- end_time = time.time()
19
- print(f"Image generation time: {end_time:.2f}s")
20
 
21
- return final_image, enhanced_text, f"Total generation time: {end_time - start_time:.2f}s"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
 
23
- def ui_reset():
24
- session.reset()
25
- return None, "Session cleared. Next generation will be a brand new Base Image.", "Session reset. Next generation will be a brand new Base Image."
26
 
27
  with gr.Blocks(title="Active Image Generator", theme=gr.Theme.from_hub("Respair/Shiki")) as demo:
28
  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.")
@@ -33,20 +57,18 @@ with gr.Blocks(title="Active Image Generator", theme=gr.Theme.from_hub("Respair/
33
  reset_button = gr.Button("Reset Session", variant="secondary")
34
 
35
  with gr.Column():
36
- output_image = gr.Image(label="Generated Image")
37
  enhanced_prompt = gr.Textbox(label="Enhanced Prompt", interactive=False)
38
- # Fix: Defined the component here and assigned it to a variable
39
  time_output = gr.Textbox(label="Generation Time", interactive=False)
40
 
41
- # Fix: Used the variable 'time_output' in the outputs list
42
  generate_button.click(
43
- fn=ui_handler,
44
  inputs=prompt_input,
45
  outputs=[output_image, enhanced_prompt, time_output]
46
  )
47
 
48
  reset_button.click(
49
- fn=ui_reset,
50
  inputs=None,
51
  outputs=[output_image, enhanced_prompt, time_output]
52
  )
 
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.")
 
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
  )