hfmrbean/Multitab_genai
0
1import os2import time3import gradio as gr4import numpy as np5import requests6from PIL import Image7from io import BytesIO8from azure.storage.blob import BlobServiceClient9from huggingface_hub import InferenceClient10from paintingface import generate11from azure.storage.blob import BlobClient12from pathlib import Path13 14os.environ['AZURE_STORAGE_CONNECTION_STRING'] = 'DefaultEndpointsProtocol=https;AccountName=myblobstorage6397;AccountKey=nITHnSxOMtUk9jEyq4FeDqJnrPOChIN210p9iOxvo2bkBP9l3jKwwINBbGiEw5kdpuDRwpo4lXUK+ASt0D9zLQ==;EndpointSuffix=core.windows.net'15connect_str = os.getenv('AZURE_STORAGE_CONNECTION_STRING')16container_name = "images"17blob_service_client = BlobServiceClient.from_connection_string(conn_str=connect_str) # create a blob service client to interact with the storage account18try:19 container_client = blob_service_client.get_container_client(container=container_name) # get container client to interact with the container in which images will be stored20 container_client.get_container_properties() # get properties of the container to force exception to be thrown if container does not exist21except Exception as e:22 container_client = blob_service_client.create_container(container_name) # create a container in the storage account if it does not exista23 24def load_image(url):25 response = requests.get(url)26 img = Image.open(BytesIO(response.content))27 return img28 29def image_loader(url):30 img = load_image(url)31 return img32 33def txt2img(x):34 client = InferenceClient()35 image = client.text_to_image(x)36 timestr = time.strftime("%Y%m%d-%H%M%S")37 local_path = "./"38 39 # Upload the created file40 imgfile="txt2img%s.png"%timestr41 print("######################file name %s",imgfile)42 img_saved = image.save(imgfile)43 print("######################file is" ,os.path.abspath(imgfile))44 blob_client = blob_service_client.get_blob_client(container=container_name, blob=imgfile)45 print("\nUploading to Azure Storage as blob:\n\t" + imgfile)46 upload_file_path = os.path.join(local_path, imgfile)47 48 with open(file=upload_file_path, mode="rb") as data:49 blob_client.upload_blob(data)50 blob_items = container_client.list_blobs() # list all the blobs in the container51 for blob in blob_items:52 blob_client = container_client.get_blob_client(blob=imgfile)53 url=blob_client.url54 print("i###################URL OF IMAGE = %s", url)55 return image_loader(url), generate(image_loader(url))56 57 58def img2img(x):59 timestr = time.strftime("%Y%m%d-%H%M%S")60 local_path = "./"61 62 # Upload the created file63 imgfile="img2img%s.png"%timestr64 print("######################file name %s",imgfile)65 img = Image.fromarray(x, "RGB")66 67 img_saved = img.save(imgfile)68 print("######################file is" ,os.path.abspath(imgfile))69 blob_client = blob_service_client.get_blob_client(container=container_name, blob=imgfile)70 print("\nUploading to Azure Storage as blob:\n\t" + imgfile)71 upload_file_path = os.path.join(local_path, imgfile)72 73 with open(file=upload_file_path, mode="rb") as data:74 blob_client.upload_blob(data)75 blob_items = container_client.list_blobs() # list all the blobs in the container76 for blob in blob_items:77 blob_client = container_client.get_blob_client(blob=imgfile)78 url=blob_client.url79 print("i###################URL OF IMAGE = %s", url)80 return image_loader(url), generate(image_loader(url))81 #return image_loader(x), generate(image_loader(x))82 83def imgproc(x):84 limg = load_image(x)85 return np.fliplr(limg)86 87 88title = "Azure Huggingface"89t2i_demo = gr.Interface(fn=txt2img, inputs="text", outputs=["image","image"])90i2i_demo = gr.Interface(fn=img2img, inputs=gr.inputs.Image(label="Image Input 2") , outputs=["image","image"])91 92demo = gr.TabbedInterface([t2i_demo,i2i_demo ], ["Text-to-Image", "Image-to-Image"])93 94#if __name__ == "__main__":95demo.launch()96 