alexander-lazarin commited on
Commit
b11f8f0
·
1 Parent(s): 1b0da9b
Files changed (1) hide show
  1. app.py +40 -14
app.py CHANGED
@@ -63,12 +63,13 @@ def get_additional_images(id_product_money: str, marketplace: str) -> List[str]:
63
  with db_conn.connect() as connection:
64
  result = connection.execute(query, {"id_product_money": id_product_money}).first()
65
  if result and result.more_images:
 
66
  paths = json.loads(result.more_images)
67
  return [f"https://a.lmcdn.ru/product{path}" for path in paths]
68
 
69
  elif marketplace == 'wildberries':
70
  query = text("""
71
- select features->'images' as more_images
72
  from public.wb_chrc
73
  where id_product_money = :id_product_money
74
  limit 1
@@ -76,7 +77,17 @@ def get_additional_images(id_product_money: str, marketplace: str) -> List[str]:
76
  with db_conn.connect() as connection:
77
  result = connection.execute(query, {"id_product_money": id_product_money}).first()
78
  if result and result.more_images:
79
- return json.loads(result.more_images)
 
 
 
 
 
 
 
 
 
 
80
 
81
  return []
82
 
@@ -101,7 +112,15 @@ def get_gemini_response(model_name: str, encoded_images: List[Dict], prompt: str
101
  """Get response from a Gemini model."""
102
  try:
103
  model = genai.GenerativeModel(model_name)
104
- response = model.generate_content(encoded_images + [prompt])
 
 
 
 
 
 
 
 
105
  return response.text
106
  except Exception as e:
107
  return f"Error with {model_name}: {str(e)}"
@@ -109,28 +128,35 @@ def get_gemini_response(model_name: str, encoded_images: List[Dict], prompt: str
109
  def process_input(id_product_money: str, prompt: str) -> Tuple[List[str], str, str]:
110
  """Main processing function."""
111
  try:
112
- # Get marketplace and main image
113
  marketplace, main_image = get_marketplace_and_main_image(id_product_money)
 
 
114
 
115
- # Get additional images
116
  additional_images = get_additional_images(id_product_money, marketplace)
 
117
 
118
  # Combine all images
119
  all_image_urls = [main_image] + additional_images
 
120
 
121
- # Download and encode images
122
  encoded_images = download_and_encode_images(all_image_urls)
 
123
 
124
  if not encoded_images:
125
  raise ValueError("No images could be downloaded")
126
 
 
127
  # Get responses from both models
128
- gemini_1_5_response = get_gemini_response("gemini-1.5-pro", encoded_images, prompt)
129
- gemini_2_0_response = get_gemini_response("gemini-pro-vision", encoded_images, prompt)
130
 
131
  return all_image_urls, gemini_1_5_response, gemini_2_0_response
132
 
133
  except Exception as e:
 
134
  return [], f"Error: {str(e)}", f"Error: {str(e)}"
135
 
136
  # Create Gradio interface
@@ -139,20 +165,20 @@ with gr.Blocks() as demo:
139
 
140
  with gr.Row():
141
  id_input = gr.Textbox(label="Product ID (id_product_money)")
142
- prompt_input = gr.Textbox(label="Prompt for VLMs")
143
 
144
  submit_btn = gr.Button("Analyze")
145
 
146
  with gr.Row():
147
- image_gallery = gr.Gallery(label="Product Images")
148
 
149
  with gr.Row():
150
  with gr.Column():
151
- gr.Markdown("### Gemini 1.5 Pro Response")
152
- gemini_1_5_output = gr.Textbox(label="")
153
  with gr.Column():
154
- gr.Markdown("### Gemini Pro Vision Response")
155
- gemini_2_0_output = gr.Textbox(label="")
156
 
157
  submit_btn.click(
158
  fn=process_input,
 
63
  with db_conn.connect() as connection:
64
  result = connection.execute(query, {"id_product_money": id_product_money}).first()
65
  if result and result.more_images:
66
+ print(f"Lamoda raw more_images: {result.more_images}")
67
  paths = json.loads(result.more_images)
68
  return [f"https://a.lmcdn.ru/product{path}" for path in paths]
69
 
70
  elif marketplace == 'wildberries':
71
  query = text("""
72
+ select features->>'images' as more_images
73
  from public.wb_chrc
74
  where id_product_money = :id_product_money
75
  limit 1
 
77
  with db_conn.connect() as connection:
78
  result = connection.execute(query, {"id_product_money": id_product_money}).first()
79
  if result and result.more_images:
80
+ print(f"Wildberries raw more_images: {result.more_images}")
81
+ try:
82
+ urls = json.loads(result.more_images)
83
+ if isinstance(urls, list) and len(urls) > 0:
84
+ # Split the URLs by semicolons
85
+ return urls[0].split(';')
86
+ return []
87
+ except Exception as e:
88
+ print(f"Error parsing JSON: {str(e)}")
89
+ print(f"Type of more_images: {type(result.more_images)}")
90
+ return []
91
 
92
  return []
93
 
 
112
  """Get response from a Gemini model."""
113
  try:
114
  model = genai.GenerativeModel(model_name)
115
+ # Create a list of content parts
116
+ content = []
117
+ # Add each image as a separate content part
118
+ for img in encoded_images:
119
+ content.append(img)
120
+ # Add the prompt as the final content part
121
+ content.append(prompt)
122
+ # Generate response
123
+ response = model.generate_content(content)
124
  return response.text
125
  except Exception as e:
126
  return f"Error with {model_name}: {str(e)}"
 
128
  def process_input(id_product_money: str, prompt: str) -> Tuple[List[str], str, str]:
129
  """Main processing function."""
130
  try:
131
+ print("Getting marketplace and main image...")
132
  marketplace, main_image = get_marketplace_and_main_image(id_product_money)
133
+ print(f"Marketplace: {marketplace}")
134
+ print(f"Main image: {main_image}")
135
 
136
+ print("\nGetting additional images...")
137
  additional_images = get_additional_images(id_product_money, marketplace)
138
+ print(f"Additional images: {additional_images}")
139
 
140
  # Combine all images
141
  all_image_urls = [main_image] + additional_images
142
+ print(f"\nAll image URLs: {all_image_urls}")
143
 
144
+ print("\nDownloading and encoding images...")
145
  encoded_images = download_and_encode_images(all_image_urls)
146
+ print(f"Number of encoded images: {len(encoded_images)}")
147
 
148
  if not encoded_images:
149
  raise ValueError("No images could be downloaded")
150
 
151
+ print("\nGetting Gemini responses...")
152
  # Get responses from both models
153
+ gemini_1_5_response = get_gemini_response("gemini-1.5-flash", encoded_images, prompt)
154
+ gemini_2_0_response = get_gemini_response("gemini-2.0-flash-exp", encoded_images, prompt)
155
 
156
  return all_image_urls, gemini_1_5_response, gemini_2_0_response
157
 
158
  except Exception as e:
159
+ print(f"\nError in process_input: {str(e)}")
160
  return [], f"Error: {str(e)}", f"Error: {str(e)}"
161
 
162
  # Create Gradio interface
 
165
 
166
  with gr.Row():
167
  id_input = gr.Textbox(label="Product ID (id_product_money)")
168
+ prompt_input = gr.Textbox(label="Prompt for VLMs", value="What is this?")
169
 
170
  submit_btn = gr.Button("Analyze")
171
 
172
  with gr.Row():
173
+ image_gallery = gr.Gallery(label="Product Images", show_label=True)
174
 
175
  with gr.Row():
176
  with gr.Column():
177
+ gr.Markdown("### Gemini 1.5 Flash Response")
178
+ gemini_1_5_output = gr.Textbox(label="", show_copy_button=True)
179
  with gr.Column():
180
+ gr.Markdown("### Gemini 2.0 Flash Exp Response")
181
+ gemini_2_0_output = gr.Textbox(label="", show_copy_button=True)
182
 
183
  submit_btn.click(
184
  fn=process_input,