DemahAlmutairi commited on
Commit
7bea74a
·
verified ·
1 Parent(s): d453b77

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -52
app.py CHANGED
@@ -7,6 +7,7 @@ from PIL import Image
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))
@@ -22,73 +23,31 @@ def parse_input(image, sketchpad, state):
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 Eng_txt[0]['generated_text'], text_translated[0]['translation_text']
36
 
37
-
38
  def tabs_select(e: gr.SelectData, _state):
39
  _state["tab_index"] = e.index
40
 
41
  example_img_paths = [["https://4.img-dpreview.com/files/p/E~TS590x0~articles/3925134721/0266554465.jpeg"],
42
  ["https://images4.alphacoders.com/688/688832.jpg"]]
43
 
44
-
45
- css_code = """
46
- <style>
47
- body {
48
- font-family: Arial, sans-serif;
49
- background-color: #f4f4f4;
50
- margin: 0;
51
- padding: 20px;
52
- }
53
- h1 {
54
- font-size: 2.5em;
55
- color: #333;
56
- text-align: center;
57
- }
58
- p {
59
- color: #666;
60
- text-align: center;
61
- margin: 10px 0;
62
- }
63
- #examples_row {
64
- margin-top: 20px;
65
- justify-content: center;
66
- }
67
- .gr-button {
68
- background-color: #007bff;
69
- color: white;
70
- border-radius: 5px;
71
- border: none;
72
- padding: 10px 20px;
73
- margin: 5px;
74
- cursor: pointer;
75
- }
76
- .gr-button:hover {
77
- background-color: #0056b3;
78
- }
79
- .gr-button:focus {
80
- outline: none;
81
- }
82
- </style>
83
- """
84
-
85
- with gr.Blocks(css=css_code) as iface:
86
  gr.HTML("""<p align="center"><img src="https://cdn-icons-png.flaticon.com/512/5853/5853758.png" style="height: 60px"/><p>""")
87
  gr.HTML("""<center><font size=8>Image Captioning Demo</center>""")
88
  gr.HTML("""<center><font size=3>In this space you can input either an image or draw a sketch of object to recieve an Arabic caption.</center>""")
89
-
90
  state = gr.State({"tab_index": 0})
91
-
92
  with gr.Row():
93
  with gr.Column():
94
  with gr.Tabs() as input_tabs:
@@ -98,16 +57,13 @@ with gr.Blocks(css=css_code) as iface:
98
  input_sketchpad = gr.Sketchpad(type="pil", label="Sketch", layers=False)
99
  input_tabs.select(fn=tabs_select, inputs=[state])
100
 
101
-
102
-
103
- with gr.Row(elem_id="examples_row"):
104
- with gr.Column(elem_id="examples_container"):
105
  gr.Markdown("### Example Prompts")
106
  gr.Examples(
107
  example_img_paths,
108
  inputs=[input_image],
109
- cache_examples=False
110
- )
111
 
112
  with gr.Row():
113
  with gr.Column():
 
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
+ #process sketch input
11
  def process_image(image, shouldConvert=False):
12
  if shouldConvert:
13
  new_img = Image.new('RGB', size=(image.width, image.height), color=(255, 255, 255))
 
23
  if current_tab_index == 0:
24
  if image is not None:
25
  new_image = process_image(image)
 
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
+ # send to pipeline
33
  Eng_txt = pipe(new_image)
34
  to_Ar_txt = str(Eng_txt[0]['generated_text'])
35
  text_translated = translator(to_Ar_txt, src_lang="eng_Latn", tgt_lang="arz_Arab")
36
  return Eng_txt[0]['generated_text'], text_translated[0]['translation_text']
37
 
38
+ #select tab
39
  def tabs_select(e: gr.SelectData, _state):
40
  _state["tab_index"] = e.index
41
 
42
  example_img_paths = [["https://4.img-dpreview.com/files/p/E~TS590x0~articles/3925134721/0266554465.jpeg"],
43
  ["https://images4.alphacoders.com/688/688832.jpg"]]
44
 
45
+ with gr.Blocks() as iface:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
46
  gr.HTML("""<p align="center"><img src="https://cdn-icons-png.flaticon.com/512/5853/5853758.png" style="height: 60px"/><p>""")
47
  gr.HTML("""<center><font size=8>Image Captioning Demo</center>""")
48
  gr.HTML("""<center><font size=3>In this space you can input either an image or draw a sketch of object to recieve an Arabic caption.</center>""")
 
49
  state = gr.State({"tab_index": 0})
50
+
51
  with gr.Row():
52
  with gr.Column():
53
  with gr.Tabs() as input_tabs:
 
57
  input_sketchpad = gr.Sketchpad(type="pil", label="Sketch", layers=False)
58
  input_tabs.select(fn=tabs_select, inputs=[state])
59
 
60
+ with gr.Row():
61
+ with gr.Column():
 
 
62
  gr.Markdown("### Example Prompts")
63
  gr.Examples(
64
  example_img_paths,
65
  inputs=[input_image],
66
+ cache_examples=False)
 
67
 
68
  with gr.Row():
69
  with gr.Column():