Mayada commited on
Commit
5b1faf1
1 Parent(s): 5f85a7e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -20
app.py CHANGED
@@ -66,7 +66,7 @@ def generate_questions(context, answer):
66
  'question: ', ' ') for g in generated_ids]
67
  return questions
68
 
69
- # Define the Gradio interface with Seafoam theme
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
- # Correct captions using the dictionary
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
- answer = words[0]
89
- question = generate_questions(caption, answer)
90
- questions_with_answers.extend([(q, answer) for q in question])
91
  # Generate questions for the second word
92
  if len(words) > 1:
93
- answer = words[1]
94
- question = generate_questions(caption, answer)
95
- questions_with_answers.extend([(q, answer) for q in question])
96
  # Generate questions for the second word + first word
97
  if len(words) > 1:
98
- answer = " ".join(words[:2])
99
- question = generate_questions(caption, answer)
100
- questions_with_answers.extend([(q, answer) for q in question])
101
  # Generate questions for the third word
102
  if len(words) > 2:
103
- answer = words[2]
104
- question = generate_questions(caption, answer)
105
- questions_with_answers.extend([(q, answer) for q in question])
106
  # Generate questions for the fourth word
107
  if len(words) > 3:
108
- answer = words[3]
109
- question = generate_questions(caption, answer)
110
- questions_with_answers.extend([(q, answer) for q in question])
111
 
112
  # Format questions with answers
113
- formatted_questions = [f"Question: {q}\nAnswer: {a}" for q, a in questions_with_answers]
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 and Answers")
125
  ],
126
- title="Image Captioning and Question Generation",
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
  )