Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,37 +1,27 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import
|
| 3 |
-
import
|
|
|
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
-
def
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
|
|
|
|
|
|
| 13 |
|
| 14 |
-
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
-
|
| 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()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
from PIL import Image
|
| 4 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 5 |
|
| 6 |
+
# Load model versi open-source (Gratis selamanya)
|
| 7 |
+
model_id = "vikhyatk/moondream2"
|
| 8 |
+
revision = "2024-08-26"
|
| 9 |
+
model = AutoModelForCausalLM.from_pretrained(model_id, trust_remote_code=True, revision=revision)
|
| 10 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id, revision=revision)
|
| 11 |
|
| 12 |
+
def predict(image, prompt):
|
| 13 |
+
if image is None:
|
| 14 |
+
return "No image uploaded"
|
| 15 |
+
# Proses gambar ke model
|
| 16 |
+
enc_image = model.encode_image(image)
|
| 17 |
+
answer = model.answer_question(enc_image, prompt, tokenizer)
|
| 18 |
+
return answer
|
| 19 |
|
| 20 |
+
# Interface Gradio sederhana buat bot PHP lo
|
| 21 |
+
interface = gr.Interface(
|
| 22 |
+
fn=predict,
|
| 23 |
+
inputs=[gr.Image(type="pil"), gr.Textbox(label="Prompt")],
|
| 24 |
+
outputs=gr.Text(),
|
| 25 |
+
)
|
| 26 |
|
| 27 |
+
interface.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|