File size: 1,926 Bytes
fda8dae
5781b89
 
 
 
 
1322687
a5ebb82
1322687
5781b89
 
6d0cb8a
12e7969
5781b89
a5ebb82
 
 
 
 
1322687
bac7d5d
5781b89
 
fda8dae
5781b89
a5ebb82
 
 
5781b89
 
 
 
 
 
 
 
 
 
 
 
 
 
6b26249
 
5781b89
 
 
 
485e277
a5ebb82
5781b89
 
 
a5ebb82
5781b89
 
 
 
a5ebb82
5781b89
 
 
a5ebb82
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import spaces
import torch
import re
import gradio as gr
from threading import Thread
from transformers import TextIteratorStreamer, AutoTokenizer, AutoModelForCausalLM
import subprocess

subprocess.run('pip install flash-attn --no-build-isolation', env={'FLASH_ATTENTION_SKIP_CUDA_BUILD': "TRUE"}, shell=True)

model_id = "vikhyatk/moondream2"
revision = "2024-05-20"
tokenizer = AutoTokenizer.from_pretrained(model_id, revision=revision)
moondream = AutoModelForCausalLM.from_pretrained(
    model_id,
    trust_remote_code=True,
    revision=revision,
    torch_dtype=torch.bfloat16,
    device_map={"": "cuda"},
    attn_implementation="flash_attention_2"
)
moondream.eval()

@spaces.GPU(duration=10)
def answer_question(img, prompt):
    if img is None:
        raise gr.Error("Please upload an image.")

    image_embeds = moondream.encode_image(img)
    streamer = TextIteratorStreamer(tokenizer, skip_special_tokens=True)
    thread = Thread(
        target=moondream.answer_question,
        kwargs={
            "image_embeds": image_embeds,
            "question": prompt,
            "tokenizer": tokenizer,
            "streamer": streamer,
        },
    )
    thread.start()
    buffer = ""
    for new_text in streamer:
        buffer += new_text
        yield buffer.strip()

with gr.Blocks() as demo:
    gr.Markdown(
        """
        # myAI - AMI Vision Module
        A lightweight Computer Vision model by @vikhyat - 🌔 [moondream2](https://github.com/vikhyat/moondream)
        """
    )
    with gr.Row():
        prompt = gr.Textbox(label="Input", value="Identify people in this image", scale=4)
        submit = gr.Button("Submit")
    with gr.Row():
        img = gr.Image(type="pil", label="Upload an Image")
        output = gr.TextArea(label="Response")

    submit.click(answer_question, [img, prompt], output)
    prompt.submit(answer_question, [img, prompt], output)

demo.queue().launch()