Update app.py
Browse files
app.py
CHANGED
@@ -1,33 +1,30 @@
|
|
|
|
1 |
import gradio as gr
|
2 |
-
from
|
3 |
-
from PIL import Image, ImageOps
|
4 |
|
5 |
-
#
|
6 |
-
|
7 |
-
model = VisionEncoderDecoderModel.from_pretrained("microsoft/trocr-base-stage1")
|
8 |
|
9 |
-
#
|
10 |
-
|
11 |
-
|
12 |
-
if invert_image:
|
13 |
-
image = ImageOps.mirror(image)
|
14 |
-
|
15 |
-
# 画像をモデルに入力し、文字起こしを実行
|
16 |
-
pixel_values = processor(images=image, return_tensors="pt").pixel_values
|
17 |
-
generated_ids = model.generate(pixel_values)
|
18 |
-
generated_text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
|
19 |
-
return generated_text
|
20 |
|
21 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
interface = gr.Interface(
|
23 |
-
fn=
|
24 |
inputs=[
|
25 |
-
gr.Image(type="
|
26 |
-
gr.
|
27 |
],
|
28 |
-
outputs="
|
29 |
-
live=False # リアルタイム処理を無効化
|
30 |
)
|
31 |
|
32 |
-
#
|
33 |
interface.launch()
|
|
|
1 |
+
import torch
|
2 |
import gradio as gr
|
3 |
+
from promptcap import PromptCap
|
|
|
4 |
|
5 |
+
# モデルの読み込み
|
6 |
+
model = PromptCap("tifa-benchmark/promptcap-coco-vqa")
|
|
|
7 |
|
8 |
+
# CUDAが使用可能ならGPUを使用
|
9 |
+
if torch.cuda.is_available():
|
10 |
+
model.cuda()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
|
12 |
+
# 画像と質問を入力として、キャプションを生成する関数を定義
|
13 |
+
def generate_caption(image, question):
|
14 |
+
prompt = f"please describe this image according to the given question: {question}"
|
15 |
+
# PromptCapモデルでキャプションを生成
|
16 |
+
caption = model.caption(prompt, image)
|
17 |
+
return caption
|
18 |
+
|
19 |
+
# Gradioインターフェースの定義
|
20 |
interface = gr.Interface(
|
21 |
+
fn=generate_caption, # キャプション生成関数
|
22 |
inputs=[
|
23 |
+
gr.inputs.Image(type="filepath", label="Input Image"), # 画像入力
|
24 |
+
gr.inputs.Textbox(label="Question") # 質問入力
|
25 |
],
|
26 |
+
outputs=gr.outputs.Textbox(label="Generated Caption") # キャプション出力
|
|
|
27 |
)
|
28 |
|
29 |
+
# インターフェースを起動
|
30 |
interface.launch()
|