File size: 1,807 Bytes
bc0d978
0cdadc9
cd42042
b85438c
0cdadc9
6dc4bbd
13298a2
7aae5d0
f466dd9
c9b9787
7aae5d0
6dc4bbd
bc0d978
 
 
f466dd9
c9b9787
789e6b5
d26a101
bc0d978
 
7aae5d0
bc0d978
 
 
 
 
 
 
 
 
 
 
cd42042
bc0d978
 
 
f466dd9
 
bc0d978
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import gradio as gr
from diffusers import AutoPipelineForText2Image
from generate_prompts import generate_prompt

# Load the model once outside of the function
model = AutoPipelineForText2Image.from_pretrained("stabilityai/sdxl-turbo")

def generate_image(prompt, prompt_name):
    try:
        print(f"Generating image for {prompt_name}")
        output = model(prompt=prompt, num_inference_steps=1, guidance_scale=0.0)
        image = output.images[0]
        img_bytes = image.tobytes()
        print(f"Image bytes length for {prompt_name}: {len(img_bytes)}")
        return img_bytes
    except Exception as e:
        print(f"Error generating image for {prompt_name}: {e}")
        return None

def gradio_interface(sentence_mapping, character_dict, selected_style):
    prompts = generate_prompt(sentence_mapping, character_dict, selected_style)
    image_bytes_list = [generate_image(prompt, f"Prompt {i}") for i, prompt in enumerate(prompts)]
    outputs = [gr.Image.update(value=img_bytes) if img_bytes else gr.Image.update(value=None) for img_bytes in image_bytes_list]
    return outputs

# Gradio Interface
with gr.Blocks() as demo:
    with gr.Row():
        with gr.Column():
            sentence_mapping_input = gr.Textbox(label="Sentence Mapping")
            character_dict_input = gr.Textbox(label="Character Dictionary")
            selected_style_input = gr.Textbox(label="Selected Style")
            submit_btn = gr.Button(value='Submit')
            prompt_responses = []  # Empty list for dynamic addition of Image components
            submit_btn.click(fn=gradio_interface,
                             inputs=[sentence_mapping_input, character_dict_input, selected_style_input],
                             outputs=prompt_responses)

if __name__ == "__main__":
    demo.launch()