soiz commited on
Commit
8d94907
·
verified ·
1 Parent(s): 7bc35e5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -23
app.py CHANGED
@@ -1,33 +1,30 @@
 
1
  import gradio as gr
2
- from transformers import TrOCRProcessor, VisionEncoderDecoderModel
3
- from PIL import Image, ImageOps
4
 
5
- # OCRモデルとプロセッサの準備
6
- processor = TrOCRProcessor.from_pretrained("microsoft/trocr-base-stage1")
7
- model = VisionEncoderDecoderModel.from_pretrained("microsoft/trocr-base-stage1")
8
 
9
- # OCR実行関数
10
- def ocr_image(image, invert_image):
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
- # Gradioインターフェースの構築
 
 
 
 
 
 
 
22
  interface = gr.Interface(
23
- fn=ocr_image, # 実行関数
24
  inputs=[
25
- gr.Image(type="pil"), # カメラからの画像入力
26
- gr.Checkbox(label="画像を反転") # 画像反転のチェックボックス
27
  ],
28
- outputs="text", # 出力形式(テキスト)
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()