Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import requests | |
| from openai import OpenAI | |
| from PIL import Image | |
| def create_var(image_path, open_api_key): | |
| url_list = [] | |
| img_list = [] | |
| im1 = Image.open(image_path) | |
| im1.save(r'new_image.png') | |
| client = OpenAI(api_key = open_api_key) | |
| # image2 = con_image("white_tile.jpg") | |
| response = client.images.create_variation( | |
| image=open('new_image.png', "rb"), | |
| n=4, | |
| size="1024x1024" | |
| ) | |
| for i in range(len(response.data)): | |
| url1 = response.data[i].url | |
| url_list.append(url1) | |
| for j in range(len(url_list)) : | |
| response = requests.get(url_list[j]) | |
| with open(f"image{j}.jpg", "wb") as f: | |
| f.write(response.content) | |
| img_list.append(f"image{j}.jpg") | |
| return (img_list) | |
| block = gr.Blocks(theme=gr.themes.Glass(primary_hue="slate")) | |
| with block: | |
| gr.Markdown("""<h1><center>Create Image Variations</center></h1> """) | |
| with gr.Row(): | |
| with gr.Column(scale=0.5): | |
| img_input = gr.Image(type="filepath", label="Upload Image") | |
| api_input = gr.Textbox(label= "Enter Api-key") | |
| upload_button = gr.Button(value="Upload & Start Chat", interactive=True, variant="primary") | |
| with gr.Column(): | |
| img_gallery = gr.Gallery(label="Generated images", show_label=True, elem_id="gallery", object_fit="contain", height="auto",allow_preview=True) | |
| upload_button.click(create_var, inputs=[img_input, api_input], outputs = img_gallery) | |
| block.launch() |