Spaces:
Running on Zero
Running on Zero
Download app.py from hugging-apps/agronvila: direct link, hf CLI and curl.
- Browser
- Download file 5.14 kB
-
https://huggingface.co/spaces/hugging-apps/agronvila/resolve/main/app.py
- Command line
-
hf download hf://spaces/hugging-apps/agronvila/app.py
-
curl -L -o app.py https://huggingface.co/spaces/hugging-apps/agronvila/resolve/main/app.py
5.14 kB
| 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) | |
| 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( | |
| """ | |
| <div style="text-align: center; margin-bottom: 8px;"> | |
| <h1 style="font-size: 2rem; margin-bottom: 4px;">🌾 AgroNVILA</h1> | |
| <p style="font-size: 1rem; opacity: 0.8; margin-top: 0;"> | |
| Agricultural cross-scale vision-language reasoning from the | |
| <a href="https://huggingface.co/papers/2603.14342" target="_blank">AgroOmni</a> project. | |
| Upload a ground, UAV/drone, or satellite image and ask any question about the scene. | |
| </p> | |
| </div> | |
| """ | |
| ) | |
| with gr.Row(): | |
| with gr.Column(): | |
| image_input = gr.Image(type="pil", label="Agricultural image", height=360) | |
| question_input = gr.Textbox( | |
| label="Question", | |
| placeholder="e.g. What crop is shown in this image, and what stage of growth is it in?", | |
| lines=2, | |
| ) | |
| submit_button = gr.Button("Answer", variant="primary") | |
| with gr.Accordion("Advanced options", open=False): | |
| max_new_tokens_slider = gr.Slider( | |
| minimum=32, maximum=1024, value=512, step=32, label="Max new tokens" | |
| ) | |
| temperature_slider = gr.Slider( | |
| minimum=0.0, maximum=1.5, value=0.2, step=0.05, label="Temperature (when sampling)" | |
| ) | |
| top_p_slider = gr.Slider( | |
| minimum=0.1, maximum=1.0, value=0.9, step=0.05, label="Top-p (when sampling)" | |
| ) | |
| do_sample_checkbox = gr.Checkbox(value=False, label="Enable sampling") | |
| with gr.Column(): | |
| answer_output = gr.Textbox(label="Answer", lines=12, buttons=["copy"]) | |
| submit_button.click( | |
| fn=answer, | |
| inputs=[image_input, question_input, max_new_tokens_slider, temperature_slider, top_p_slider, do_sample_checkbox], | |
| outputs=answer_output, | |
| api_name="answer", | |
| ) | |
| gr.Examples( | |
| examples=[ | |
| ["examples/strawberries_closeup.jpg", "What is shown in this image? Describe the crop and its condition."], | |
| ["examples/sunflower_field.jpg", "What crop is growing in this field? What growth stage does it appear to be in?"], | |
| ["examples/lavender_field.jpg", "Describe the planting pattern of this field and the crop being grown."], | |
| ["examples/landscape.jpg", "What agricultural activity or land use is visible in this image?"], | |
| ], | |
| inputs=[image_input, question_input], | |
| fn=answer, | |
| outputs=answer_output, | |
| cache_examples=True, | |
| cache_mode="lazy", | |
| label="Try an example", | |
| ) | |
| demo.launch(theme=gr.themes.Citrus(), css=CUSTOM_CSS, mcp_server=True) | |