|
| 1 | +# Copyright (c) 2023, NVIDIA CORPORATION. All rights reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import base64 |
| 16 | +import io |
| 17 | + |
| 18 | +import gradio as gr |
| 19 | +import PIL.Image |
| 20 | +from omegaconf import OmegaConf |
| 21 | + |
| 22 | +from nemo.collections.multimodal.parts.utils import create_neva_model_and_processor |
| 23 | + |
| 24 | +CFG_STRING = """ |
| 25 | +trainer: |
| 26 | + devices: 1 |
| 27 | + num_nodes: 1 |
| 28 | + accelerator: gpu |
| 29 | + logger: False # logger provided by exp_manager |
| 30 | + precision: bf16 # 16, 32, or bf16 |
| 31 | +
|
| 32 | +inference: |
| 33 | + greedy: False # Whether or not to use sampling ; use greedy decoding otherwise |
| 34 | + top_k: 0 # The number of highest probability vocabulary tokens to keep for top-k-filtering. |
| 35 | + top_p: 0.9 # If set to float < 1, only the most probable tokens with probabilities that add up to top_p or higher are kept for generation. |
| 36 | + temperature: 0.2 # sampling temperature |
| 37 | + add_BOS: False # add the bos token at the begining of the prompt |
| 38 | + tokens_to_generate: 256 # The minimum length of the sequence to be generated. |
| 39 | + all_probs: False # whether return the log prob for all the tokens in vocab |
| 40 | + repetition_penalty: 1.2 # The parameter for repetition penalty. 1.0 means no penalty. |
| 41 | + min_tokens_to_generate: 0 # The minimum length of the sequence to be generated. |
| 42 | + compute_logprob: False # a flag used to compute logprob of all the input text, a very special case of running inference, default False |
| 43 | + end_strings: ["<extra_id_1>","<extra_id_7>",] # generation will stop when one of these tokens is generated |
| 44 | + images_base_path: /pwd/images |
| 45 | + insert_image_token: null # `left` or `right` or `null` |
| 46 | +
|
| 47 | +cluster_type: BCP |
| 48 | +tensor_model_parallel_size: 1 |
| 49 | +pipeline_model_parallel_size: 1 |
| 50 | +pipeline_model_parallel_split_rank: 0 # used for encoder and decoder model (0 for others) |
| 51 | +
|
| 52 | +neva_model_file: /pwd/nemo_experiments/nemo_llava.nemo #neva_22b_tp8_finetuned_v1.nemo neva_8b_tp4_finetuned_v1.nemo |
| 53 | +base_model_file: null |
| 54 | +checkpoint_dir: null #/pwd/nemo_multimodal/nemo_experiments/nemo_llava_finetune/checkpoints # checkpoint file dir. This is used to load the PTL checkpoint generated during the Kosmos training |
| 55 | +checkpoint_name: null #megatron_clip--val_loss=0.41-step=13499-consumed_samples=431904.0.ckpt # PTL checkpoint file name, only used for PTL checkpoint loading |
| 56 | +hparams_file: null #/pwd/nemo_multimodal/nemo_experiments/nemo_llava_finetune/version_0/hparams.yaml # model configuration file, only used for PTL checkpoint loading |
| 57 | +""" |
| 58 | + |
| 59 | +cfg = OmegaConf.create(CFG_STRING) |
| 60 | +cfg.neva_model_file = "/path/to/llava-v1.5-7b.nemo" |
| 61 | +model, image_processor = create_neva_model_and_processor(cfg) |
| 62 | + |
| 63 | + |
| 64 | +def predict(prompt, image_base64=None): |
| 65 | + input_data = {"prompt": prompt} |
| 66 | + if image_base64 is not None: |
| 67 | + image_data = base64.b64decode(image_base64) |
| 68 | + # image = PIL.Image.fromarray(image) |
| 69 | + image = PIL.Image.open(io.BytesIO(image_data)) |
| 70 | + input_data["image"] = image_processor(image) |
| 71 | + |
| 72 | + length_params: LengthParam = { |
| 73 | + "max_length": cfg.inference.tokens_to_generate, |
| 74 | + "min_length": cfg.inference.min_tokens_to_generate, |
| 75 | + } |
| 76 | + sampling_params: SamplingParam = { |
| 77 | + "use_greedy": cfg.inference.greedy, |
| 78 | + "temperature": cfg.inference.temperature, |
| 79 | + "top_k": cfg.inference.top_k, |
| 80 | + "top_p": cfg.inference.top_p, |
| 81 | + "repetition_penalty": cfg.inference.repetition_penalty, |
| 82 | + "add_BOS": cfg.inference.add_BOS, |
| 83 | + "all_probs": cfg.inference.all_probs, |
| 84 | + "compute_logprob": cfg.inference.compute_logprob, |
| 85 | + "end_strings": cfg.inference.end_strings, |
| 86 | + } |
| 87 | + |
| 88 | + # Generate model responses |
| 89 | + responses = model.generate( |
| 90 | + input_prompts=[input_data], # Adjust based on your model's requirements |
| 91 | + length_params=length_params, # Define these parameters as in your original code |
| 92 | + sampling_params=sampling_params, # Define these parameters as in your original code |
| 93 | + inference_config=cfg, |
| 94 | + ) |
| 95 | + |
| 96 | + return responses[0]["clean_response"] |
| 97 | + |
| 98 | + |
| 99 | +iface = gr.Interface( |
| 100 | + fn=predict, |
| 101 | + inputs=[gr.Textbox(), gr.Textbox()], |
| 102 | + outputs="text", |
| 103 | + title="Multimodal Model Inference", |
| 104 | + description="Enter a prompt and optionally upload an image for model inference.", |
| 105 | +) |
| 106 | + |
| 107 | +if __name__ == "__main__": |
| 108 | + iface.launch(server_port=8890, share=False) |
0 commit comments