Spaces:
Sleeping
Sleeping
Juho Inkinen
commited on
Allow selecting language of outputs
Browse files
app.py
CHANGED
|
@@ -75,11 +75,11 @@ def get_subjects(caption, project_id):
|
|
| 75 |
|
| 76 |
def process_image(image, project_id):
|
| 77 |
prompt = (
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
'Vastaa vain lopullisella alt-tekstillä, älä lisää "tässä on alt-teksti", selityksiä tai väliotsikoita. '
|
| 84 |
)
|
| 85 |
caption = get_caption(image, prompt)
|
|
@@ -101,14 +101,23 @@ with gr.Blocks(title="VLM Caption & Annif Demo") as demo:
|
|
| 101 |
with gr.Column():
|
| 102 |
gr.Markdown("### Input")
|
| 103 |
image_input = gr.Image(
|
| 104 |
-
type="pil",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
)
|
| 106 |
project_dropdown = gr.Dropdown(
|
| 107 |
-
choices=[
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
|
|
|
| 112 |
label="Annif Project",
|
| 113 |
info="Select the vocabulary from where subject suggestions are drawn",
|
| 114 |
)
|
|
@@ -119,13 +128,45 @@ with gr.Blocks(title="VLM Caption & Annif Demo") as demo:
|
|
| 119 |
caption_output = gr.Textbox(label="Caption", lines=10, interactive=False)
|
| 120 |
subjects_output = gr.Label(label="Subject Suggestions", show_heading=False)
|
| 121 |
|
| 122 |
-
|
| 123 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 124 |
return caption, subjects
|
| 125 |
|
| 126 |
submit_btn.click(
|
| 127 |
run_app,
|
| 128 |
-
inputs=[image_input, project_dropdown],
|
| 129 |
outputs=[caption_output, subjects_output],
|
| 130 |
)
|
| 131 |
clear_btn.click(lambda: ("", {}), outputs=[caption_output, subjects_output])
|
|
@@ -135,4 +176,4 @@ with gr.Blocks(title="VLM Caption & Annif Demo") as demo:
|
|
| 135 |
|
| 136 |
image_input.upload(update_submit_btn, inputs=image_input, outputs=submit_btn)
|
| 137 |
|
| 138 |
-
demo.launch()
|
|
|
|
| 75 |
|
| 76 |
def process_image(image, project_id):
|
| 77 |
prompt = (
|
| 78 |
+
"Luo vaihtoehtoinen tekstikuvaus, joka on tarkoitettu henkilöille, jotka eivät näe kuvaa. "
|
| 79 |
+
"Kuvaile kuvan todellista sisältöä, älä tulkitse mitään. "
|
| 80 |
+
"Aloita yleisellä kuvauksella ja siirry sitten yksityiskohtiin. "
|
| 81 |
+
"Kuvaile yksityiskohtia ainakin viiden lauseen verran. "
|
| 82 |
+
"Jos kuvassa näkyy tekstiä, kerro mitä siinä lukee ja jos teksti ei ole suomea, käännä se myös suomeksi. "
|
| 83 |
'Vastaa vain lopullisella alt-tekstillä, älä lisää "tässä on alt-teksti", selityksiä tai väliotsikoita. '
|
| 84 |
)
|
| 85 |
caption = get_caption(image, prompt)
|
|
|
|
| 101 |
with gr.Column():
|
| 102 |
gr.Markdown("### Input")
|
| 103 |
image_input = gr.Image(
|
| 104 |
+
type="pil",
|
| 105 |
+
label="Image Input (upload or take a photo)",
|
| 106 |
+
mirror_webcam=False,
|
| 107 |
+
)
|
| 108 |
+
language_dropdown = gr.Dropdown(
|
| 109 |
+
choices=[("English", "en"), ("Finnish", "fi"), ("Swedish", "sv")],
|
| 110 |
+
value="fi",
|
| 111 |
+
label="Output Language",
|
| 112 |
+
info="Select the output language for caption and subject suggestions",
|
| 113 |
)
|
| 114 |
project_dropdown = gr.Dropdown(
|
| 115 |
+
choices=[
|
| 116 |
+
("YSO - General Finnish Ontology", "yso"),
|
| 117 |
+
("YKL - Finnish Public Library Classification ", "ykl"),
|
| 118 |
+
("KAUNO - Ontology for Fiction", "kauno"),
|
| 119 |
+
],
|
| 120 |
+
value="yso",
|
| 121 |
label="Annif Project",
|
| 122 |
info="Select the vocabulary from where subject suggestions are drawn",
|
| 123 |
)
|
|
|
|
| 128 |
caption_output = gr.Textbox(label="Caption", lines=10, interactive=False)
|
| 129 |
subjects_output = gr.Label(label="Subject Suggestions", show_heading=False)
|
| 130 |
|
| 131 |
+
# Translated prompts for VLM
|
| 132 |
+
VLM_PROMPTS = {
|
| 133 |
+
"fi": (
|
| 134 |
+
"Luo vaihtoehtoinen tekstikuvaus, joka on tarkoitettu henkilöille, jotka eivät näe kuvaa. "
|
| 135 |
+
"Kuvaile kuvan todellista sisältöä, älä tulkitse mitään. "
|
| 136 |
+
"Aloita yleisellä kuvauksella ja siirry sitten yksityiskohtiin. "
|
| 137 |
+
"Kuvaile yksityiskohtia ainakin viiden lauseen verran. "
|
| 138 |
+
"Jos kuvassa näkyy tekstiä, kerro mitä siinä lukee ja jos teksti ei ole suomea, käännä se myös suomeksi. "
|
| 139 |
+
'Vastaa vain lopullisella alt-tekstillä, älä lisää "tässä on alt-teksti", selityksiä tai väliotsikoita. '
|
| 140 |
+
),
|
| 141 |
+
"en": (
|
| 142 |
+
"Create an alternative text description for people who cannot see the image. "
|
| 143 |
+
"Describe the actual content of the image, do not interpret anything. "
|
| 144 |
+
"Start with a general description and then move to details. "
|
| 145 |
+
"Describe details in at least five sentences. "
|
| 146 |
+
"If there is text in the image, state what it says and translate it into English if it is not in English. "
|
| 147 |
+
"Respond only with the final alt text, do not add explanations or headings."
|
| 148 |
+
),
|
| 149 |
+
"sv": (
|
| 150 |
+
"Skapa en alternativ textbeskrivning för personer som inte kan se bilden. "
|
| 151 |
+
"Beskriv bildens faktiska innehåll, tolka ingenting. "
|
| 152 |
+
"Börja med en allmän beskrivning och gå sedan vidare till detaljer. "
|
| 153 |
+
"Beskriv detaljerna med minst fem meningar. "
|
| 154 |
+
"Om det finns text i bilden, ange vad det står och översätt det till svenska om det inte är på svenska. "
|
| 155 |
+
"Svara endast med den slutliga alt-texten, lägg inte till förklaringar eller rubriker."
|
| 156 |
+
),
|
| 157 |
+
}
|
| 158 |
+
|
| 159 |
+
def run_app(image, language, project):
|
| 160 |
+
prompt = VLM_PROMPTS.get(language, VLM_PROMPTS["fi"])
|
| 161 |
+
# Compose Annif project identifier
|
| 162 |
+
project_id = f"{project}-{language}"
|
| 163 |
+
caption = get_caption(image, prompt)
|
| 164 |
+
subjects = get_subjects(caption, project_id)
|
| 165 |
return caption, subjects
|
| 166 |
|
| 167 |
submit_btn.click(
|
| 168 |
run_app,
|
| 169 |
+
inputs=[image_input, language_dropdown, project_dropdown],
|
| 170 |
outputs=[caption_output, subjects_output],
|
| 171 |
)
|
| 172 |
clear_btn.click(lambda: ("", {}), outputs=[caption_output, subjects_output])
|
|
|
|
| 176 |
|
| 177 |
image_input.upload(update_submit_btn, inputs=image_input, outputs=submit_btn)
|
| 178 |
|
| 179 |
+
demo.launch()
|