Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -29,33 +29,30 @@ def answer_questions(image_tuples, prompt_text):
|
|
29 |
answers = []
|
30 |
|
31 |
for prompt in prompts:
|
32 |
-
thread = Thread(
|
33 |
-
image_answers = moondream.batch_answer(
|
34 |
images=[img.convert("RGB") for img in image_embeds],
|
35 |
prompts=[prompt] * len(image_embeds),
|
36 |
-
tokenizer=tokenizer
|
37 |
-
)
|
38 |
-
answers.append(image_answers)
|
39 |
-
)
|
40 |
thread.start()
|
|
|
41 |
|
42 |
for i, prompt in enumerate(prompts):
|
43 |
Q_and_A += f"### Q: {prompt}\n"
|
44 |
for j, image_tuple in enumerate(image_tuples):
|
45 |
image_name = f"image{j+1}"
|
46 |
answer_text = answers[i][j]
|
47 |
-
Q_and_A += f"**{image_name} A:** \n {answer_text} \n
|
48 |
|
49 |
result = {'headers': prompts, 'data': answers}
|
50 |
-
print(
|
51 |
return Q_and_A, result
|
52 |
|
53 |
with gr.Blocks() as demo:
|
54 |
gr.Markdown("# moondream2 unofficial batch processing demo")
|
55 |
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")
|
56 |
-
gr.Markdown("**Currently each image will be sent as a batch with the prompts thus asking each
|
57 |
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*")
|
58 |
-
gr.Markdown("
|
59 |
with gr.Row():
|
60 |
img = gr.Gallery(label="Upload Images", type="pil", preview=True, columns=4)
|
61 |
with gr.Row():
|
@@ -66,6 +63,6 @@ with gr.Blocks() as demo:
|
|
66 |
output = gr.Markdown(label="Questions and Answers", line_breaks=True)
|
67 |
with gr.Row():
|
68 |
output2 = gr.Dataframe(label="Structured Dataframe", type="array", wrap=True)
|
69 |
-
submit.click(answer_questions, [img, prompt], [output, output2])
|
70 |
|
71 |
-
demo.queue().launch()
|
|
|
29 |
answers = []
|
30 |
|
31 |
for prompt in prompts:
|
32 |
+
thread = Thread(target=lambda: answers.append(moondream.batch_answer(
|
|
|
33 |
images=[img.convert("RGB") for img in image_embeds],
|
34 |
prompts=[prompt] * len(image_embeds),
|
35 |
+
tokenizer=tokenizer)))
|
|
|
|
|
|
|
36 |
thread.start()
|
37 |
+
thread.join()
|
38 |
|
39 |
for i, prompt in enumerate(prompts):
|
40 |
Q_and_A += f"### Q: {prompt}\n"
|
41 |
for j, image_tuple in enumerate(image_tuples):
|
42 |
image_name = f"image{j+1}"
|
43 |
answer_text = answers[i][j]
|
44 |
+
Q_and_A += f"**{image_name} A:** \n {answer_text} \n"
|
45 |
|
46 |
result = {'headers': prompts, 'data': answers}
|
47 |
+
print("result\n{}\n\nQ_and_A\n{}\n\n".format(result, Q_and_A))
|
48 |
return Q_and_A, result
|
49 |
|
50 |
with gr.Blocks() as demo:
|
51 |
gr.Markdown("# moondream2 unofficial batch processing demo")
|
52 |
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")
|
53 |
+
gr.Markdown("**Currently each image will be sent as a batch with the prompts thus asking each prompt on each image**")
|
54 |
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*")
|
55 |
+
gr.Markdown("A tiny vision language model. [moondream2](https://huggingface.co/vikhyatk/moondream2)")
|
56 |
with gr.Row():
|
57 |
img = gr.Gallery(label="Upload Images", type="pil", preview=True, columns=4)
|
58 |
with gr.Row():
|
|
|
63 |
output = gr.Markdown(label="Questions and Answers", line_breaks=True)
|
64 |
with gr.Row():
|
65 |
output2 = gr.Dataframe(label="Structured Dataframe", type="array", wrap=True)
|
66 |
+
submit.click(answer_questions, inputs=[img, prompt], outputs=[output, output2])
|
67 |
|
68 |
+
demo.queue().launch()
|