Spaces:
Sleeping
Sleeping
add the app file and log_in option
Browse files- app.py +92 -0
- requirements.txt +8 -0
app.py
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import nltk
|
| 4 |
+
import random
|
| 5 |
+
from bs4 import BeautifulSoup
|
| 6 |
+
from sklearn.metrics.pairwise import cosine_similarity
|
| 7 |
+
import numpy as np
|
| 8 |
+
import spacy
|
| 9 |
+
from numpy.linalg import norm
|
| 10 |
+
from sentence_transformers import SentenceTransformer, util
|
| 11 |
+
|
| 12 |
+
# nltk.download('punkt')
|
| 13 |
+
# nltk.download('wordnet')
|
| 14 |
+
# nltk.download('omw-1.4')
|
| 15 |
+
|
| 16 |
+
threshold = 0.65
|
| 17 |
+
sentence_length = 6
|
| 18 |
+
questions = [
|
| 19 |
+
"Is it new or used", "Are there any wear & tear", "Does it come with dust bag, receipt & original box",
|
| 20 |
+
"Are there any scratches, marks", "Are there any fading, stains, discolorization",
|
| 21 |
+
"Is this item customized, repainted or has hardware been replaced", "Is it special edition", "Is there any odour",
|
| 22 |
+
"Are there multiple items or extra add-ons in this listing?",
|
| 23 |
+
"Is there a date code or serial number present on the item?"
|
| 24 |
+
]
|
| 25 |
+
|
| 26 |
+
model = SentenceTransformer("all-MiniLM-L6-v2")
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
def generate_phrases(desc: str, length: int):
|
| 30 |
+
desc_list = desc.split()
|
| 31 |
+
phrase_list = []
|
| 32 |
+
if len(desc_list) >= length:
|
| 33 |
+
for i in range(len(desc_list) - (length - 1)):
|
| 34 |
+
sub_list = []
|
| 35 |
+
for j in range(i, i + length):
|
| 36 |
+
sub_list.append(desc_list[j])
|
| 37 |
+
phrase_list.append(' '.join(sub_list))
|
| 38 |
+
else:
|
| 39 |
+
phrase_list.append(' '.join(desc_list))
|
| 40 |
+
|
| 41 |
+
return phrase_list
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
def find_answers_new(description: str):
|
| 45 |
+
sentences = generate_phrases(description, sentence_length)
|
| 46 |
+
sentences_embedding = model.encode(sentences)
|
| 47 |
+
|
| 48 |
+
answers = []
|
| 49 |
+
|
| 50 |
+
for question in questions:
|
| 51 |
+
query_embedding = model.encode(question)
|
| 52 |
+
similarities = util.cos_sim(query_embedding, sentences_embedding)
|
| 53 |
+
|
| 54 |
+
similarity_i = 0
|
| 55 |
+
|
| 56 |
+
new_row = None
|
| 57 |
+
|
| 58 |
+
for similarity in similarities[0]:
|
| 59 |
+
model_answer = sentences[similarity_i]
|
| 60 |
+
|
| 61 |
+
similarity_i += 1
|
| 62 |
+
|
| 63 |
+
if round(similarity.item(), 2) > threshold:
|
| 64 |
+
if new_row is not None and similarity.item() < new_row['Similarity']:
|
| 65 |
+
continue
|
| 66 |
+
|
| 67 |
+
new_row = {'ModelAnswer': model_answer, 'Similarity': similarity.item()}
|
| 68 |
+
|
| 69 |
+
if new_row is not None:
|
| 70 |
+
answers.append(new_row['ModelAnswer'])
|
| 71 |
+
else:
|
| 72 |
+
answers.append('No answer')
|
| 73 |
+
return answers
|
| 74 |
+
|
| 75 |
+
|
| 76 |
+
authorized_users = [("test_user_account", "test_user_pwd"), ]
|
| 77 |
+
|
| 78 |
+
demo = gr.Interface(fn=find_answers_new,
|
| 79 |
+
inputs=[gr.Textbox(lines=20, label="Item Description", placeholder="Desc Here...")],
|
| 80 |
+
outputs=[gr.Textbox(lines=1, label="Is it new or used"),
|
| 81 |
+
gr.Textbox(lines=1, label="Are there any wear & tear"),
|
| 82 |
+
gr.Textbox(lines=1, label="Does it come with dust bag, receipt & original box"),
|
| 83 |
+
gr.Textbox(lines=1, label="Are there any scratches, marks"),
|
| 84 |
+
gr.Textbox(lines=1, label="Are there any fading, stains, discolorization"),
|
| 85 |
+
gr.Textbox(lines=1,
|
| 86 |
+
label="Is this item customized, repainted or has hardware been replaced"),
|
| 87 |
+
gr.Textbox(lines=1, label="Is it special edition"),
|
| 88 |
+
gr.Textbox(lines=1, label="Is there any odour"),
|
| 89 |
+
gr.Textbox(lines=1, label="Are there multiple items or extra add-ons in this listing?"),
|
| 90 |
+
gr.Textbox(lines=1, label="Is there a date code or serial number present on the item?")
|
| 91 |
+
])
|
| 92 |
+
demo.launch(debug=False, share=True, auth=authorized_users)
|
requirements.txt
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio~=4.36.1
|
| 2 |
+
pandas~=2.2.2
|
| 3 |
+
nltk~=3.8.1
|
| 4 |
+
numpy~=1.26.4
|
| 5 |
+
spacy~=3.7.5
|
| 6 |
+
beautifulsoup4~=4.12.3
|
| 7 |
+
scikit-learn~=1.5.0
|
| 8 |
+
sentence-transformers~=2.2.2
|