asdfaman commited on
Commit
097bb8f
·
verified ·
1 Parent(s): e54b061

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +113 -200
app.py CHANGED
@@ -1,67 +1,92 @@
 
1
  import streamlit as st
2
- from ultralytics import YOLO
3
- import tensorflow as tf # Change this to import TensorFlow
4
  import numpy as np
5
- from PIL import Image, ImageOps, ImageDraw, ImageFont
6
  import pandas as pd
 
7
  import time
8
  from paddleocr import PaddleOCR, draw_ocr
9
- import re
10
- import dateparser
11
  import os
12
- import matplotlib.pyplot as plt
13
-
14
- #######Llama3bi integration########
15
- import torch
16
- from transformers import pipeline
17
- model_id = "meta-llama/Llama-3.2-3B-Instruct"
18
- pipe = pipeline(
19
- "text-generation",
20
- model=model_id,
21
- torch_dtype=torch.bfloat16,
22
- device_map="auto",
23
- )
24
  messages = [
25
- {"role": "system", "content": """Your task is to get the product details out of the text given. The text given will be raw text from OCR of social media images of products,
26
- and the goal is to get product details and description so that it can be used for amazon product listing. """},
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27
  ]
28
 
 
29
  # Function to get Instagram post details
30
  import instaloader
 
 
31
  def get_instagram_post_details(post_url):
32
  try:
 
 
 
 
33
  shortcode = post_url.split('/')[-2]
 
 
34
  post = instaloader.Post.from_shortcode(L.context, shortcode)
35
 
36
- # Retrieve caption and image URL
37
  caption = post.caption
38
- image_url = post.url
39
 
40
- return caption, image_url
 
 
 
 
 
 
 
 
 
 
 
 
41
  except Exception as e:
42
  return str(e), None
43
 
44
-
45
  # Initialize PaddleOCR model
46
  ocr = PaddleOCR(use_angle_cls=True, lang='en')
47
 
48
  # Team details
49
  team_members = [
50
  {"name": "Aman Deep", "image": "aman.jpg"}, # Replace with actual paths to images
51
- {"name": "Nandini", "image": "myimage.jpg"},
52
- {"name": "Abhay Sharma", "image": "gaurav.jpg"},
53
- {"name": "Ratan Prakash Mishra", "image": "anandimg.jpg"}
54
  ]
55
 
56
  # Function to preprocess the images for the model
57
-
58
  def preprocess_image(image):
59
  """
60
  Preprocess the input image for model prediction.
61
-
62
  Args:
63
  image (PIL.Image): Input image in PIL format.
64
-
65
  Returns:
66
  np.ndarray: Preprocessed image array ready for prediction.
67
  """
@@ -90,58 +115,6 @@ def preprocess_image(image):
90
  print(f"Error processing image: {e}")
91
  return None # Return None if there's an error
92
 
93
-
94
- # Function to create a high-quality circular mask for an image
95
- def make_image_circular1(img, size=(256, 256)):
96
- img = img.resize(size, Image.LANCZOS)
97
- mask = Image.new("L", size, 0)
98
- draw = ImageDraw.Draw(mask)
99
- draw.ellipse((0, 0) + size, fill=255)
100
- output = ImageOps.fit(img, mask.size, centering=(0.5, 0.5))
101
- output.putalpha(mask) # Apply the mask as transparency
102
- return output
103
-
104
-
105
- # Function to check if a file exists
106
- def file_exists(file_path):
107
- return os.path.isfile(file_path)
108
-
109
- def make_image_circular(image):
110
- # Create a circular mask
111
- mask = Image.new("L", image.size, 0)
112
- draw = ImageDraw.Draw(mask)
113
- draw.ellipse((0, 0, image.size[0], image.size[1]), fill=255)
114
-
115
- # Apply the mask to the image
116
- circular_image = Image.new("RGB", image.size)
117
- circular_image.paste(image.convert("RGBA"), (0, 0), mask)
118
-
119
- return circular_image
120
-
121
- # Function to extract dates from recognized text using regex
122
- def extract_dates_with_dateparser(texts, result):
123
- date_texts = []
124
- date_boxes = []
125
- date_scores = []
126
-
127
- def is_potential_date(text):
128
- valid_date_pattern = r'^(0[1-9]|[12][0-9]|3[01])[-/.]?(0[1-9]|1[0-2])[-/.]?(\d{2}|\d{4})$|' \
129
- r'^(0[1-9]|[12][0-9]|3[01])[-/.]?[A-Za-z]{3}[-/.]?(\d{2}|\d{4})$|' \
130
- r'^(0[1-9]|1[0-2])[-/.]?(\d{2}|\d{4})$|' \
131
- r'^[A-Za-z]{3}[-/.]?(\d{2}|\d{4})$'
132
- return bool(re.match(valid_date_pattern, text))
133
-
134
- dates_found = []
135
- for i, text in enumerate(texts):
136
- if is_potential_date(text): # Only process texts that are potential dates
137
- parsed_date = dateparser.parse(text, settings={'DATE_ORDER': 'DMY'})
138
- if parsed_date:
139
- dates_found.append(parsed_date.strftime('%Y-%m-%d')) # Store as 'YYYY-MM-DD'
140
- date_texts.append(text) # Store the original text
141
- date_boxes.append(result[0][i][0]) # Store the bounding box
142
- date_scores.append(result[0][i][1][1]) # Store confidence score
143
- return dates_found, date_texts, date_boxes, date_scores
144
-
145
  # Function to display circular images in a matrix format
146
  def display_images_in_grid(images, max_images_per_row=4):
147
  num_images = len(images)
@@ -163,10 +136,18 @@ def display_team_members(members, max_members_per_row=4):
163
  for j, member in enumerate(members[i * max_members_per_row:(i + 1) * max_members_per_row]):
164
  with cols[j]:
165
  img = Image.open(member["image"]) # Load the image
166
- circular_img = make_image_circular(img) # Convert to circular format
 
167
  st.image(circular_img, use_column_width=True) # Display the circular image
168
  st.write(member["name"]) # Display the name below the image
169
 
 
 
 
 
 
 
 
170
  # Title and description
171
  st.title("Amazon Smbhav")
172
  # Team Details with links
@@ -262,7 +243,6 @@ elif app_mode=="Project Details":
262
  - **Multi-Market Compatibility:** Expand support to other e-commerce platforms.
263
 
264
  This approach automates listing creation directly from social media content, helping sellers quickly launch optimized Amazon product pages.
265
-
266
  """)
267
 
268
  elif app_mode == "Team Details":
@@ -275,7 +255,6 @@ elif app_mode == "Task 1":
275
  st.write("## Task 1: 🖼️ OCR to Extract Details 📄")
276
  st.write("Using OCR to extract details from product packaging material, including brand name and pack size.")
277
 
278
-
279
  # Instantiate Instaloader
280
  L = instaloader.Instaloader()
281
 
@@ -286,39 +265,39 @@ elif app_mode == "Task 1":
286
  post_url = st.text_input("Enter Instagram Post URL:")
287
 
288
  if post_url:
289
- caption, image_path = get_instagram_post_details(post_url)
 
 
 
 
 
290
 
291
- if image_path and os.path.exists(image_path):
292
- st.subheader("Caption:")
293
- st.write(caption)
294
- st.subheader("Image:")
295
-
296
- # Load and display the image
297
- image = Image.open(image_path)
298
- st.image(image, use_column_width=True)
299
-
300
- # Convert image to numpy array for OCR processing
301
- img_array = np.array(image)
302
-
303
- # Perform OCR on the image
304
- st.write(f"Extracting details from {uploaded_image.name}...")
305
- result = ocr.ocr(img_array, cls=True)
306
-
307
- #############################
308
- #OCR result text to be parsed here through LLM and get product listing content.
309
- messages.append({"role": "user", "content": ""})
310
- outputs = pipe(
311
- messages,
312
- max_new_tokens=256,
313
- )
314
- productListingContent = outputs[0]["generated_text"][-1]
315
- st.markdown(productListingContent)
316
- #############################
317
-
318
- # Clean up (optional)
319
- os.remove(image_path)
320
- else:
321
- st.error("Failed to retrieve the post details. Please check the URL.")
322
 
323
  # File uploader for images (supports multiple files)
324
  uploaded_files = st.file_uploader("Upload images of products", type=["jpeg", "png", "jpg"], accept_multiple_files=True)
@@ -329,112 +308,46 @@ elif app_mode == "Task 1":
329
 
330
  for uploaded_file in uploaded_files:
331
  img = Image.open(uploaded_file)
332
- circular_img = make_image_circular(img) # Create circular images
333
- circular_images.append(circular_img)
334
 
335
  # Display the circular images in a matrix/grid format
336
  display_images_in_grid(circular_images, max_images_per_row=4)
337
 
338
- # Function to simulate loading process with a progress bar
339
- def simulate_progress():
340
- progress_bar = st.progress(0)
341
- for percent_complete in range(100):
342
- time.sleep(0.02)
343
- progress_bar.progress(percent_complete + 1)
344
- # Function to remove gibberish using regex (removes non-alphanumeric chars, filters out very short text)
345
- def clean_text(text):
346
- # Keep text with letters, digits, and spaces, and remove short/irrelevant text
347
- return re.sub(r'[^a-zA-Z0-9\s]', '', text).strip()
348
-
349
- # Function to extract the most prominent text (product name) and other details
350
- def extract_product_info(results):
351
- product_name = ""
352
- product_details = ""
353
- largest_text_size = 0
354
-
355
- for line in results:
356
- for box in line:
357
- text, confidence = box[1][0], box[1][1]
358
- text_size = box[0][2][1] - box[0][0][1] # Calculate height of the text box
359
-
360
- # Clean the text to avoid gibberish
361
- clean_text_line = clean_text(text)
362
-
363
- if confidence > 0.7 and len(clean_text_line) > 2: # Only consider confident, meaningful text
364
- if text_size > largest_text_size: # Assume the largest text is the product name
365
- largest_text_size = text_size
366
- product_name = clean_text_line
367
- else:
368
- product_details += clean_text_line + " "
369
- return product_name, product_details.strip()
370
-
371
  if st.button("Start Analysis"):
372
  simulate_progress()
373
  # Loop through each uploaded image and process them
374
  for uploaded_image in uploaded_files:
375
  # Load the uploaded image
376
  image = Image.open(uploaded_image)
377
- # st.image(image, caption=f'Uploaded Image: {uploaded_image.name}', use_column_width=True)
378
-
379
  # Convert image to numpy array for OCR processing
380
  img_array = np.array(image)
381
 
 
382
  # Perform OCR on the image
383
  st.write(f"Extracting details from {uploaded_image.name}...")
384
  result = ocr.ocr(img_array, cls=True)
385
-
386
- #############################
 
 
 
 
387
  #OCR result text to be parsed here through LLM and get product listing content.
388
- messages.append({"role": "user", "content": ""})
389
- outputs = pipe(
390
- messages,
391
- max_new_tokens=256,
 
 
392
  )
393
- productListingContent = outputs[0]["generated_text"][-1]
394
- st.markdown(productListingContent)
395
- #############################
396
-
397
- # Process the OCR result to extract product name and properties
398
- product_name, product_details = extract_product_info(result)
399
-
400
- # UI display for single image product details
401
- st.markdown("---")
402
- st.markdown(f"### **Product Name:** `{product_name}`")
403
- st.write(f"**Product Properties:** {product_details}")
404
  st.markdown("---")
 
405
 
406
  else:
407
  st.write("Please upload images to extract product details.")
408
 
409
-
410
-
411
- def make_image_circular1(image):
412
- # Create a circular mask
413
- mask = Image.new("L", image.size, 0)
414
- draw = ImageDraw.Draw(mask)
415
- draw.ellipse((0, 0, image.size[0], image.size[1]), fill=255)
416
-
417
- # Apply the mask to the image
418
- circular_image = Image.new("RGB", image.size)
419
- circular_image.paste(image.convert("RGBA"), (0, 0), mask)
420
-
421
- return circular_image
422
-
423
- def display_images_in_grid1(images, max_images_per_row=4):
424
- rows = (len(images) + max_images_per_row - 1) // max_images_per_row # Calculate number of rows needed
425
-
426
- for i in range(0, len(images), max_images_per_row):
427
- cols_to_show = images[i:i + max_images_per_row]
428
-
429
- # Prepare to display in a grid format
430
- cols = st.columns(max_images_per_row) # Create columns dynamically
431
-
432
- for idx, img in enumerate(cols_to_show):
433
- img = img.convert("RGB") # Ensure the image is in RGB mode
434
-
435
- if idx < len(cols):
436
- cols[idx].image(img, use_column_width=True)
437
-
438
  # Footer with animation
439
  st.markdown("""
440
  <style>
@@ -452,4 +365,4 @@ st.markdown("""
452
  <div class="footer">
453
  <p>© 2024 Amazon Smbhav Challenge. All rights reserved.</p>
454
  </div>
455
- """, unsafe_allow_html=True)
 
1
+ from itertools import product
2
  import streamlit as st
 
 
3
  import numpy as np
 
4
  import pandas as pd
5
+ from PIL import Image, ImageOps, ImageDraw, ImageFont
6
  import time
7
  from paddleocr import PaddleOCR, draw_ocr
 
 
8
  import os
9
+ from dotenv import load_dotenv
10
+ import os
11
+ from huggingface_hub import login
12
+ load_dotenv() # Load .env file
13
+ huggingface_token = os.getenv("HF_TOKEN")
14
+ login(huggingface_token)
15
+
16
+ ##########################LLAMA3BI################################
17
+ from huggingface_hub import InferenceClient
18
+ client = InferenceClient(api_key=huggingface_token)
 
 
19
  messages = [
20
+ {"role": "system", "content": """Your task is to get the product details out of the text given.
21
+ The text given will be raw text from OCR of social media images of products,
22
+ and the goal is to get product details and description so that it can be used for amazon product listing.
23
+ TRY TO KEEP THE LISTING IN FOLLOWING FORMAT.
24
+ 📦 [Product Name]
25
+ 💰 Price: $XX.XX
26
+ ✨ Key Features:
27
+ •⁠ ⁠[Main Feature 1]
28
+ •⁠ ⁠[Main Feature 2]
29
+ •⁠ ⁠[Main Feature 3]
30
+ 📸 [Product Image]
31
+ 🏷 Available Now on Amazon
32
+ ✈️ Prime Shipping Available
33
+ 🛍 Shop Now: [Link]
34
+ 🔍 Search: [Main Keywords]
35
+ [#RelevantHashtags] """},
36
  ]
37
 
38
+
39
  # Function to get Instagram post details
40
  import instaloader
41
+ from io import BytesIO
42
+ import requests
43
  def get_instagram_post_details(post_url):
44
  try:
45
+ # Initialize Instaloader
46
+ L = instaloader.Instaloader()
47
+
48
+ # Extract shortcode from URL
49
  shortcode = post_url.split('/')[-2]
50
+
51
+ # Load post using Instaloader
52
  post = instaloader.Post.from_shortcode(L.context, shortcode)
53
 
54
+ # Retrieve caption
55
  caption = post.caption
 
56
 
57
+ # Retrieve the image URL
58
+ image_url = post.url
59
+
60
+ # Fetch image using requests
61
+ response = requests.get(image_url)
62
+ response.raise_for_status() # Raise an exception for failed requests
63
+
64
+ # Open image using PIL and convert to NumPy array
65
+ img = Image.open(BytesIO(response.content))
66
+ img_array = np.array(img)
67
+
68
+ return caption, img_array
69
+
70
  except Exception as e:
71
  return str(e), None
72
 
 
73
  # Initialize PaddleOCR model
74
  ocr = PaddleOCR(use_angle_cls=True, lang='en')
75
 
76
  # Team details
77
  team_members = [
78
  {"name": "Aman Deep", "image": "aman.jpg"}, # Replace with actual paths to images
79
+ {"name": "Nandini", "image": "nandini.jpg"},
80
+ {"name": "Abhay Sharma", "image": "abhay.jpg"},
81
+ {"name": "Ratan Prakash Mishra", "image": "ratan.jpg"}
82
  ]
83
 
84
  # Function to preprocess the images for the model
 
85
  def preprocess_image(image):
86
  """
87
  Preprocess the input image for model prediction.
 
88
  Args:
89
  image (PIL.Image): Input image in PIL format.
 
90
  Returns:
91
  np.ndarray: Preprocessed image array ready for prediction.
92
  """
 
115
  print(f"Error processing image: {e}")
116
  return None # Return None if there's an error
117
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
118
  # Function to display circular images in a matrix format
119
  def display_images_in_grid(images, max_images_per_row=4):
120
  num_images = len(images)
 
136
  for j, member in enumerate(members[i * max_members_per_row:(i + 1) * max_members_per_row]):
137
  with cols[j]:
138
  img = Image.open(member["image"]) # Load the image
139
+ # circular_img = make_image_circular(img) # Convert to circular format
140
+ circular_img = img
141
  st.image(circular_img, use_column_width=True) # Display the circular image
142
  st.write(member["name"]) # Display the name below the image
143
 
144
+ # Function to simulate loading process with a progress bar
145
+ def simulate_progress():
146
+ progress_bar = st.progress(0)
147
+ for percent_complete in range(100):
148
+ time.sleep(0.02)
149
+ progress_bar.progress(percent_complete + 1)
150
+
151
  # Title and description
152
  st.title("Amazon Smbhav")
153
  # Team Details with links
 
243
  - **Multi-Market Compatibility:** Expand support to other e-commerce platforms.
244
 
245
  This approach automates listing creation directly from social media content, helping sellers quickly launch optimized Amazon product pages.
 
246
  """)
247
 
248
  elif app_mode == "Team Details":
 
255
  st.write("## Task 1: 🖼️ OCR to Extract Details 📄")
256
  st.write("Using OCR to extract details from product packaging material, including brand name and pack size.")
257
 
 
258
  # Instantiate Instaloader
259
  L = instaloader.Instaloader()
260
 
 
265
  post_url = st.text_input("Enter Instagram Post URL:")
266
 
267
  if post_url:
268
+ caption, imageArray = get_instagram_post_details(post_url)
269
+ if caption or imageArray.shape[0] > 0:
270
+ st.subheader("Caption:")
271
+ st.write(caption)
272
+ st.subheader("Image:")
273
+ st.image(imageArray, use_column_width=True)
274
 
275
+ # Convert image to numpy array for OCR processing
276
+ img_array = imageArray
277
+
278
+ #############################
279
+ # Perform OCR on the image
280
+ result = ocr.ocr(img_array, cls=True)
281
+ text = ""
282
+ for line in result:
283
+ for box in line:
284
+ currText, confidence = box[1][0], box[1][1]
285
+ text += currText + " "
286
+ message = f"image ocr: {text} Caption: {caption}"
287
+ st.write(message)
288
+ #OCR result text to be parsed here through LLM and get product listing content.
289
+ simulate_progress()
290
+ messages.append({"role": "user", "content": message})
291
+ completion = client.chat.completions.create(
292
+ model="meta-llama/Llama-3.2-3B-Instruct",
293
+ messages=messages,
294
+ max_tokens=500
295
+ )
296
+ productListingContent = completion.choices[0].message
297
+ st.markdown(productListingContent.content)
298
+ #############################
299
+ else:
300
+ st.error("Failed to retrieve the post details. Please check the URL. ////////")
 
 
 
 
 
301
 
302
  # File uploader for images (supports multiple files)
303
  uploaded_files = st.file_uploader("Upload images of products", type=["jpeg", "png", "jpg"], accept_multiple_files=True)
 
308
 
309
  for uploaded_file in uploaded_files:
310
  img = Image.open(uploaded_file)
311
+ circular_images.append(img)
 
312
 
313
  # Display the circular images in a matrix/grid format
314
  display_images_in_grid(circular_images, max_images_per_row=4)
315
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
316
  if st.button("Start Analysis"):
317
  simulate_progress()
318
  # Loop through each uploaded image and process them
319
  for uploaded_image in uploaded_files:
320
  # Load the uploaded image
321
  image = Image.open(uploaded_image)
 
 
322
  # Convert image to numpy array for OCR processing
323
  img_array = np.array(image)
324
 
325
+ # #############################
326
  # Perform OCR on the image
327
  st.write(f"Extracting details from {uploaded_image.name}...")
328
  result = ocr.ocr(img_array, cls=True)
329
+ text = ""
330
+ for line in result:
331
+ for box in line:
332
+ currText, confidence = box[1][0], box[1][1]
333
+ text += currText + " "
334
+ st.write(f"OCR Result: {text}")
335
  #OCR result text to be parsed here through LLM and get product listing content.
336
+ simulate_progress()
337
+ messages.append({"role": "user", "content": text})
338
+ completion = client.chat.completions.create(
339
+ model="meta-llama/Llama-3.2-3B-Instruct",
340
+ messages=messages,
341
+ max_tokens=500
342
  )
343
+ productListingContent = completion.choices[0].message
344
+ st.markdown(productListingContent.content)
 
 
 
 
 
 
 
 
 
345
  st.markdown("---")
346
+ #########################################
347
 
348
  else:
349
  st.write("Please upload images to extract product details.")
350
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
351
  # Footer with animation
352
  st.markdown("""
353
  <style>
 
365
  <div class="footer">
366
  <p>© 2024 Amazon Smbhav Challenge. All rights reserved.</p>
367
  </div>
368
+ """, unsafe_allow_html=True)