Multitab_genai / app.py
hfmrbean's picture
.
69ded19
raw
history blame
3.72 kB
import os
import time
import gradio as gr
import numpy as np
import requests
from PIL import Image
from io import BytesIO
from azure.storage.blob import BlobServiceClient
from huggingface_hub import InferenceClient
from paintingface import generate
from azure.storage.blob import BlobClient
from pathlib import Path
connect_str = os.getenv('AZURE_STORAGE_CONNECTION_STRING')
container_name = "images"
blob_service_client = BlobServiceClient.from_connection_string(conn_str=connect_str) # create a blob service client to interact with the storage account
try:
container_client = blob_service_client.get_container_client(container=container_name) # get container client to interact with the container in which images will be stored
container_client.get_container_properties() # get properties of the container to force exception to be thrown if container does not exist
except Exception as e:
container_client = blob_service_client.create_container(container_name) # create a container in the storage account if it does not exista
def load_image(url):
response = requests.get(url)
img = Image.open(BytesIO(response.content))
return img
def image_loader(url):
img = load_image(url)
return img
def txt2img(x):
client = InferenceClient()
image = client.text_to_image(x)
timestr = time.strftime("%Y%m%d-%H%M%S")
local_path = "./"
# Upload the created file
imgfile="txt2img%s.png"%timestr
print("######################file name %s",imgfile)
img_saved = image.save(imgfile)
print("######################file is" ,os.path.abspath(imgfile))
blob_client = blob_service_client.get_blob_client(container=container_name, blob=imgfile)
print("\nUploading to Azure Storage as blob:\n\t" + imgfile)
upload_file_path = os.path.join(local_path, imgfile)
with open(file=upload_file_path, mode="rb") as data:
blob_client.upload_blob(data)
blob_items = container_client.list_blobs() # list all the blobs in the container
for blob in blob_items:
blob_client = container_client.get_blob_client(blob=imgfile)
url=blob_client.url
print("i###################URL OF IMAGE = %s", url)
return image_loader(url), generate(image_loader(url))
def img2img(x):
timestr = time.strftime("%Y%m%d-%H%M%S")
local_path = "./"
# Upload the created file
imgfile="img2img%s.png"%timestr
print("######################file name %s",imgfile)
img = Image.fromarray(x, "RGB")
img_saved = img.save(imgfile)
print("######################file is" ,os.path.abspath(imgfile))
blob_client = blob_service_client.get_blob_client(container=container_name, blob=imgfile)
print("\nUploading to Azure Storage as blob:\n\t" + imgfile)
upload_file_path = os.path.join(local_path, imgfile)
with open(file=upload_file_path, mode="rb") as data:
blob_client.upload_blob(data)
blob_items = container_client.list_blobs() # list all the blobs in the container
for blob in blob_items:
blob_client = container_client.get_blob_client(blob=imgfile)
url=blob_client.url
print("i###################URL OF IMAGE = %s", url)
return image_loader(url), generate(image_loader(url))
#return image_loader(x), generate(image_loader(x))
def imgproc(x):
limg = load_image(x)
return np.fliplr(limg)
title = "Azure Huggingface"
t2i_demo = gr.Interface(fn=txt2img, inputs="text", outputs=["image","image"])
i2i_demo = gr.Interface(fn=img2img, inputs=gr.inputs.Image(label="Image Input 2") , outputs=["image","image"])
demo = gr.TabbedInterface([t2i_demo,i2i_demo ], ["Text-to-Image", "Image-to-Image"])
#if __name__ == "__main__":
demo.launch(share=True)