Spaces:
Runtime error
Runtime error
app
Browse files
back.py
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import spaces
|
| 2 |
+
import torch
|
| 3 |
+
import re
|
| 4 |
+
import gradio as gr
|
| 5 |
+
from threading import Thread
|
| 6 |
+
from transformers import TextIteratorStreamer, AutoTokenizer, AutoModelForCausalLM
|
| 7 |
+
from PIL import ImageDraw
|
| 8 |
+
from torchvision.transforms.v2 import Resize
|
| 9 |
+
import subprocess
|
| 10 |
+
|
| 11 |
+
#subprocess.run('pip install flash-attn --no-build-isolation', env={'FLASH_ATTENTION_SKIP_CUDA_BUILD': "TRUE"}, shell=True)
|
| 12 |
+
|
| 13 |
+
#subprocess.run('cp -r moondream/torch clients/python/moondream/torch')
|
| 14 |
+
#subprocess.run('pip install moondream[gpu]')
|
| 15 |
+
|
| 16 |
+
#def load_moondream():
|
| 17 |
+
# """Load Moondream model and tokenizer."""
|
| 18 |
+
# model = AutoModelForCausalLM.from_pretrained(
|
| 19 |
+
# "vikhyatk/moondream2", trust_remote_code=True, device_map={"": "cuda"}
|
| 20 |
+
# )
|
| 21 |
+
# tokenizer = AutoTokenizer.from_pretrained("vikhyatk/moondream2")
|
| 22 |
+
# return model, tokenizer
|
| 23 |
+
|
| 24 |
+
"""Load Moondream model and tokenizer."""
|
| 25 |
+
moondream = AutoModelForCausalLM.from_pretrained(
|
| 26 |
+
"vikhyatk/moondream2", trust_remote_code=True, device_map={"": "cuda"}
|
| 27 |
+
)
|
| 28 |
+
tokenizer = AutoTokenizer.from_pretrained("vikhyatk/moondream2")
|
| 29 |
+
|
| 30 |
+
#model_id = "vikhyatk/moondream2"
|
| 31 |
+
#revision = "2025-01-09"
|
| 32 |
+
#tokenizer = AutoTokenizer.from_pretrained(model_id, revision=revision)
|
| 33 |
+
#moondream = AutoModelForCausalLM.from_pretrained(
|
| 34 |
+
# model_id, trust_remote_code=True, revision=revision,
|
| 35 |
+
# torch_dtype=torch.bfloat16, device_map={"": "cuda"},
|
| 36 |
+
#)
|
| 37 |
+
|
| 38 |
+
#moondream.eval()
|
| 39 |
+
|
| 40 |
+
@spaces.GPU(durtion="150")
|
| 41 |
+
def answer_questions(image_tuples, prompt_text):
|
| 42 |
+
result = ""
|
| 43 |
+
Q_and_A = ""
|
| 44 |
+
prompts = [p.strip() for p in prompt_text.split('?')]
|
| 45 |
+
image_embeds = [img[0] for img in image_tuples if img[0] is not None]
|
| 46 |
+
answers = []
|
| 47 |
+
|
| 48 |
+
for prompt in prompts:
|
| 49 |
+
answers.append(moondream.batch_answer(
|
| 50 |
+
images=[img.convert("RGB") for img in image_embeds],
|
| 51 |
+
prompts=[prompt] * len(image_embeds),
|
| 52 |
+
tokenizer=tokenizer
|
| 53 |
+
))
|
| 54 |
+
|
| 55 |
+
for i, prompt in enumerate(prompts):
|
| 56 |
+
Q_and_A += f"### Q: {prompt}\n"
|
| 57 |
+
for j, image_tuple in enumerate(image_tuples):
|
| 58 |
+
image_name = f"image{j+1}"
|
| 59 |
+
answer_text = answers[i][j]
|
| 60 |
+
Q_and_A += f"**{image_name} A:** \n {answer_text} \n"
|
| 61 |
+
|
| 62 |
+
result = {'headers': prompts, 'data': answers}
|
| 63 |
+
#print("result\n{}\n\nQ_and_A\n{}\n\n".format(result, Q_and_A))
|
| 64 |
+
return Q_and_A, result
|
| 65 |
+
|
| 66 |
+
with gr.Blocks() as demo:
|
| 67 |
+
gr.Markdown("# moondream2 unofficial batch processing demo")
|
| 68 |
+
gr.Markdown("1. Select images\n2. Enter one or more prompts separated by commas. Ex: Describe this image, What is in this image?\n\n")
|
| 69 |
+
gr.Markdown("**Currently each image will be sent as a batch with the prompts thus asking each prompt on each image**")
|
| 70 |
+
gr.Markdown("*Running on free CPU space tier currently so results may take a bit to process compared to duplicating space and using GPU space hardware*")
|
| 71 |
+
gr.Markdown("A tiny vision language model. [moondream2](https://huggingface.co/vikhyatk/moondream2)")
|
| 72 |
+
with gr.Row():
|
| 73 |
+
img = gr.Gallery(label="Upload Images", type="pil", preview=True, columns=4)
|
| 74 |
+
with gr.Row():
|
| 75 |
+
prompt = gr.Textbox(label="Input Prompts", placeholder="Enter prompts (one prompt for each image provided) separated by question marks. Ex: Describe this image? What is in this image?", lines=8)
|
| 76 |
+
with gr.Row():
|
| 77 |
+
submit = gr.Button("Submit")
|
| 78 |
+
with gr.Row():
|
| 79 |
+
output = gr.Markdown(label="Questions and Answers", line_breaks=True)
|
| 80 |
+
with gr.Row():
|
| 81 |
+
output2 = gr.Dataframe(label="Structured Dataframe", type="array", wrap=True)
|
| 82 |
+
submit.click(answer_questions, inputs=[img, prompt], outputs=[output, output2])
|
| 83 |
+
|
| 84 |
+
demo.queue().launch()
|