Rachel7 commited on
Commit
06abebe
·
verified ·
1 Parent(s): b30c4ec

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -16
app.py CHANGED
@@ -9,6 +9,7 @@ import pandas as pd
9
  import numpy as np
10
  import random
11
  from diffusers import DiffusionPipeline
 
12
 
13
  #function to turn dataframe into prompt
14
  def format_prompt_from_dataframe(df):
@@ -52,8 +53,23 @@ def most_frequent_values(df):
52
 
53
  # Iterate over each column in the DataFrame
54
  for column in df.columns:
55
- # Get the most frequent value in the column
56
- most_common = df[column].mode().iloc[0]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
57
  # Add it to the dictionary
58
  most_frequent[column] = most_common
59
 
@@ -75,8 +91,7 @@ def list_to_dataframe(attribute_list):
75
 
76
  return df
77
 
78
- processor = ViltProcessor.from_pretrained("dandelin/vilt-b32-finetuned-vqa")
79
- model = ViltForQuestionAnswering.from_pretrained("dandelin/vilt-b32-finetuned-vqa")
80
  def answer_question(gallery):
81
  try:
82
  answer_list = []
@@ -90,14 +105,7 @@ def answer_question(gallery):
90
  'What font is in the image?']
91
  for i,c in gallery:
92
  for q in question_list:
93
- encoding = processor(i, q, return_tensors="pt")
94
- # forward pass
95
- with torch.no_grad():
96
- outputs = model(**encoding)
97
-
98
- logits = outputs.logits
99
- idx = logits.argmax(-1).item()
100
- predicted_answer = model.config.id2label[idx]
101
  answer_list.append(predicted_answer)
102
  df = list_to_dataframe(answer_list)
103
  frequent= most_frequent_values(df)
@@ -145,17 +153,28 @@ if torch.cuda.is_available():
145
  else:
146
  power_device = "CPU"
147
  with gr.Blocks() as demo:
 
 
 
 
 
 
 
 
 
148
 
149
  gallery = gr.Gallery(label="Gallery",type='pil')
150
  #question = gr.Textbox(label="question")
151
- result = gr.Image(label="Result", show_label=False)
152
 
153
  #name buttons to display
154
- image_button = gr.Button("show image")
155
- text_button = gr.Button("submit")
156
-
157
  answer = gr.Textbox(label='Predicted answer')
 
 
 
158
  image_button.click(fn=answer_question, inputs=[gallery], outputs=[answer])
159
  text_button.click(fn=infer,inputs =[answer],outputs=[result])
 
160
 
161
  demo.launch()
 
9
  import numpy as np
10
  import random
11
  from diffusers import DiffusionPipeline
12
+ from transformers import pipeline
13
 
14
  #function to turn dataframe into prompt
15
  def format_prompt_from_dataframe(df):
 
53
 
54
  # Iterate over each column in the DataFrame
55
  for column in df.columns:
56
+ # Create a dictionary to store the scores for each unique answer
57
+ scores = {}
58
+
59
+ # Iterate over each row in the column
60
+ for row in df[column]:
61
+ # Iterate over each entry in the list of dictionaries
62
+ for entry in row:
63
+ answer = entry['answer']
64
+ score = entry['score']
65
+ # Accumulate the score for each answer
66
+ if answer in scores:
67
+ scores[answer] += score
68
+ else:
69
+ scores[answer] = score
70
+
71
+ # Find the answer with the highest total score
72
+ most_common = max(scores, key=scores.get)
73
  # Add it to the dictionary
74
  most_frequent[column] = most_common
75
 
 
91
 
92
  return df
93
 
94
+ vqa_pipeline = pipeline("visual-question-answering",model='dandelin/vilt-b32-finetuned-vqa')
 
95
  def answer_question(gallery):
96
  try:
97
  answer_list = []
 
105
  'What font is in the image?']
106
  for i,c in gallery:
107
  for q in question_list:
108
+ predicted_answer = vqa_pipeline(i, q, top_k=3)
 
 
 
 
 
 
 
109
  answer_list.append(predicted_answer)
110
  df = list_to_dataframe(answer_list)
111
  frequent= most_frequent_values(df)
 
153
  else:
154
  power_device = "CPU"
155
  with gr.Blocks() as demo:
156
+ title = gr.Markdown("""
157
+ <div align="center">
158
+ <strong style = 'font-size: 24px;''>Finding Common Trends</strong>
159
+ </div>""")
160
+
161
+ instructions = gr.Markdown("""
162
+ <div align="center">
163
+ <strong style = 'font-size: 16px;''>Upload a Few Product Images to Find Commonalites Between Them</strong>
164
+ </div>""")
165
 
166
  gallery = gr.Gallery(label="Gallery",type='pil')
167
  #question = gr.Textbox(label="question")
168
+
169
 
170
  #name buttons to display
171
+ image_button = gr.Button("predict trends")
 
 
172
  answer = gr.Textbox(label='Predicted answer')
173
+ text_button = gr.Button("show image")
174
+
175
+
176
  image_button.click(fn=answer_question, inputs=[gallery], outputs=[answer])
177
  text_button.click(fn=infer,inputs =[answer],outputs=[result])
178
+ result = gr.Image(label="Result", show_label=False)
179
 
180
  demo.launch()