File size: 1,402 Bytes
b149f48
 
 
 
 
 
 
c8f83ff
b149f48
 
 
 
c8f83ff
 
 
 
b149f48
 
 
 
 
 
 
 
c8f83ff
b149f48
 
 
c8f83ff
b149f48
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import gradio as gr
import torch
from PIL import Image
import requests
from transformers import AutoProcessor, AutoModelForCausalLM

# Model ve işlemciyi yükle
model = AutoModelForCausalLM.from_pretrained("microsoft/Florence-2-base-mini", torch_dtype=torch.float32, trust_remote_code=True)

def process_image(image, task):
    # Girdiyi hazırla
    inputs = processor(text=task, images=image, return_tensors="pt")
    
    # Giriş verilerini half precision'a dönüştür
    inputs = {k: v.half() if isinstance(v, torch.Tensor) else v for k, v in inputs.items()}
    
    # Çıktıyı oluştur
    generated_ids = model.generate(
        input_ids=inputs["input_ids"],
        pixel_values=inputs["pixel_values"],
        max_new_tokens=1024,
        num_beams=3,
        do_sample=False
    )
    
    # Sonucu işle
    generated_text = processor.batch_decode(generated_ids, skip_special_tokens=False)[0]
    parsed_answer = processor.post_process_generation(generated_text, task=task, image_size=(image.width, image.height))
    
    return parsed_answer

# Gradio arayüzü
iface = gr.Interface(
    fn=process_image,
    inputs=[
        gr.Image(type="pil"),
        gr.Radio(["<IC>", "<OD>", "<VQA>", "<IS>"], label="Task")
    ],
    outputs="text",
    title="Florence-2 Image Processing",
    description="Upload an image and select a task to process with Florence-2."
)

iface.launch()