Spaces:
Running
on
Zero
Running
on
Zero
Add gradio app
Browse files- app.py +65 -0
- requirements.txt +2 -0
app.py
ADDED
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import base64
|
2 |
+
from io import BytesIO
|
3 |
+
|
4 |
+
import gradio as gr
|
5 |
+
import spaces
|
6 |
+
|
7 |
+
from llama_cpp import Llama
|
8 |
+
from llama_cpp.llama_chat_format import NanoLlavaChatHandler
|
9 |
+
|
10 |
+
chat_handler = NanoLlavaChatHandler.from_pretrained(
|
11 |
+
repo_id="abetlen/nanollava-gguf",
|
12 |
+
filename="*mmproj*",
|
13 |
+
)
|
14 |
+
llm = Llama.from_pretrained(
|
15 |
+
repo_id="abetlen/nanollava-gguf",
|
16 |
+
filename="*text-model*",
|
17 |
+
chat_handler=chat_handler,
|
18 |
+
n_ctx=2048, # n_ctx should be increased to accommodate the image embedding
|
19 |
+
n_gpu_layers=-1,
|
20 |
+
)
|
21 |
+
|
22 |
+
|
23 |
+
@spaces.GPU(duration=10)
|
24 |
+
def answer_question(img, prompt):
|
25 |
+
img_bytes = BytesIO()
|
26 |
+
img.save(img_bytes, format='JPEG')
|
27 |
+
|
28 |
+
# Encode the bytes object to a base64-encoded string
|
29 |
+
data_url = 'data:image/jpeg;base64,' + base64.b64encode(img_bytes.getvalue()).decode()
|
30 |
+
|
31 |
+
response = llm.create_chat_completion(
|
32 |
+
messages=[
|
33 |
+
{
|
34 |
+
"role": "user",
|
35 |
+
"content": [
|
36 |
+
{"type": "text", "text": prompt},
|
37 |
+
{"type": "image_url", "image_url": data_url},
|
38 |
+
],
|
39 |
+
}
|
40 |
+
],
|
41 |
+
stream=True,
|
42 |
+
)
|
43 |
+
|
44 |
+
for chunk in response:
|
45 |
+
if "content" in chunk["choices"][0]["delta"]:
|
46 |
+
yield chunk["choices"][0]["delta"]["content"]
|
47 |
+
|
48 |
+
|
49 |
+
with gr.Blocks() as demo:
|
50 |
+
gr.Markdown(
|
51 |
+
"""
|
52 |
+
# 🌔 moondream2
|
53 |
+
A tiny vision language model. [GitHub](https://github.com/vikhyat/moondream)
|
54 |
+
"""
|
55 |
+
)
|
56 |
+
with gr.Row():
|
57 |
+
prompt = gr.Textbox(label="Input", value="Describe this image.", scale=4)
|
58 |
+
submit = gr.Button("Submit")
|
59 |
+
with gr.Row():
|
60 |
+
img = gr.Image(type="pil", label="Upload an Image")
|
61 |
+
output = gr.TextArea(label="Response")
|
62 |
+
submit.click(answer_question, [img, prompt], output)
|
63 |
+
prompt.submit(answer_question, [img, prompt], output)
|
64 |
+
|
65 |
+
demo.queue().launch()
|
requirements.txt
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
huggingface-hub
|
2 |
+
llama-cpp-python --extra-index-url https://abetlen.github.io/llama-cpp-python/whl/cu121
|