Spaces:
Sleeping
Sleeping
File size: 5,562 Bytes
3a385c0 c5e64d6 3a385c0 c5e64d6 3a385c0 c5e64d6 44e1aeb 200bbb5 3a385c0 200bbb5 44e1aeb c5e64d6 200bbb5 c5e64d6 3a385c0 c5e64d6 3a385c0 c5e64d6 3a385c0 c5e64d6 3a385c0 c5e64d6 3a385c0 c5e64d6 3a385c0 c5e64d6 3a385c0 c5e64d6 200bbb5 c5e64d6 3a385c0 c5e64d6 3a385c0 c5e64d6 3a385c0 c5e64d6 3a385c0 c5e64d6 3a385c0 200bbb5 3a385c0 c5e64d6 3a385c0 200bbb5 3a385c0 200bbb5 c5e64d6 3a385c0 c5e64d6 200bbb5 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
from itertools import product
import streamlit as st
import numpy as np
import pandas as pd
from PIL import Image, ImageOps
import time
from paddleocr import PaddleOCR
import os
# from dotenv import load_dotenv
import torch
from transformers import AutoTokenizer, AutoModelForQuestionAnswering
# Load environment variables
# load_dotenv()
# huggingface_token = os.getenv("HF_TOKEN")
# Load TinyBERT model and tokenizer
tokenizer = AutoTokenizer.from_pretrained("Intel/dynamic_tinybert")
model = AutoModelForQuestionAnswering.from_pretrained("Intel/dynamic_tinybert")
# Initialize PaddleOCR
ocr = PaddleOCR(use_angle_cls=True, lang='en')
# Team details
team_members = [
{"name": "Aman Deep", "image": "aman.jpg"}, # Replace with actual paths to images
{"name": "Nandini", "image": "nandini.jpg"},
{"name": "Abhay Sharma", "image": "abhay.jpg"},
{"name": "Ratan Prakash Mishra", "image": "anandimg.jpg"}
]
# Function to preprocess images for the model
def preprocess_image(image):
"""
Preprocess the input image for model prediction.
Args:
image (PIL.Image): Input image in PIL format.
Returns:
np.ndarray: Preprocessed image array ready for prediction.
"""
try:
img = image.resize((128, 128), Image.LANCZOS)
img_array = np.array(img)
if img_array.ndim == 2: # Grayscale image
img_array = np.stack([img_array] * 3, axis=-1)
elif img_array.shape[2] == 1: # Single-channel image
img_array = np.concatenate([img_array, img_array, img_array], axis=-1)
img_array = img_array / 255.0
img_array = np.expand_dims(img_array, axis=0)
return img_array
except Exception as e:
print(f"Error processing image: {e}")
return None
# Function to perform Q&A with TinyBERT
def answer_question(context, question):
"""
Extract the answer to a question from the given context using TinyBERT.
Args:
context (str): The text to search for answers.
question (str): The question to answer.
Returns:
str: The extracted answer or an error message.
"""
try:
tokens = tokenizer.encode_plus(question, context, return_tensors="pt", truncation=True)
input_ids = tokens["input_ids"]
attention_mask = tokens["attention_mask"]
# Perform question answering
outputs = model(input_ids, attention_mask=attention_mask)
start_scores = outputs.start_logits
end_scores = outputs.end_logits
answer_start = torch.argmax(start_scores)
answer_end = torch.argmax(end_scores) + 1
answer = tokenizer.convert_tokens_to_string(tokenizer.convert_ids_to_tokens(input_ids[0][answer_start:answer_end]))
return answer
except Exception as e:
return f"Error: {e}"
# Function to display team members in circular format
def display_team_members(members, max_members_per_row=4):
num_members = len(members)
num_rows = (num_members + max_members_per_row - 1) // max_members_per_row
for i in range(num_rows):
cols = st.columns(min(max_members_per_row, num_members - i * max_members_per_row))
for j, member in enumerate(members[i * max_members_per_row:(i + 1) * max_members_per_row]):
with cols[j]:
img = Image.open(member["image"])
st.image(img, use_column_width=True)
st.write(member["name"])
# Function to simulate loading process with a progress bar
def simulate_progress():
progress_bar = st.progress(0)
for percent_complete in range(100):
time.sleep(0.02)
progress_bar.progress(percent_complete + 1)
# Title and description
st.title("Product Listing Assistant")
# Navbar with task tabs
st.sidebar.title("Navigation")
st.sidebar.write("Team Name: Sadhya")
app_mode = st.sidebar.selectbox("Choose the task", ["Welcome", "Project Details", "Team Details", "Extract Product Details"])
if app_mode == "Welcome":
st.write("# Welcome to the Product Listing Assistant! 🎉")
elif app_mode == "Project Details":
st.write("""
## Project Overview:
- Automates product listings from social media content.
- Extracts product details from posts using OCR and Q&A.
- Outputs structured, engaging, and optimized e-commerce listings.
""")
elif app_mode == "Team Details":
st.write("## Meet Our Team:")
display_team_members(team_members)
elif app_mode == "Extract Product Details":
st.write("## Extract Product Details Using OCR and Q&A")
post_url = st.text_input("Enter Post URL:")
uploaded_files = st.file_uploader("Upload Product Images", type=["jpeg", "png", "jpg"], accept_multiple_files=True)
user_question = st.text_input("Ask a question about the extracted details:")
if uploaded_files:
st.write("### Uploaded Images:")
simulate_progress()
for uploaded_image in uploaded_files:
image = Image.open(uploaded_image)
st.image(image, use_column_width=True)
simulate_progress()
# Perform OCR
st.write("Extracting text from image...")
result = ocr.ocr(np.array(image), cls=True)
extracted_text = " ".join([line[1][0] for line in result[0]])
st.write("Extracted Text:")
st.text(extracted_text)
# Use Q&A model
if user_question:
st.write("### Answer to your question:")
answer = answer_question(extracted_text, user_question)
st.write(answer)
|