File size: 829 Bytes
5781b89
0529d67
 
 
5781b89
0529d67
 
 
 
 
5781b89
0529d67
 
 
 
 
 
 
07d11bb
0529d67
 
 
 
 
 
5781b89
0529d67
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import gradio as gr
import torch
from PIL import Image
from transformers import AutoModelForCausalLM, AutoTokenizer

# Load model versi open-source (Gratis selamanya)
model_id = "vikhyatk/moondream2"
revision = "2024-08-26"
model = AutoModelForCausalLM.from_pretrained(model_id, trust_remote_code=True, revision=revision)
tokenizer = AutoTokenizer.from_pretrained(model_id, revision=revision)

def predict(image, prompt):
    if image is None:
        return "No image uploaded"
    # Proses gambar ke model
    enc_image = model.encode_image(image)
    answer = model.answer_question(enc_image, prompt, tokenizer)
    return answer

# Interface Gradio sederhana buat bot PHP lo
interface = gr.Interface(
    fn=predict,
    inputs=[gr.Image(type="pil"), gr.Textbox(label="Prompt")],
    outputs=gr.Text(),
)

interface.launch()