import gradio as gr from transformers import pipeline import torch from PIL import Image # Load models pipe = pipeline("image-to-text", model="Salesforce/blip-image-captioning-base") translator = pipeline(task="translation", model="facebook/nllb-200-distilled-600M", torch_dtype=torch.bfloat16) #process sketch input def process_image(image, shouldConvert=False): if shouldConvert: new_img = Image.new('RGB', size=(image.width, image.height), color=(255, 255, 255)) new_img.paste(image, (0, 0), mask=image) image = new_img return image def parse_input(image, sketchpad, state): current_tab_index = state["tab_index"] new_image = None # Upload if current_tab_index == 0: if image is not None: new_image = process_image(image) # Sketch elif current_tab_index == 1: #print(sketchpad) if sketchpad and sketchpad["composite"]: new_image = process_image(sketchpad["composite"], True) # send to pipeline Eng_txt = pipe(new_image) to_Ar_txt = str(Eng_txt[0]['generated_text']) text_translated = translator(to_Ar_txt, src_lang="eng_Latn", tgt_lang="arz_Arab") return Eng_txt[0]['generated_text'], text_translated[0]['translation_text'] #select tab def tabs_select(e: gr.SelectData, _state): _state["tab_index"] = e.index example_img_paths = [["https://4.img-dpreview.com/files/p/E~TS590x0~articles/3925134721/0266554465.jpeg"], ["https://images4.alphacoders.com/688/688832.jpg"]] with gr.Blocks() as iface: gr.HTML("""
![]()
""") gr.HTML("""