Abdullah-Habib commited on
Commit
46b2abb
·
1 Parent(s): 2b5f98f

trying with base 64

Browse files
Files changed (1) hide show
  1. app.py +12 -2
app.py CHANGED
@@ -2,6 +2,8 @@ import gradio as gr
2
  import json
3
  import logging
4
  import torch
 
 
5
  from PIL import Image
6
  from diffusers import (
7
  DiffusionPipeline,
@@ -27,6 +29,14 @@ base_model = "stabilityai/stable-diffusion-xl-base-1.0"
27
  pipe = DiffusionPipeline.from_pretrained(base_model, torch_dtype=torch.float16)
28
  pipe.to("cuda")
29
 
 
 
 
 
 
 
 
 
30
  def update_selection(evt: gr.SelectData):
31
  selected_lora = loras[evt.index]
32
  new_placeholder = f"Type a prompt for {selected_lora['title']}"
@@ -106,7 +116,7 @@ def run_lora(prompt, negative_prompt, cfg_scale, steps, scheduler, seed, width,
106
  # Unload LoRA weights
107
  pipe.unload_lora_weights()
108
 
109
- return image
110
 
111
  with gr.Blocks(theme=gr.themes.Soft()) as app:
112
 
@@ -114,7 +124,7 @@ with gr.Blocks(theme=gr.themes.Soft()) as app:
114
 
115
  with gr.Row():
116
  with gr.Column(scale=2):
117
- result = gr.Image(label="Generated Image", height=768)
118
  generate_button = gr.Button("Generate", variant="primary")
119
 
120
  # with gr.Column(scale=1):
 
2
  import json
3
  import logging
4
  import torch
5
+ import base64
6
+ from io import BytesIO
7
  from PIL import Image
8
  from diffusers import (
9
  DiffusionPipeline,
 
29
  pipe = DiffusionPipeline.from_pretrained(base_model, torch_dtype=torch.float16)
30
  pipe.to("cuda")
31
 
32
+ def image_to_base64(image: Image) -> str:
33
+ buffered = BytesIO()
34
+ image.save(buffered, format="PNG") # You can change the format as needed (e.g., "JPEG")
35
+ img_base64 = base64.b64encode(buffered.getvalue()).decode('utf-8')
36
+ return img_base64
37
+
38
+
39
+
40
  def update_selection(evt: gr.SelectData):
41
  selected_lora = loras[evt.index]
42
  new_placeholder = f"Type a prompt for {selected_lora['title']}"
 
116
  # Unload LoRA weights
117
  pipe.unload_lora_weights()
118
 
119
+ return image_to_base64(image)
120
 
121
  with gr.Blocks(theme=gr.themes.Soft()) as app:
122
 
 
124
 
125
  with gr.Row():
126
  with gr.Column(scale=2):
127
+ result = gr.Text(label="Generated Image")
128
  generate_button = gr.Button("Generate", variant="primary")
129
 
130
  # with gr.Column(scale=1):