Spaces:
Sleeping
Sleeping
Update gradio text
Browse files
app.py
CHANGED
@@ -1,125 +1,124 @@
|
|
1 |
-
|
2 |
-
import
|
3 |
-
|
4 |
-
import
|
5 |
-
import
|
6 |
-
import
|
7 |
-
from langchain.
|
8 |
-
from langchain.
|
9 |
-
from langchain.
|
10 |
-
from langchain.
|
11 |
-
from langchain.
|
12 |
-
from
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
reviews
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
reviews_df
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
df = df.
|
45 |
-
df
|
46 |
-
df["comment"]
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
text =
|
53 |
-
text =
|
54 |
-
text = re.sub(r"
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
OpenAIModel =
|
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 |
-
).launch()
|
|
|
1 |
+
import re
|
2 |
+
from urllib.parse import urlparse, parse_qs
|
3 |
+
import pandas as pd
|
4 |
+
import unicodedata as uni
|
5 |
+
import emoji
|
6 |
+
from langchain.chat_models import ChatOpenAI
|
7 |
+
from langchain.embeddings import HuggingFaceEmbeddings
|
8 |
+
from langchain.document_loaders import DataFrameLoader
|
9 |
+
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
10 |
+
from langchain.vectorstores import FAISS
|
11 |
+
from langchain.chains import RetrievalQA
|
12 |
+
from tokopedia import request_product_id, request_product_review
|
13 |
+
import gradio as gr
|
14 |
+
|
15 |
+
shop_id = ""
|
16 |
+
item_id = ""
|
17 |
+
item = {}
|
18 |
+
LIMIT = 1000 # Limit to 1000 reviews so that processing does not take too long
|
19 |
+
|
20 |
+
def scrape(URL, max_reviews=LIMIT):
|
21 |
+
parsed_url = urlparse(URL)
|
22 |
+
*_, SHOP, PRODUCT_KEY = parsed_url.path.split("/")
|
23 |
+
product_id = request_product_id(SHOP, PRODUCT_KEY).json()["data"]["pdpGetLayout"][
|
24 |
+
"basicInfo"
|
25 |
+
]["id"]
|
26 |
+
all_reviews = []
|
27 |
+
page = 1
|
28 |
+
has_next = True
|
29 |
+
|
30 |
+
while has_next and len(all_reviews) <= max_reviews:
|
31 |
+
response = request_product_review(product_id, page=page)
|
32 |
+
data = response.json()["data"]["productrevGetProductReviewList"]
|
33 |
+
reviews = data["list"]
|
34 |
+
all_reviews.extend(reviews)
|
35 |
+
has_next = data["hasNext"]
|
36 |
+
page += 1
|
37 |
+
|
38 |
+
reviews_df = pd.json_normalize(all_reviews)
|
39 |
+
return reviews_df
|
40 |
+
|
41 |
+
# Clean
|
42 |
+
def clean(df):
|
43 |
+
df = df.dropna().copy().reset_index(drop=True) # drop reviews with empty comments
|
44 |
+
df = df[df["comment"] != ""].reset_index(drop=True) # remove empty reviews
|
45 |
+
df["comment"] = df["comment"].apply(lambda x: clean_text(x)) # clean text
|
46 |
+
df = df[df["comment"] != ""].reset_index(drop=True) # remove empty reviews
|
47 |
+
return df
|
48 |
+
|
49 |
+
|
50 |
+
def clean_text(text):
|
51 |
+
text = uni.normalize("NFKD", text) # normalise characters
|
52 |
+
text = emoji.replace_emoji(text, "") # remove emoji
|
53 |
+
text = re.sub(r"(\w)\1{2,}", r"\1", text) # repeated chars
|
54 |
+
text = re.sub(r"[ ]+", " ", text).strip() # remove extra spaces
|
55 |
+
return text
|
56 |
+
|
57 |
+
|
58 |
+
# LLM
|
59 |
+
OpenAIModel = "gpt-3.5-turbo"
|
60 |
+
llm = ChatOpenAI(model=OpenAIModel, temperature=0.1)
|
61 |
+
|
62 |
+
# Embeddings
|
63 |
+
embeddings = HuggingFaceEmbeddings(model_name="Blaxzter/LaBSE-sentence-embeddings")
|
64 |
+
|
65 |
+
cache_URL = ""
|
66 |
+
db = None
|
67 |
+
qa = None
|
68 |
+
|
69 |
+
|
70 |
+
def generate(URL, query):
|
71 |
+
global cache_URL, db, qa
|
72 |
+
if URL != cache_URL:
|
73 |
+
# Get reviews
|
74 |
+
try:
|
75 |
+
reviews = scrape(URL)
|
76 |
+
# Clean reviews
|
77 |
+
cleaned_reviews = clean(reviews)
|
78 |
+
# Load data
|
79 |
+
loader = DataFrameLoader(cleaned_reviews, page_content_column="comment")
|
80 |
+
documents = loader.load()
|
81 |
+
except Exception as e:
|
82 |
+
return "Error getting reviews: " + str(e)
|
83 |
+
|
84 |
+
# Split text
|
85 |
+
text_splitter = RecursiveCharacterTextSplitter(
|
86 |
+
chunk_size=1000, chunk_overlap=50
|
87 |
+
)
|
88 |
+
docs = text_splitter.split_documents(documents)
|
89 |
+
cache_URL = URL
|
90 |
+
# Vector store
|
91 |
+
db = FAISS.from_documents(docs, embeddings)
|
92 |
+
# Chain to answer questions
|
93 |
+
qa = RetrievalQA.from_chain_type(llm=llm, retriever=db.as_retriever())
|
94 |
+
return qa.run(query)
|
95 |
+
|
96 |
+
|
97 |
+
# Gradio
|
98 |
+
product_box = gr.Textbox(
|
99 |
+
label="URL Produk", placeholder="URL produk dari Tokopedia"
|
100 |
+
)
|
101 |
+
query_box = gr.Textbox(
|
102 |
+
lines=2,
|
103 |
+
label="Kueri",
|
104 |
+
placeholder="Contoh: Apa yang orang katakan tentang kualitas produknya?, Bagaimana pendapat orang yang kurang puas dengan produknya?",
|
105 |
+
)
|
106 |
+
|
107 |
+
gr.Interface(
|
108 |
+
fn=generate,
|
109 |
+
inputs=[product_box, query_box],
|
110 |
+
outputs=gr.Textbox(label="Jawaban"),
|
111 |
+
title="RingkasUlas",
|
112 |
+
description="Bot percakapan yang bisa meringkas ulasan-ulasan produk di Tokopedia Indonesia (https://tokopedia.com/). Harap bersabar, bot ini dapat memakan waktu agak lama saat mengambil ulasan dari Tokopedia dan menyiapkan jawabannya.",
|
113 |
+
allow_flagging="never",
|
114 |
+
examples=[
|
115 |
+
[
|
116 |
+
"https://www.tokopedia.com/benitashop/telur-asin-powder-madam-kwan-golden-salted-egg-powder",
|
117 |
+
"Berapa lama produknya bisa bertahan?",
|
118 |
+
],
|
119 |
+
[
|
120 |
+
"https://www.tokopedia.com/benitashop/telur-asin-powder-madam-kwan-golden-salted-egg-powder",
|
121 |
+
"Produknya bisa dipakai untuk memasak apa?",
|
122 |
+
],
|
123 |
+
],
|
124 |
+
).launch()
|
|