import spaces # noqa: F401 (ZeroGPU: must be imported before torch) import copy import time import gradio as gr import torch from PIL import Image from llava.model.builder import load_pretrained_model MODEL_PATH = "AgroOmni/AgroNVILA-SFT" MODEL_NAME = "AgroNVILA-SFT" print(f"[app] loading {MODEL_PATH} ...", flush=True) tokenizer, model, image_processor, context_len = load_pretrained_model( MODEL_PATH, MODEL_NAME, None, device="cpu", ) model.to("cuda") model.eval() print("[app] model loaded and moved to cuda.", flush=True) @spaces.GPU(duration=30) def answer( image: Image.Image, question: str, max_new_tokens: int = 512, temperature: float = 0.2, top_p: float = 0.9, do_sample: bool = False, progress=gr.Progress(), ): """Answer an agricultural question about an image with AgroNVILA. Args: image: Input image (ground, UAV/drone, or satellite view of an agricultural scene). question: Question to ask about the image. max_new_tokens: Maximum number of new tokens to generate. temperature: Sampling temperature (used only when do_sample is enabled). top_p: Nucleus sampling probability (used only when do_sample is enabled). do_sample: If True, sample tokens instead of greedy decoding. Returns: str: The model's answer. """ if image is None: return "Please upload an image first." if not question or not question.strip(): return "Please enter a question." if image.mode != "RGB": image = image.convert("RGB") generation_config = copy.deepcopy(model.default_generation_config) generation_config.max_new_tokens = int(max_new_tokens) generation_config.do_sample = bool(do_sample) if do_sample: generation_config.temperature = float(temperature) generation_config.top_p = float(top_p) else: generation_config.temperature = 1.0 generation_config.top_p = 1.0 t0 = time.time() response = model.generate_content([image, question.strip()], generation_config=generation_config) elapsed = time.time() - t0 print(f"[app] generate_content took {elapsed:.1f}s", flush=True) return response CUSTOM_CSS = """ .gradio-container {max-width: 1200px !important; margin: 0 auto !important;} """ with gr.Blocks(title="AgroNVILA — Agricultural Visual Question Answering") as demo: gr.HTML( """
Agricultural cross-scale vision-language reasoning from the AgroOmni project. Upload a ground, UAV/drone, or satellite image and ask any question about the scene.