Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import torch
|
3 |
+
from PIL import Image
|
4 |
+
import requests
|
5 |
+
from transformers import AutoProcessor, AutoModelForCausalLM
|
6 |
+
|
7 |
+
# Model ve işlemciyi yükle
|
8 |
+
model = AutoModelForCausalLM.from_pretrained("microsoft/Florence-2-base", torch_dtype=torch.float16, trust_remote_code=True)
|
9 |
+
processor = AutoProcessor.from_pretrained("microsoft/Florence-2-base", trust_remote_code=True)
|
10 |
+
|
11 |
+
def process_image(image, task):
|
12 |
+
# Girdiyi hazırla
|
13 |
+
inputs = processor(text=task, images=image, return_tensors="pt")
|
14 |
+
|
15 |
+
# Çıktıyı oluştur
|
16 |
+
generated_ids = model.generate(
|
17 |
+
input_ids=inputs["input_ids"],
|
18 |
+
pixel_values=inputs["pixel_values"],
|
19 |
+
max_new_tokens=1024,
|
20 |
+
num_beams=3,
|
21 |
+
do_sample=False
|
22 |
+
)
|
23 |
+
|
24 |
+
# Sonucu işle
|
25 |
+
generated_text = processor.batch_decode(generated_ids, skip_special_tokens=False)[0]
|
26 |
+
parsed_answer = processor.post_process_generation(generated_text, task=task, image_size=(image.width, image.height))
|
27 |
+
|
28 |
+
return parsed_answer
|
29 |
+
|
30 |
+
# Gradio arayüzü
|
31 |
+
iface = gr.Interface(
|
32 |
+
fn=process_image,
|
33 |
+
inputs=[
|
34 |
+
gr.Image(type="pil"),
|
35 |
+
gr.Radio(["<IC>", "<OD>", "<VQA>", "<IS>"], label="Task")
|
36 |
+
],
|
37 |
+
outputs="text",
|
38 |
+
title="Florence-2 Image Processing",
|
39 |
+
description="Upload an image and select a task to process with Florence-2."
|
40 |
+
)
|
41 |
+
|
42 |
+
iface.launch()
|