Spaces:
Running
on
Zero
Running
on
Zero
File size: 1,942 Bytes
6a8ca1f 04fc1f1 6a8ca1f e9cc0b5 6a8ca1f 04fc1f1 e27d897 6a8ca1f 5c1fe55 e27d897 5c1fe55 e27d897 6a8ca1f e27d897 8a8a62b e27d897 8a8a62b e27d897 8a8a62b 6a8ca1f |
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 |
import spaces
import torch
import re
import gradio as gr
from transformers import AutoTokenizer, AutoModelForCausalLM
from PIL import Image
if torch.cuda.is_available():
device, dtype = "cuda", torch.float16
else:
device, dtype = "cpu", torch.float32
model_id = "vikhyatk/moondream2"
revision = "2024-04-02"
tokenizer = AutoTokenizer.from_pretrained(model_id, revision=revision)
moondream = AutoModelForCausalLM.from_pretrained(
model_id, trust_remote_code=True, revision=revision, torch_dtype=dtype
).to(device=device)
moondream.eval()
@spaces.GPU(duration=10)
def answer_questions(image_tuples, prompt_text):
print(f"prompt_text:\n{prompt_text}\n")
print(f"image_tuples:\n{image_tuples}\n")
prompts = [p.strip() for p in prompt_text.split(',')] # Splitting and cleaning prompts
image_embeds = [img[0] for img in image_tuples if img[0] is not None] # Extracting images from tuples, ignoring None
print(f"image_embeds:\n{image_embeds}\n")
print(f"split prompts:\n{prompts}\n")
answers = moondream.batch_answer(
images=image_embeds,
prompts=prompts,
tokenizer=tokenizer,
)
result = ""
for question, answer in zip(prompts, answers):
print(f"Q: {question}")
print(f"A: {answer}")
print()
result += (f"Q: {question}\nA: {answer}\n\n")
return result
with gr.Blocks() as demo:
gr.Markdown("# π moondream2\nA tiny vision language model. [GitHub](https://github.com/vikhyatk/moondream)")
with gr.Row():
img = gr.Gallery(label="Upload Images", type="pil")
prompt = gr.Textbox(label="Input Prompts", placeholder="Enter prompts separated by commas. Ex: Describe this image, What is in this image?", lines=2)
submit = gr.Button("Submit")
output = gr.TextArea(label="Responses", lines=4)
submit.click(answer_questions, [img, prompt], output)
demo.queue().launch()
|