Update app.py
Browse files
app.py
CHANGED
|
@@ -17,41 +17,7 @@ moondream = AutoModelForCausalLM.from_pretrained(
|
|
| 17 |
)
|
| 18 |
moondream.compile()
|
| 19 |
|
| 20 |
-
|
| 21 |
-
image = Image.open("complex_scene.jpg")
|
| 22 |
-
encoded = moondream.encode_image(image)
|
| 23 |
-
|
| 24 |
-
# Reuse the encoding for multiple queries
|
| 25 |
-
questions = [
|
| 26 |
-
"How many people are in this image?",
|
| 27 |
-
"What time of day was this taken?",
|
| 28 |
-
"What's the weather like?"
|
| 29 |
-
]
|
| 30 |
-
|
| 31 |
-
for q in questions:
|
| 32 |
-
result = moondream.query(image=encoded, question=q, reasoning=False)
|
| 33 |
-
print(f"Q: {q}")
|
| 34 |
-
print(f"A: {result['answer']}\n")
|
| 35 |
-
|
| 36 |
-
# Also works with other skills
|
| 37 |
-
caption = moondream.caption(encoded, length="normal")
|
| 38 |
-
objects = moondream.detect(encoded, "poop")
|
| 39 |
-
pointe = moondream.point(encoded, "grass")
|
| 40 |
-
print(f"caption: {e}, objects:{g}, point:{h}")
|
| 41 |
-
|
| 42 |
-
# Segment an object
|
| 43 |
-
result = moondream.segment(image, "cat")
|
| 44 |
-
svg_path = result["path"]
|
| 45 |
-
bbox = result["bbox"]
|
| 46 |
-
|
| 47 |
-
print(f"SVG Path: {svg_path[:100]}...")
|
| 48 |
-
print(f"Bounding box: {bbox}")
|
| 49 |
-
|
| 50 |
-
# With spatial hint (point) to guide segmentation
|
| 51 |
-
result = model.segment(image, "cat", spatial_refs=[[0.5, 0.3]])
|
| 52 |
-
|
| 53 |
-
# With spatial hint (bounding box)
|
| 54 |
-
result = model.segment(image, "cat", spatial_refs=[[0.2, 0.1, 0.8, 0.9]])
|
| 55 |
|
| 56 |
"""
|
| 57 |
#model_id = "vikhyatk/moondream2"
|
|
@@ -83,6 +49,42 @@ model = AutoModelForCausalLM.from_pretrained(
|
|
| 83 |
|
| 84 |
@spaces.GPU(durtion="150")
|
| 85 |
def answer_questions(image_tuples, prompt_text):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
result = ""
|
| 87 |
Q_and_A = ""
|
| 88 |
prompts = [p.strip() for p in prompt_text.split('?')]
|
|
|
|
| 17 |
)
|
| 18 |
moondream.compile()
|
| 19 |
|
| 20 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
"""
|
| 23 |
#model_id = "vikhyatk/moondream2"
|
|
|
|
| 49 |
|
| 50 |
@spaces.GPU(durtion="150")
|
| 51 |
def answer_questions(image_tuples, prompt_text):
|
| 52 |
+
# Encode image once
|
| 53 |
+
image = Image.open("complex_scene.jpg")
|
| 54 |
+
encoded = moondream.encode_image(image)
|
| 55 |
+
|
| 56 |
+
# Reuse the encoding for multiple queries
|
| 57 |
+
questions = [
|
| 58 |
+
"How many people are in this image?",
|
| 59 |
+
"What time of day was this taken?",
|
| 60 |
+
"What's the weather like?"
|
| 61 |
+
]
|
| 62 |
+
|
| 63 |
+
for q in questions:
|
| 64 |
+
result = moondream.query(image=encoded, question=q, reasoning=False)
|
| 65 |
+
print(f"Q: {q}")
|
| 66 |
+
print(f"A: {result['answer']}\n")
|
| 67 |
+
|
| 68 |
+
# Also works with other skills
|
| 69 |
+
caption = moondream.caption(encoded, length="normal")
|
| 70 |
+
objects = moondream.detect(encoded, "poop")
|
| 71 |
+
pointe = moondream.point(encoded, "grass")
|
| 72 |
+
print(f"caption: {e}, objects:{g}, point:{h}")
|
| 73 |
+
|
| 74 |
+
# Segment an object
|
| 75 |
+
result = moondream.segment(image, "cat")
|
| 76 |
+
svg_path = result["path"]
|
| 77 |
+
bbox = result["bbox"]
|
| 78 |
+
|
| 79 |
+
print(f"SVG Path: {svg_path[:100]}...")
|
| 80 |
+
print(f"Bounding box: {bbox}")
|
| 81 |
+
|
| 82 |
+
# With spatial hint (point) to guide segmentation
|
| 83 |
+
result = model.segment(image, "cat", spatial_refs=[[0.5, 0.3]])
|
| 84 |
+
|
| 85 |
+
# With spatial hint (bounding box)
|
| 86 |
+
result = model.segment(image, "cat", spatial_refs=[[0.2, 0.1, 0.8, 0.9]])
|
| 87 |
+
|
| 88 |
result = ""
|
| 89 |
Q_and_A = ""
|
| 90 |
prompts = [p.strip() for p in prompt_text.split('?')]
|