DemahAlmutairi commited on
Commit
a294799
·
verified ·
1 Parent(s): 5e5717f

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +69 -0
app.py ADDED
@@ -0,0 +1,69 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import pipeline
3
+ import torch
4
+ from PIL import Image
5
+
6
+ # Load models
7
+ pipe = pipeline("image-to-text", model="Salesforce/blip-image-captioning-base")
8
+ translator = pipeline(task="translation", model="facebook/nllb-200-distilled-600M", torch_dtype=torch.bfloat16)
9
+
10
+ def process_image(image, shouldConvert=False):
11
+ if shouldConvert:
12
+ new_img = Image.new('RGB', size=(image.width, image.height), color=(255, 255, 255))
13
+ new_img.paste(image, (0, 0), mask=image)
14
+ image = new_img
15
+ return image
16
+
17
+ def parse_input(image, sketchpad, state):
18
+ current_tab_index = state["tab_index"]
19
+ new_image = None
20
+
21
+ # Upload
22
+ if current_tab_index == 0:
23
+ if image is not None:
24
+ new_image = process_image(image)
25
+
26
+ # Sketch
27
+ elif current_tab_index == 1:
28
+ #print(sketchpad)
29
+ if sketchpad and sketchpad["composite"]:
30
+ new_image = process_image(sketchpad["composite"], True)
31
+
32
+ Eng_txt = pipe(new_image)
33
+ to_Ar_txt = str(Eng_txt[0]['generated_text'])
34
+ text_translated = translator(to_Ar_txt, src_lang="eng_Latn", tgt_lang="arz_Arab")
35
+ return text_translated[0]['translation_text']
36
+
37
+
38
+ def tabs_select(e: gr.SelectData, _state):
39
+ _state["tab_index"] = e.index
40
+
41
+ with gr.Blocks() as iface:
42
+ gr.HTML("""<p align="center"><img src="https://cdn-icons-png.flaticon.com/512/5853/5853758.png" style="height: 60px"/><p>""")
43
+ gr.HTML("""<center><font size=8>Image Captioning Demo</center>""")
44
+ gr.HTML("""<center><font size=3>In this space you can input either an image or draw a sketch of objects to recieve an Arabic caption.</center>""")
45
+
46
+ state = gr.State({"tab_index": 0})
47
+
48
+ with gr.Row():
49
+ with gr.Column():
50
+ with gr.Tabs() as input_tabs:
51
+ with gr.Tab("Upload"):
52
+ input_image = gr.Image(type="pil", label="Upload")
53
+ with gr.Tab("Sketch"):
54
+ input_sketchpad = gr.Sketchpad(type="pil", label="Sketch", layers=False)
55
+ input_tabs.select(fn=tabs_select, inputs=[state])
56
+ with gr.Row():
57
+ with gr.Column():
58
+ clear_btn = gr.ClearButton(
59
+ [input_image, input_sketchpad])
60
+ with gr.Column():
61
+ submit_btn = gr.Button("Submit", variant="primary")
62
+ submit_btn.click(
63
+ fn=parse_input,
64
+ inputs=[input_image, input_sketchpad, state],
65
+ outputs= gr.Textbox(label = "Result"))
66
+
67
+ # Launch the interface
68
+ if __name__ == "__main__":
69
+ iface.launch()