import streamlit as st import re # Use a pipeline as a high-level helper # # Load model directly # from transformers import AutoTokenizer, AutoModelForCausalLM # tokenizer = AutoTokenizer.from_pretrained("shivanikerai/TinyLlama-1.1B-Chat-v1.0-sku-title-ner-generation-reversed-v1.0") # model = AutoModelForCausalLM.from_pretrained("shivanikerai/TinyLlama-1.1B-Chat-v1.0-sku-title-ner-generation-reversed-v1.0") # import requests # import os # token=os.environ.get("HUGGING_FACE_TOKEN") # API_URL = "https://api-inference.huggingface.co/models/shivanikerai/TinyLlama-1.1B-Chat-v1.0-sku-title-ner-generation-reversed-v1.0" # headers = {"Authorization": f"Bearer {token}"} # def query(payload): # response = requests.post(API_URL, headers=headers, json=payload) # return response.json() # Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-generation", model="shivanikerai/TinyLlama-1.1B-Chat-v1.0-sku-title-ner-generation-reversed-v1.0") def convert_to_dictionary(input_string): try: input_string = input_string.replace('', '') input_string = input_string.replace("\n ","\n") input_string = input_string.replace(" :",":") input_string = input_string.replace("\n"," ") data_dict = {} for item in input_string.split('", "'): key, value = item.split('": "') key = key.strip('{}"') value = value.strip('{}"') data_dict[key] = value inverted_dict = {} for key, value in data_dict.items(): if value in inverted_dict: if not isinstance(inverted_dict[value], list): inverted_dict[value] = [inverted_dict[value]] inverted_dict[value].append(key) else: inverted_dict[value] = [key] return inverted_dict except Exception as e: #print(f"\nAn error occurred: {e}\n{input_string}") return({}) def main(): st.title("Amazon Title Suggestion") initialize_state() if not st.session_state.submitted_title: submit_title() elif st.session_state.submitted_title and not st.session_state.submitted_ner_keywords: submit_ner_keywords() if st.button("Reset"): reset_app() def initialize_state(): if "title" not in st.session_state: st.session_state.title = "" if "ner_dict" not in st.session_state: st.session_state.ner_dict = {} if "selected_keywords" not in st.session_state: st.session_state.selected_keywords = [] if "submitted_title" not in st.session_state: st.session_state.submitted_title = False if "submitted_ner_keywords" not in st.session_state: st.session_state.submitted_ner_keywords = False def submit_title(): title = st.text_input("Enter Product Title:", value=st.session_state.title) if st.button("Submit Title"): st.session_state.title = title ner = ner_title(title) st.session_state.ner_dict = ner st.session_state.submitted_title = True st.write(ner) def submit_ner_keywords(): st.write("Title:", st.session_state.title) st.subheader("Product Features:") selected_features = [] for key, value in st.session_state.ner_dict.items(): if st.checkbox(f"{key}: {value}"): selected_features.append(value) st.subheader("Select Search Terms:") keyword_list =list(set("""shampoo head & shoulder, shampoo and conditioner combo, skincare+, dandruff treatment, seaweed, body&wash, conditioners for dry and frizzy hair, shampoo brush, conditioner for women, l'orã©al professionnel, head and shoulder shampoo, hair cream for women, conditioners, scalpe plus anti-dandruff shampoo, hair shampoo for women, sampoo offer 1litter today, tresses shampoo, conditioner for men, head and shoulder shampoo 1 ltr, anti dandruff shampoo for women, conditioner, wholesale skincare australia, hair fall control shampoo, dandruff shampoo, anti dandruff shampoo for men, scalpe, best affordable skin care australia, hair conditioner, scalp massager, head and shoulders anti dandruff shampoo, clinic plus shampoo, skin care australia, shampoo for men, hair conditioner for dry frizzy hair, best shampoo, professional shampoo, dandruff remover, leave in conditioner for frizzy hair, sampoo, xtenso shampoo, best shampoo for hair growth, conditioner for frizzy hair, hair care, biotech shampoo, head n shoulder shampoo, onion shampoo, skin care, guarana, biotin shampoo, clinic plus, scalp massager shampoo brush, selsun shampoo anti dandruff, dream length shampoo, clinic plus shampoo 1ltr, men shampoo, hair growth shampoo, hair products, leave in conditioner for curly hair, selsun shampoo, best skincare brands australia, shampoo 1 litre, conditioner 1 litre offer, shampoos, skincare australia, best skin care products australia, l'orãƒâ©al professionnel, kera gain black shampoo, skin care products, best organic skin care australia, shampoo for women, onion shampoo for hair growth, keratin shampoo, hair shampoo, shampoo 1 litre offer, sulphate and paraben free shampoo, vitamin e, sampoo all offer, head and shoulder shampoo 2 in 1, biotic shampoo, keragain hair color shampoo black, egg shampoo, shampoo clinic plus, sulphate free shampoo, shampoo for hair fall control, tea tree shampoo, skin care product, shampoo for dry and frizzy hair, leave in conditioner, skin care packs ausrtalia, conditioner for dry hair, ketomac shampoo anti dandruff, sulphate free shampoo for women, halo shampoo, skin care face mask, hair conditioners for women, scalp shampoo, organic skin care australia, paraben and sulphate free shampoo, 8x shampoo, ketoconazole shampoo, best australian skin care products, ketoconazole shampoo anti dandruff shampoo, shampoo in fresh, scalpe pro, eucalyptus, head and shoulder, herbal essences, ayurvedic shampoo, hair conditioner for men, hair colour shampoo black natural sulphate and paraben free shampoo, organic natural shampoo, best shampoo for hair, anti dandruff shampoo mens, mens hair thickening shampoo and conditioner, organic hair shampoo, color protecting shampoo, hair shampoos for oily hair, red hair shampoos, natural shampoo and conditioner, best shampoo, dry shampoo, dandruff shampoo, charcoal shampoo, hair fall control shampoo, best shampoo for hair growth, hair shampoos for thinning hair, shampoo for hairfall, best shampoo for hairfall, shampoo for straightened hair, natural hair shampoo bar, organic hair shampoo for women, thin hair shampoos, natural shampoo, anti hair fall shampoo, gray hair shampoo men, anti dandruff shampoo curly hair, beard shampoo, natural hair shampoos, natural hair shampoo, natural hair shampoo for black women, anti dandruff shampoo oily hair, scalp cleanser, hair colour shampoo, organic hair shampoos, red hair shampoo care, shampoo for anti hair fall, anti dandruff shampoo for men, hair shampoos for hair loss, red hair shampoo, hair straightening shampoos, mild shampoo, gray hair shampoo bar hair, hair wash, gray hair shampoos, sulphate free shampoo, hair straightening cream, naturals shampoo, anti dandruff shampoo and conditioner set, gray hair shampoo, antidandruff, anti dandruff shampoo 2 in 1, anti dandruff shampoo for colored hair, anti dandruff shampoo, keratin shampoo, hair loss shampoo, hair fall shampoo, amla natural hair shampoos, best natural shampoo, hair shampoo, natural protein, dandruff, shampoo, shampoo and conditioner, red hair shampoo dye, volumizing shampoo, natural hair shampoo and conditioner, anti dandruff shampoo organic, anti hair loss shampoo, hair straightening shampoo for women, thin hair shampoo, hair straightening shampoo, anti frizz shampoo, best shampoos for dry hair, shampoo hair regrowth, coloured hair shampoo, shampoo anti hair fall, natural hair shampoo and conditioner for black women kids shampoo for 5 to 10 years, baby shampoo for new born, baby shampoo for kids 3+ years, shampoo for kids, baby chakra product, shampoo for kids 5to10, kids shampoo for 5 to 10 years girl, baby shampoo for kids 3 years, baby shampoo for kids, baby shampoo for 1 year old, kids shampoo 2-5 years, baby shampoo in fresh, baby body wash and shampoo, baby shampoo, baby wash and shampoo baby shampoo, shampoo for kids, baby shampoos, baby shampoo for kids, pregnancy products for women, baby care kit amla reetha shikakai shampoo, dry shampoo for women spray, shampoo powder, dry hair, kunkudukayalu, dry shampoo spray, powder shampoo for hair, hair wash powder, herbal hair wash powder curly hair conditioner and shampoo, shampoo conditioner, ogx shampoo and conditioner, shampoo & conditioner sets, bed head shampoo, hask shampoo, hair shampoo and conditioner avocado nourish shampoo, shampoo anti hair fall, beard shampoo for men, hair wash, hair shampoo for dry hair, naturals shampoo, hair strengthening shampoo, dry scalp repair shampoo, hairfall control shampoo, shampoos, offers hair shampoos, dry and frizzy hair shampoo, dexe hair shampoo, dandruff control shampoo, shampoo with avocado, daily using mild shampoo, daily shine shampoo, curry leaves and biotin shampoo, curly hair shampoo, shampoo color hair, shampoo sulphate free paraben free, oily hair shampoo, coloured hair shampoo, shampoo sulphate free, shampoo natural, shampoo keratin, color protecting shampoo, shampoo hair regrowth, charcoal shampoos, shampoo hair colour, shampoo for women, charcoal shampoo, onion and jojoba oil hairfall shampoo, shampoo for straightened hair, hairfall shampoo best, onion hair shampoo, best shampoo for hairfall, onion shampoo, shampoo for hair growth, hairfall shampoos for women, best shampoo for hair growth, best shampoo for hair, hairfall soultion shampoo, best shampoo, hair shampoo for women, shampoo for hairfall, herbal hair shampoo, onion shampoo for hair fall, herbal mild shampoo for dry hair, best natural shampoo, best hair shampoo for hair growth, onion shampoo for hair growth, organic hair shampoo, best anti dandruff shampoo for men, organic handmade shampoo chemical free, organic natural shampoo, beard shampoo, baby wash & shampoo, ayurvedic shampoo, organo gold red onion shampoo, avocado shampoo for hair, protein hair shampoo, red onion hair shampoo, avocado shampoo, art nourish shampoo, argan oil shampoo, anti hairfall shampoo for women, shampoo for hair fall control, anti hairfall shampoo, anti hair loss shampoo, anti hair fall shampoo, anti frizz shampoo, shampoos for hair, anti dandruff shampoo, aloe vera shampoo for hair growth, aloe vera shampoo, almond and honey shampoo, shampoo for coloured hair, volumizing shampoo, shampoo for anti hair fall, hairfall, anti dandruff shampoo for men, dandruff shampoo, shampoo, shampoo 1 litre offer, shampoo and conditioner combo, hair shampoo for men, keratin hair shampoo, intense repair shampoo, keratin shampoo, mild hair shampoo, sulphate free shampoo for women, mild shampoo, mild shampoo for daily use, natural hair fall arrest shampoo, natural hair shampoo, sulphate free shampoo for men, natural pollution defense shampoo, natural shampoo, sulphate free shampoo, hair loss shampoo, hair shampoo, natural shampoo 340ml, natural shampoo and conditioner, natural shampoo colour, hair growth shampoo, hair fall shampoo, natural shampoo for men, natural shampoo for men cheap, hair fall rescue shampoo, sulphate and paraben free shampoo, hair fall control shampoo, hair fall best shampoo, straight hair shampoo, hair colour shampoo, smooth hair shampoo, natural shampoo for women, hairfall shampoo, for hair fall and regrowth shampoo, shikakai shampoo, shampoos anti hair fall mens shampoo, men shampoo, 2 in 1 shampoo and conditioner, shampoo for men, shampoo for color treated hair, color safe shampoo and conditioner 1821 man made, 3 in 1 shampoo conditioner body wash, mens 3 in 1 shampoo conditioner body wash dry shampoo powder, travel size dry shampoo, travel dry shampoo, dry shampoo travel size, dry shampoo for dark hair, dry shampoo batiste hair growth shampoo, hair loss shampoo, routine shampoo for women hair loss, ginger hair regrowth shampoo bar leave in conditioner for dry damaged hair, leave in conditioner for curly hair, organic shampoo, shampoos, medicated shampoo, hair growth for women, shampoos for men, shampoo hair growth, detangler spray, hair growth, leave in conditioner spray, rice water for hair growth, hair gel, shampoo dandruff, shampoos for hair growth, lice treatment shampoo, shampoo bar, lice shampoo that kills eggs and lice, shampoo conditioner, shampoo for black women, head lice treatment, lice shampoo for kids, shampoos for oily hair, lice shampoo, shampoo sulfate free, lice prevention shampoo, tea tree shampoo paul mitchell, indian shampoo and conditioner, rid lice treatment, lice treatment for kids, shampoo hair loss, indian shampoo, lice treatment, lice prevention spray, cream of natural shampoo, coffee shampoo for hair growth, head lice treatment kit, dandruff shampoo, tea tree shampoo, shampoo with conditioner, shampoo shea moisture, shampoo para la caspa, coconut shampoo, shampoo and conditioner set coconut, shampoo for dandruff, shampoos thinning hair, shampoo, purple shampoo, minoxidil for women, shampoo for lice and nits, shampoos and conditioners, shampoos maui, shampoos for thinning hair, shampoos and conditioners for curly hair, leave in conditioner, shampoo curly hair, dry shampoo, ayurvedic shampoo, shampoo ketoconazole""".replace("\n",", ").split(", "))) keyword_list=keyword_list[:20]#['a','b','c','f','g',"Feature", "Price", "Quality", "Availability"] # for keyword in keyword_list: # st.checkbox(keyword, key=keyword) col1, col2, col3, col4 = st.columns(4) columns = [col1, col2, col3, col4] # Distribute keywords evenly across the four columns for i, keyword in enumerate(keyword_list): with columns[i % 4]: if st.checkbox(keyword, key=keyword): selected_features.append(keyword) if st.button("Suggest Titles"): model2_keywords = [keyword for keyword in keyword_list if st.session_state[keyword]] st.session_state.selected_keywords = model2_keywords st.session_state.submitted_ner_keywords = True st.write("Selected Keywords for Model2:", model2_keywords) st.write("Selected features for Model2:", selected_features) def ner_title(title): # Define the roles and markers B_SYS, E_SYS = "<>", "<>" B_INST, E_INST = "[INST]", "[/INST]" B_in, E_in = "[Title]", "[/Title]" # Format your prompt template prompt = f"""{B_INST} {B_SYS} You are a helpful assistant that provides accurate and concise responses. {E_SYS}\nExtract named entities from the given product title. Provide the output in JSON format.\n{B_in} {title.strip()} {E_in}\n{E_INST}\n\n### NER Response:\n{{"{title.split()[0].lower()}""" output= pipe(prompt) # output = query({ # "inputs": prompt, # }) # encoding = tokenizer(prompt, return_tensors="pt").to("cuda:0") # output = model.generate(input_ids=encoding.input_ids, # attention_mask=encoding.attention_mask, # max_new_tokens=512, # do_sample=True, # temperature=0.01, # eos_token_id=tokenizer.eos_token_id, # top_k=0) # Subtract the length of input_ids from output to get only the model's response # output_text = tokenizer.decode(output[0, len(encoding.input_ids[0]):], skip_special_tokens=False) # output = re.sub('\n+', '\n', output_text) # remove excessive newline characters #output = f"""{{\"{title.split()[0].lower()} {output_text}""" #output = re.sub(' ": "', '": "', output) #return eval(pipe(text)[0]["generated_text"].split("### NER Response:\n")[-1]) return output# convert_to_dictionary((output[0]['generated_text'].split("### NER Response:\n")[-1])) def reset_app(): # Clear relevant session states st.session_state.title = "" st.session_state.ner_dict = {} st.session_state.selected_keywords = [] st.session_state.submitted_title = False st.session_state.submitted_ner_keywords = False # Optionally clear any dynamic states you may have added # such as checkboxes or other inputs dynamic_keys = [key for key in st.session_state.keys() if key.startswith('dynamic_')] for key in dynamic_keys: del st.session_state[key] # Display the initial title input UI submit_title() # def reset_app(): # # Clear all session states # for key in list(st.session_state.keys()): # del st.session_state[key] # # Reinitialize state # initialize_state() # # Rerun the app to reset the UI # st.experimental_rerun() if __name__ == "__main__": main()