Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,65 +1,65 @@
|
|
| 1 |
-
import spaces
|
| 2 |
-
import torch
|
| 3 |
-
import re
|
| 4 |
-
import gradio as gr
|
| 5 |
-
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 6 |
-
from PIL import Image
|
| 7 |
-
|
| 8 |
-
if torch.cuda.is_available():
|
| 9 |
-
device, dtype = "cuda", torch.float16
|
| 10 |
-
else:
|
| 11 |
-
device, dtype = "cpu", torch.float32
|
| 12 |
-
|
| 13 |
-
model_id = "vikhyatk/moondream2"
|
| 14 |
-
revision = "2024-08-26"
|
| 15 |
-
tokenizer = AutoTokenizer.from_pretrained(model_id, revision=revision)
|
| 16 |
-
moondream = AutoModelForCausalLM.from_pretrained(
|
| 17 |
-
model_id, trust_remote_code=True, revision=revision, torch_dtype=dtype
|
| 18 |
-
).to(device=device)
|
| 19 |
-
moondream.eval()
|
| 20 |
-
|
| 21 |
-
@spaces.GPU
|
| 22 |
-
def answer_questions(image_tuples, prompt_text):
|
| 23 |
-
result = ""
|
| 24 |
-
Q_and_A = ""
|
| 25 |
-
prompts = [p.strip() for p in prompt_text.split(',')]
|
| 26 |
-
image_embeds = [img[0] for img in image_tuples if img[0] is not None]
|
| 27 |
-
|
| 28 |
-
#print(f"\nprompts: {prompts}\n\n")
|
| 29 |
-
answers = []
|
| 30 |
-
for prompt in prompts:
|
| 31 |
-
image_answers = moondream.batch_answer(
|
| 32 |
-
images=[img.convert("RGB") for img in image_embeds],
|
| 33 |
-
prompts=[prompt] * len(image_embeds),
|
| 34 |
-
tokenizer=tokenizer,
|
| 35 |
-
)
|
| 36 |
-
answers.append(image_answers)
|
| 37 |
-
|
| 38 |
-
for i, prompt in enumerate(prompts):
|
| 39 |
-
Q_and_A += f"### Q: {prompt}\n"
|
| 40 |
-
for j, image_tuple in enumerate(image_tuples):
|
| 41 |
-
image_name = f"image{j+1}"
|
| 42 |
-
answer_text = answers[i][j]
|
| 43 |
-
Q_and_A += f"**{image_name} A:** \n {answer_text} \n\n"
|
| 44 |
-
|
| 45 |
-
result = {'headers': prompts, 'data': answers}
|
| 46 |
-
#print(f"result\n{result}\n\nQ_and_A\n{Q_and_A}\n\n")
|
| 47 |
-
return Q_and_A, result
|
| 48 |
-
|
| 49 |
-
with gr.Blocks() as demo:
|
| 50 |
-
gr.Markdown("# MoonDream WebUI")
|
| 51 |
-
gr.Markdown("## π
|
| 52 |
-
gr.Markdown("## π moondream2
|
| 53 |
-
with gr.Row():
|
| 54 |
-
img = gr.Gallery(label="Upload Images", type="pil", preview=True, columns=4)
|
| 55 |
-
with gr.Row():
|
| 56 |
-
prompt = gr.Textbox(label="Input Prompts", placeholder="Enter prompts (one prompt for each image provided) separated by commas. Ex: Describe this image, What is in this image?", lines=8)
|
| 57 |
-
with gr.Row():
|
| 58 |
-
submit = gr.Button("Submit")
|
| 59 |
-
with gr.Row():
|
| 60 |
-
output = gr.Markdown(label="Questions and Answers", line_breaks=True)
|
| 61 |
-
with gr.Row():
|
| 62 |
-
output2 = gr.Dataframe(label="Structured Dataframe", type="array", wrap=True)
|
| 63 |
-
submit.click(answer_questions, [img, prompt], [output, output2])
|
| 64 |
-
|
| 65 |
-
demo.queue().launch()
|
|
|
|
| 1 |
+
import spaces
|
| 2 |
+
import torch
|
| 3 |
+
import re
|
| 4 |
+
import gradio as gr
|
| 5 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 6 |
+
from PIL import Image
|
| 7 |
+
|
| 8 |
+
if torch.cuda.is_available():
|
| 9 |
+
device, dtype = "cuda", torch.float16
|
| 10 |
+
else:
|
| 11 |
+
device, dtype = "cpu", torch.float32
|
| 12 |
+
|
| 13 |
+
model_id = "vikhyatk/moondream2"
|
| 14 |
+
revision = "2024-08-26"
|
| 15 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id, revision=revision)
|
| 16 |
+
moondream = AutoModelForCausalLM.from_pretrained(
|
| 17 |
+
model_id, trust_remote_code=True, revision=revision, torch_dtype=dtype
|
| 18 |
+
).to(device=device)
|
| 19 |
+
moondream.eval()
|
| 20 |
+
|
| 21 |
+
@spaces.GPU
|
| 22 |
+
def answer_questions(image_tuples, prompt_text):
|
| 23 |
+
result = ""
|
| 24 |
+
Q_and_A = ""
|
| 25 |
+
prompts = [p.strip() for p in prompt_text.split(',')]
|
| 26 |
+
image_embeds = [img[0] for img in image_tuples if img[0] is not None]
|
| 27 |
+
|
| 28 |
+
#print(f"\nprompts: {prompts}\n\n")
|
| 29 |
+
answers = []
|
| 30 |
+
for prompt in prompts:
|
| 31 |
+
image_answers = moondream.batch_answer(
|
| 32 |
+
images=[img.convert("RGB") for img in image_embeds],
|
| 33 |
+
prompts=[prompt] * len(image_embeds),
|
| 34 |
+
tokenizer=tokenizer,
|
| 35 |
+
)
|
| 36 |
+
answers.append(image_answers)
|
| 37 |
+
|
| 38 |
+
for i, prompt in enumerate(prompts):
|
| 39 |
+
Q_and_A += f"### Q: {prompt}\n"
|
| 40 |
+
for j, image_tuple in enumerate(image_tuples):
|
| 41 |
+
image_name = f"image{j+1}"
|
| 42 |
+
answer_text = answers[i][j]
|
| 43 |
+
Q_and_A += f"**{image_name} A:** \n {answer_text} \n\n"
|
| 44 |
+
|
| 45 |
+
result = {'headers': prompts, 'data': answers}
|
| 46 |
+
#print(f"result\n{result}\n\nQ_and_A\n{Q_and_A}\n\n")
|
| 47 |
+
return Q_and_A, result
|
| 48 |
+
|
| 49 |
+
with gr.Blocks() as demo:
|
| 50 |
+
gr.Markdown("# MoonDream WebUI")
|
| 51 |
+
gr.Markdown("## π WebUI is modify by https://huggingface.co/spaces/Csplk/moondream2-batch-processing")
|
| 52 |
+
gr.Markdown("## π moondream2 - A tiny vision language model. [GitHub](https://github.com/vikhyatk/moondream)")
|
| 53 |
+
with gr.Row():
|
| 54 |
+
img = gr.Gallery(label="Upload Images", type="pil", preview=True, columns=4)
|
| 55 |
+
with gr.Row():
|
| 56 |
+
prompt = gr.Textbox(label="Input Prompts", placeholder="Enter prompts (one prompt for each image provided) separated by commas. Ex: Describe this image, What is in this image?", lines=8)
|
| 57 |
+
with gr.Row():
|
| 58 |
+
submit = gr.Button("Submit")
|
| 59 |
+
with gr.Row():
|
| 60 |
+
output = gr.Markdown(label="Questions and Answers", line_breaks=True)
|
| 61 |
+
with gr.Row():
|
| 62 |
+
output2 = gr.Dataframe(label="Structured Dataframe", type="array", wrap=True)
|
| 63 |
+
submit.click(answer_questions, [img, prompt], [output, output2])
|
| 64 |
+
|
| 65 |
+
demo.queue().launch()
|