Update app.py
Browse files
app.py
CHANGED
|
@@ -66,7 +66,7 @@ def generate_questions(context, answer):
|
|
| 66 |
'question: ', ' ') for g in generated_ids]
|
| 67 |
return questions
|
| 68 |
|
| 69 |
-
#
|
| 70 |
class Seafoam(Base):
|
| 71 |
pass
|
| 72 |
|
|
@@ -76,7 +76,7 @@ def caption_question_interface(image):
|
|
| 76 |
# Generate captions
|
| 77 |
captions = generate_captions(image)
|
| 78 |
|
| 79 |
-
#
|
| 80 |
corrected_captions = [correct_caption(caption) for caption in captions]
|
| 81 |
|
| 82 |
# Generate questions for each caption
|
|
@@ -85,32 +85,32 @@ def caption_question_interface(image):
|
|
| 85 |
words = caption.split()
|
| 86 |
# Generate questions for the first word
|
| 87 |
if len(words) > 0:
|
| 88 |
-
|
| 89 |
-
question = generate_questions(caption,
|
| 90 |
-
|
| 91 |
# Generate questions for the second word
|
| 92 |
if len(words) > 1:
|
| 93 |
-
|
| 94 |
-
question = generate_questions(caption,
|
| 95 |
-
|
| 96 |
# Generate questions for the second word + first word
|
| 97 |
if len(words) > 1:
|
| 98 |
-
|
| 99 |
-
question = generate_questions(caption,
|
| 100 |
-
|
| 101 |
# Generate questions for the third word
|
| 102 |
if len(words) > 2:
|
| 103 |
-
|
| 104 |
-
question = generate_questions(caption,
|
| 105 |
-
|
| 106 |
# Generate questions for the fourth word
|
| 107 |
if len(words) > 3:
|
| 108 |
-
|
| 109 |
-
question = generate_questions(caption,
|
| 110 |
-
questions_with_answers.extend([(q,
|
| 111 |
|
| 112 |
# Format questions with answers
|
| 113 |
-
formatted_questions = [f"Question: {q}\
|
| 114 |
formatted_questions = "\n".join(formatted_questions)
|
| 115 |
|
| 116 |
# Return the generated captions and formatted questions with answers
|
|
@@ -121,9 +121,9 @@ gr_interface = gr.Interface(
|
|
| 121 |
inputs=gr.Image(type="pil", label="Input Image"),
|
| 122 |
outputs=[
|
| 123 |
gr.Textbox(label="Generated Captions"),
|
| 124 |
-
gr.Textbox(label="Generated Questions
|
| 125 |
],
|
| 126 |
-
title="
|
| 127 |
description="Generate captions and questions for images using pre-trained models.",
|
| 128 |
theme=seafoam,
|
| 129 |
)
|
|
|
|
| 66 |
'question: ', ' ') for g in generated_ids]
|
| 67 |
return questions
|
| 68 |
|
| 69 |
+
# Interface
|
| 70 |
class Seafoam(Base):
|
| 71 |
pass
|
| 72 |
|
|
|
|
| 76 |
# Generate captions
|
| 77 |
captions = generate_captions(image)
|
| 78 |
|
| 79 |
+
# Proofread captions using the dictionary
|
| 80 |
corrected_captions = [correct_caption(caption) for caption in captions]
|
| 81 |
|
| 82 |
# Generate questions for each caption
|
|
|
|
| 85 |
words = caption.split()
|
| 86 |
# Generate questions for the first word
|
| 87 |
if len(words) > 0:
|
| 88 |
+
keyword = words[0]
|
| 89 |
+
question = generate_questions(caption, keyword)
|
| 90 |
+
questions_with_keywords.extend([(q, keyword) for q in question])
|
| 91 |
# Generate questions for the second word
|
| 92 |
if len(words) > 1:
|
| 93 |
+
keyword = words[1]
|
| 94 |
+
question = generate_questions(caption, keyword)
|
| 95 |
+
questions_with_keywords.extend([(q, keyword) for q in question])
|
| 96 |
# Generate questions for the second word + first word
|
| 97 |
if len(words) > 1:
|
| 98 |
+
keyword = " ".join(words[:2])
|
| 99 |
+
question = generate_questions(caption, keyword)
|
| 100 |
+
questions_with_keywords.extend([(q, keyword) for q in question])
|
| 101 |
# Generate questions for the third word
|
| 102 |
if len(words) > 2:
|
| 103 |
+
keyword = words[2]
|
| 104 |
+
question = generate_questions(caption, keyword)
|
| 105 |
+
questions_with_keywords.extend([(q, keyword) for q in question])
|
| 106 |
# Generate questions for the fourth word
|
| 107 |
if len(words) > 3:
|
| 108 |
+
keyword = words[3]
|
| 109 |
+
question = generate_questions(caption, keyword)
|
| 110 |
+
questions_with_answers.extend([(q, keyword) for q in question])
|
| 111 |
|
| 112 |
# Format questions with answers
|
| 113 |
+
formatted_questions = [f"Question: {q}\nKeyword: {a}" for q, a in questions_with_keywords]
|
| 114 |
formatted_questions = "\n".join(formatted_questions)
|
| 115 |
|
| 116 |
# Return the generated captions and formatted questions with answers
|
|
|
|
| 121 |
inputs=gr.Image(type="pil", label="Input Image"),
|
| 122 |
outputs=[
|
| 123 |
gr.Textbox(label="Generated Captions"),
|
| 124 |
+
gr.Textbox(label="Generated Questions")
|
| 125 |
],
|
| 126 |
+
title="Visual Question Generator",
|
| 127 |
description="Generate captions and questions for images using pre-trained models.",
|
| 128 |
theme=seafoam,
|
| 129 |
)
|