Update app.py
Browse files
app.py
CHANGED
|
@@ -4,23 +4,28 @@ import re
|
|
| 4 |
import gradio as gr
|
| 5 |
from threading import Thread
|
| 6 |
from transformers import TextIteratorStreamer, AutoTokenizer, AutoModelForCausalLM
|
| 7 |
-
|
| 8 |
import subprocess
|
|
|
|
| 9 |
subprocess.run('pip install flash-attn --no-build-isolation', env={'FLASH_ATTENTION_SKIP_CUDA_BUILD': "TRUE"}, shell=True)
|
| 10 |
|
| 11 |
model_id = "vikhyatk/moondream2"
|
| 12 |
revision = "2024-05-20"
|
| 13 |
tokenizer = AutoTokenizer.from_pretrained(model_id, revision=revision)
|
| 14 |
moondream = AutoModelForCausalLM.from_pretrained(
|
| 15 |
-
model_id,
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
| 17 |
attn_implementation="flash_attention_2"
|
| 18 |
)
|
| 19 |
moondream.eval()
|
| 20 |
|
| 21 |
-
|
| 22 |
@spaces.GPU(duration=10)
|
| 23 |
def answer_question(img, prompt):
|
|
|
|
|
|
|
|
|
|
| 24 |
image_embeds = moondream.encode_image(img)
|
| 25 |
streamer = TextIteratorStreamer(tokenizer, skip_special_tokens=True)
|
| 26 |
thread = Thread(
|
|
@@ -33,28 +38,26 @@ def answer_question(img, prompt):
|
|
| 33 |
},
|
| 34 |
)
|
| 35 |
thread.start()
|
| 36 |
-
|
| 37 |
buffer = ""
|
| 38 |
for new_text in streamer:
|
| 39 |
buffer += new_text
|
| 40 |
yield buffer.strip()
|
| 41 |
|
| 42 |
-
|
| 43 |
with gr.Blocks() as demo:
|
| 44 |
gr.Markdown(
|
| 45 |
"""
|
| 46 |
# myAI - AMI Vision Module
|
| 47 |
-
A lightweight Computer Vision model by @vikhyat
|
| 48 |
-
- 🌔 [moondream2](https://github.com/vikhyat/moondream)
|
| 49 |
"""
|
| 50 |
)
|
| 51 |
with gr.Row():
|
| 52 |
-
prompt = gr.Textbox(label="Input", value="Identify people in this
|
| 53 |
submit = gr.Button("Submit")
|
| 54 |
with gr.Row():
|
| 55 |
img = gr.Image(type="pil", label="Upload an Image")
|
| 56 |
output = gr.TextArea(label="Response")
|
|
|
|
| 57 |
submit.click(answer_question, [img, prompt], output)
|
| 58 |
prompt.submit(answer_question, [img, prompt], output)
|
| 59 |
|
| 60 |
-
demo.queue().launch()
|
|
|
|
| 4 |
import gradio as gr
|
| 5 |
from threading import Thread
|
| 6 |
from transformers import TextIteratorStreamer, AutoTokenizer, AutoModelForCausalLM
|
|
|
|
| 7 |
import subprocess
|
| 8 |
+
|
| 9 |
subprocess.run('pip install flash-attn --no-build-isolation', env={'FLASH_ATTENTION_SKIP_CUDA_BUILD': "TRUE"}, shell=True)
|
| 10 |
|
| 11 |
model_id = "vikhyatk/moondream2"
|
| 12 |
revision = "2024-05-20"
|
| 13 |
tokenizer = AutoTokenizer.from_pretrained(model_id, revision=revision)
|
| 14 |
moondream = AutoModelForCausalLM.from_pretrained(
|
| 15 |
+
model_id,
|
| 16 |
+
trust_remote_code=True,
|
| 17 |
+
revision=revision,
|
| 18 |
+
torch_dtype=torch.bfloat16,
|
| 19 |
+
device_map={"": "cuda"},
|
| 20 |
attn_implementation="flash_attention_2"
|
| 21 |
)
|
| 22 |
moondream.eval()
|
| 23 |
|
|
|
|
| 24 |
@spaces.GPU(duration=10)
|
| 25 |
def answer_question(img, prompt):
|
| 26 |
+
if img is None:
|
| 27 |
+
raise gr.Error("Please upload an image.")
|
| 28 |
+
|
| 29 |
image_embeds = moondream.encode_image(img)
|
| 30 |
streamer = TextIteratorStreamer(tokenizer, skip_special_tokens=True)
|
| 31 |
thread = Thread(
|
|
|
|
| 38 |
},
|
| 39 |
)
|
| 40 |
thread.start()
|
|
|
|
| 41 |
buffer = ""
|
| 42 |
for new_text in streamer:
|
| 43 |
buffer += new_text
|
| 44 |
yield buffer.strip()
|
| 45 |
|
|
|
|
| 46 |
with gr.Blocks() as demo:
|
| 47 |
gr.Markdown(
|
| 48 |
"""
|
| 49 |
# myAI - AMI Vision Module
|
| 50 |
+
A lightweight Computer Vision model by @vikhyat - 🌔 [moondream2](https://github.com/vikhyat/moondream)
|
|
|
|
| 51 |
"""
|
| 52 |
)
|
| 53 |
with gr.Row():
|
| 54 |
+
prompt = gr.Textbox(label="Input", value="Identify people in this image", scale=4)
|
| 55 |
submit = gr.Button("Submit")
|
| 56 |
with gr.Row():
|
| 57 |
img = gr.Image(type="pil", label="Upload an Image")
|
| 58 |
output = gr.TextArea(label="Response")
|
| 59 |
+
|
| 60 |
submit.click(answer_question, [img, prompt], output)
|
| 61 |
prompt.submit(answer_question, [img, prompt], output)
|
| 62 |
|
| 63 |
+
demo.queue().launch()
|