|
| 1 | +import requests |
| 2 | +from io import BytesIO |
| 3 | +from pathlib import Path |
| 4 | +from threading import Thread |
| 5 | +import gradio as gr |
| 6 | +import torch |
| 7 | +from transformers import set_seed, TextIteratorStreamer |
| 8 | +from PIL import Image |
| 9 | +import numpy as np |
| 10 | +from ov_janus_helper import generate_image |
| 11 | + |
| 12 | + |
| 13 | +def download_example_images(): |
| 14 | + image_urls = [ |
| 15 | + "https://github.com/deepseek-ai/Janus/blob/main/images/pie_chart.png?raw=true", |
| 16 | + "https://github.com/deepseek-ai/Janus/blob/main/images/equation.png?raw=true", |
| 17 | + ] |
| 18 | + image_names = ["pie_chart.png", "equation.png"] |
| 19 | + |
| 20 | + for image_name, image_url in zip(image_names, image_urls): |
| 21 | + if not Path(image_name).exists(): |
| 22 | + response = requests.get(image_url) |
| 23 | + image = Image.open(BytesIO(response.content)).convert("RGB") |
| 24 | + image.save(image_name) |
| 25 | + |
| 26 | + |
| 27 | +def make_demo(model, processor): |
| 28 | + download_example_images() |
| 29 | + |
| 30 | + # Multimodal Understanding function |
| 31 | + def multimodal_understanding(image, question, seed, top_p, temperature): |
| 32 | + # set seed |
| 33 | + torch.manual_seed(seed) |
| 34 | + np.random.seed(seed) |
| 35 | + |
| 36 | + conversation = [ |
| 37 | + { |
| 38 | + "role": "User", |
| 39 | + "content": f"<image_placeholder>\n{question}", |
| 40 | + "images": [image], |
| 41 | + }, |
| 42 | + {"role": "Assistant", "content": ""}, |
| 43 | + ] |
| 44 | + |
| 45 | + pil_images = [Image.fromarray(image)] |
| 46 | + prepare_inputs = processor(conversations=conversation, images=pil_images, force_batchify=True) |
| 47 | + |
| 48 | + inputs_embeds = model.prepare_inputs_embeds(**prepare_inputs) |
| 49 | + |
| 50 | + streamer = TextIteratorStreamer(processor.tokenizer, skip_prompt=True, skip_special_tokens=True) |
| 51 | + generate_kwargs = { |
| 52 | + "inputs_embeds": inputs_embeds, |
| 53 | + "attention_mask": prepare_inputs.attention_mask, |
| 54 | + "streamer": streamer, |
| 55 | + "max_new_tokens": 512, |
| 56 | + "pad_token_id": processor.tokenizer.eos_token_id, |
| 57 | + "bos_token_id": processor.tokenizer.bos_token_id, |
| 58 | + "eos_token_id": processor.tokenizer.eos_token_id, |
| 59 | + "do_sample": False if temperature == 0 else True, |
| 60 | + "temperature": temperature, |
| 61 | + "top_p": top_p, |
| 62 | + } |
| 63 | + t = Thread(target=model.language_model.generate, kwargs=generate_kwargs) |
| 64 | + t.start() |
| 65 | + |
| 66 | + # Pull the generated text from the streamer, and update the model output. |
| 67 | + model_output = "" |
| 68 | + for new_text in streamer: |
| 69 | + model_output += new_text |
| 70 | + yield model_output |
| 71 | + return model_output |
| 72 | + |
| 73 | + def image_generation(prompt, seed, cfg_weight, num_images, progress=gr.Progress(track_tqdm=True)): |
| 74 | + set_seed(seed) |
| 75 | + images = generate_image(model, processor, prompt, cfg_weight=cfg_weight, parallel_size=int(num_images)) |
| 76 | + images = [img.resize((1024, 1024), Image.LANCZOS) for img in images] |
| 77 | + return images |
| 78 | + |
| 79 | + # Gradio interface |
| 80 | + with gr.Blocks() as demo: |
| 81 | + gr.Markdown(value="# Multimodal Understanding") |
| 82 | + # with gr.Row(): |
| 83 | + with gr.Row(): |
| 84 | + image_input = gr.Image() |
| 85 | + with gr.Column(): |
| 86 | + question_input = gr.Textbox(label="Question") |
| 87 | + und_seed_input = gr.Number(label="Seed", precision=0, value=42) |
| 88 | + top_p = gr.Slider(minimum=0, maximum=1, value=0.95, step=0.05, label="top_p") |
| 89 | + temperature = gr.Slider(minimum=0, maximum=1, value=0.1, step=0.05, label="temperature") |
| 90 | + |
| 91 | + understanding_button = gr.Button("Chat") |
| 92 | + understanding_output = gr.Textbox(label="Response") |
| 93 | + |
| 94 | + examples_vl = gr.Examples( |
| 95 | + label="Multimodal Understanding examples", |
| 96 | + examples=[ |
| 97 | + [ |
| 98 | + "explain this chart", |
| 99 | + "pie_chart.png", |
| 100 | + ], |
| 101 | + [ |
| 102 | + "Convert the formula into latex code.", |
| 103 | + "equation.png", |
| 104 | + ], |
| 105 | + ], |
| 106 | + inputs=[question_input, image_input], |
| 107 | + ) |
| 108 | + |
| 109 | + gr.Markdown(value="# Text-to-Image Generation") |
| 110 | + |
| 111 | + with gr.Row(): |
| 112 | + cfg_weight_input = gr.Slider(minimum=1, maximum=10, value=5, step=0.5, label="CFG Weight") |
| 113 | + |
| 114 | + prompt_input = gr.Textbox(label="Prompt") |
| 115 | + seed_input = gr.Number(label="Seed (Optional)", precision=0, value=12345) |
| 116 | + num_images = gr.Slider(minimum=1, maximum=32, step=1, value=2, label="Number of generated images") |
| 117 | + |
| 118 | + generation_button = gr.Button("Generate Images") |
| 119 | + |
| 120 | + image_output = gr.Gallery(label="Generated Images", columns=2, rows=2, height=300) |
| 121 | + |
| 122 | + examples_t2i = gr.Examples( |
| 123 | + label="Text to image generation examples. (Tips for designing prompts: Adding description like 'digital art' at the end of the prompt or writing the prompt in more detail can help produce better images!)", |
| 124 | + examples=[ |
| 125 | + "Master shifu racoon wearing drip attire as a street gangster.", |
| 126 | + "A cute and adorable baby fox with big brown eyes, autumn leaves in the background enchanting,immortal,fluffy, shiny mane,Petals,fairyism,unreal engine 5 and Octane Render,highly detailed, photorealistic, cinematic, natural colors.", |
| 127 | + "The image features an intricately designed eye set against a circular backdrop adorned with ornate swirl patterns that evoke both realism and surrealism. At the center of attention is a strikingly vivid blue iris surrounded by delicate veins radiating outward from the pupil to create depth and intensity. The eyelashes are long and dark, casting subtle shadows on the skin around them which appears smooth yet slightly textured as if aged or weathered over time.\n\nAbove the eye, there's a stone-like structure resembling part of classical architecture, adding layers of mystery and timeless elegance to the composition. This architectural element contrasts sharply but harmoniously with the organic curves surrounding it. Below the eye lies another decorative motif reminiscent of baroque artistry, further enhancing the overall sense of eternity encapsulated within each meticulously crafted detail. \n\nOverall, the atmosphere exudes a mysterious aura intertwined seamlessly with elements suggesting timelessness, achieved through the juxtaposition of realistic textures and surreal artistic flourishes. Each component\u2014from the intricate designs framing the eye to the ancient-looking stone piece above\u2014contributes uniquely towards creating a visually captivating tableau imbued with enigmatic allure.", |
| 128 | + ], |
| 129 | + inputs=prompt_input, |
| 130 | + ) |
| 131 | + |
| 132 | + understanding_button.click( |
| 133 | + multimodal_understanding, inputs=[image_input, question_input, und_seed_input, top_p, temperature], outputs=understanding_output |
| 134 | + ) |
| 135 | + |
| 136 | + generation_button.click(fn=image_generation, inputs=[prompt_input, seed_input, cfg_weight_input, num_images], outputs=image_output) |
| 137 | + |
| 138 | + return demo |
0 commit comments