Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import moondream as md
|
| 3 |
+
import os
|
| 4 |
+
|
| 5 |
+
moondream_api_key = os.getenv("MOONDREAM_API_KEY")
|
| 6 |
+
model = md.vl(api_key=moondream_api_key)
|
| 7 |
+
|
| 8 |
+
def answer_question(img, prompt):
|
| 9 |
+
buffer = ""
|
| 10 |
+
for chunk in model.query(img, prompt, stream=True)["answer"]:
|
| 11 |
+
buffer += chunk
|
| 12 |
+
yield buffer
|
| 13 |
+
|
| 14 |
+
def process_answer(img, answer):
|
| 15 |
+
return gr.update(visible=False, value=None)
|
| 16 |
+
|
| 17 |
+
with gr.Blocks() as demo:
|
| 18 |
+
gr.Markdown(
|
| 19 |
+
"""
|
| 20 |
+
# 🌔 moondream2
|
| 21 |
+
A tiny vision language model. Check out other capabilities (object detection, pointing etc.) in the [Moondream Playground](https://moondream.ai/playground).
|
| 22 |
+
"""
|
| 23 |
+
)
|
| 24 |
+
with gr.Row():
|
| 25 |
+
prompt = gr.Textbox(label="Input", value="Describe this image.", scale=4)
|
| 26 |
+
submit = gr.Button("Submit")
|
| 27 |
+
with gr.Row():
|
| 28 |
+
img = gr.Image(type="pil", label="Upload an Image")
|
| 29 |
+
with gr.Column():
|
| 30 |
+
output = gr.Markdown(label="Response")
|
| 31 |
+
ann = gr.Image(visible=False, label="Annotated Image")
|
| 32 |
+
|
| 33 |
+
submit.click(answer_question, [img, prompt], output)
|
| 34 |
+
prompt.submit(answer_question, [img, prompt], output)
|
| 35 |
+
output.change(process_answer, [img, output], ann, show_progress=False)
|
| 36 |
+
|
| 37 |
+
demo.queue().launch()
|