Spaces:
Build error
Build error
caption using an auxiliary space if on spaces (#3)
Browse files- caption using an auxiliary space if on spaces (bc82dbcf33a595d9d5f60222daba2e0cfdfe2afa)
- Update requirements.txt (e881eb450784b8e8cc3cdef6b2cf34dce5bf14d5)
- Update requirements_local.txt (03a86ee3a538067a432ed7b47e5af526a1c556c0)
- Update requirements.txt (acf3b82e1699354a0f39644a5b656fc19c4f338f)
Files changed (3)
3 unmodified lines4567891011121314151617181920216 unmodified lines2829303132333435361 unmodified line3839404142434426 unmodified lines7172737475767734 unmodified lines11211311411511611711811912012112212312422 unmodified lines147148149150151152153154393 unmodified lines5485495505515525535543 unmodified linesfrom huggingface_hub import whoamiis_spaces = True if os.environ.get("SPACE_ID") else Falseis_canonical = True if os.environ.get("SPACE_ID") == "autotrain-projects/train-flux-lora-ease" else Falseif is_spaces:subprocess.run('pip install flash-attn --no-build-isolation', env={'FLASH_ATTENTION_SKIP_CUDA_BUILD': "TRUE"}, shell=True)import spacesos.environ["HF_HUB_ENABLE_HF_TRANSFER"] = "1"import sysfrom dotenv import load_dotenvload_dotenv()# Add the current working directory to the Python pathsys.path.insert(0, os.getcwd())6 unmodified linesimport jsonimport yamlfrom slugify import slugifyfrom transformers import AutoProcessor, AutoModelForCausalLMif not is_spaces:sys.path.insert(0, "ai-toolkit")from toolkit.job import get_jobgr.OAuthProfile = None1 unmodified lineMAX_IMAGES = 150def load_captioning(uploaded_files, concept_sentence):uploaded_images = [file for file in uploaded_files if not file.endswith('.txt')]txt_files = [file for file in uploaded_files if file.endswith('.txt')]26 unmodified linesprint(base_name)print(image_value)if base_name in txt_files_dict:print("entrou")with open(txt_files_dict[base_name], 'r') as file:corresponding_caption = file.read()34 unmodified linesreturn destination_folderdef run_captioning(images, concept_sentence, *captions):device = "cuda" if torch.cuda.is_available() else "cpu"torch_dtype = torch.float16model = AutoModelForCausalLM.from_pretrained("microsoft/Florence-2-large", torch_dtype=torch_dtype, trust_remote_code=True).to(device)processor = AutoProcessor.from_pretrained("microsoft/Florence-2-large", trust_remote_code=True)captions = list(captions)for i, image_path in enumerate(images):22 unmodified linesdel modeldel processorif is_spaces:run_captioning = spaces.GPU()(run_captioning)def recursive_update(d, u):for k, v in u.items():393 unmodified linesoutputs=progress_area,)do_captioning.click(fn=run_captioning, inputs=[images, concept_sentence] + caption_list, outputs=caption_list)demo.load(fn=swap_visibilty, outputs=main_ui)if __name__ == "__main__":3 unmodified lines456789101112136 unmodified lines202122232425262728293031321 unmodified line34353637383926 unmodified lines66676869707134 unmodified lines10610710810911011111211311411511611711822 unmodified lines141142143144145146147148149150151152153154155156157158159160161162163164165166393 unmodified lines5605615625635645655663 unmodified linesfrom huggingface_hub import whoamiis_spaces = True if os.environ.get("SPACE_ID") else Falseis_canonical = True if os.environ.get("SPACE_ID") == "autotrain-projects/train-flux-lora-ease" else Falseos.environ["HF_HUB_ENABLE_HF_TRANSFER"] = "1"import sys# Add the current working directory to the Python pathsys.path.insert(0, os.getcwd())6 unmodified linesimport jsonimport yamlfrom slugify import slugifyif is_spaces:from gradio_client import Client, handle_fileclient = Client("multimodalart/Florence-2-l4")if not is_spaces:from transformers import AutoProcessor, AutoModelForCausalLMsys.path.insert(0, "ai-toolkit")from toolkit.job import get_jobgr.OAuthProfile = None1 unmodified lineMAX_IMAGES = 150def load_captioning(uploaded_files, concept_sentence):uploaded_images = [file for file in uploaded_files if not file.endswith('.txt')]txt_files = [file for file in uploaded_files if file.endswith('.txt')]26 unmodified linesprint(base_name)print(image_value)if base_name in txt_files_dict:with open(txt_files_dict[base_name], 'r') as file:corresponding_caption = file.read()34 unmodified linesreturn destination_folderdef run_captioning_local(images, concept_sentence, *captions):device = "cuda" if torch.cuda.is_available() else "cpu"torch_dtype = torch.float16model = AutoModelForCausalLM.from_pretrained("multimodalart/Florence-2-large-no-flash-attn", torch_dtype=torch_dtype, trust_remote_code=True).to(device)processor = AutoProcessor.from_pretrained("multimodalart/Florence-2-large-no-flash-attn", trust_remote_code=True)captions = list(captions)for i, image_path in enumerate(images):22 unmodified linesdel modeldel processordef run_captioning_spaces(images, concept_sentence, *captions):captions = list(captions)for i, image_path in enumerate(images):print(captions[i])if isinstance(image_path, str): # If image is a file pathimage = Image.open(image_path).convert("RGB")answer = client.predict(image=handle_file(image_path),task_prompt="Detailed Caption",text_input=None,api_name="/process_image")[0].replace("'", '"')parsed_answer = json.loads(answer)caption_text = parsed_answer["<DETAILED_CAPTION>"].replace("The image shows ", "")if concept_sentence:caption_text = f"{caption_text} [trigger]"captions[i] = caption_textyield captionsdef recursive_update(d, u):for k, v in u.items():393 unmodified linesoutputs=progress_area,)do_captioning.click(fn=run_captioning_spaces if is_spaces else run_captioning_local, inputs=[images, concept_sentence] + caption_list, outputs=caption_list)demo.load(fn=swap_visibilty, outputs=main_ui)if __name__ == "__main__":
1234567transformersacceleratepython-slugifypython-dotenveinopstimmautotrain-advanced1234python-slugifyeinopstimmautotrain-advanced
12345gradiopython-slugifypython-dotenvflash-attnhuggingface_hub123gradiopython-slugifyhuggingface_hub
app.py+29-17
requirements.txt-3
requirements_local.txt-2