Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
P
product_recommendation_chatbot
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Bhargava Rellu
product_recommendation_chatbot
Commits
35b8bac9
Commit
35b8bac9
authored
Jan 24, 2025
by
BRellu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
refactor
parent
91c75467
Changes
13
Hide whitespace changes
Inline
Side-by-side
Showing
13 changed files
with
118 additions
and
131 deletions
+118
-131
routes.cpython-310.pyc
src/reviewsense/api/__pycache__/routes.cpython-310.pyc
+0
-0
routes.py
src/reviewsense/api/routes.py
+1
-1
ReviewService.py
src/reviewsense/service/ReviewService.py
+0
-130
ReviewService.cpython-310.pyc
...ewsense/service/__pycache__/ReviewService.cpython-310.pyc
+0
-0
ReviewService.py
src/reviewsense/services/ReviewService.py
+26
-0
ReviewService.cpython-310.pyc
...wsense/services/__pycache__/ReviewService.cpython-310.pyc
+0
-0
review_adder.cpython-310.pyc
...ewsense/services/__pycache__/review_adder.cpython-310.pyc
+0
-0
review_fetcher.cpython-310.pyc
...sense/services/__pycache__/review_fetcher.cpython-310.pyc
+0
-0
review_rater.cpython-310.pyc
...ewsense/services/__pycache__/review_rater.cpython-310.pyc
+0
-0
review_service.cpython-310.pyc
...sense/services/__pycache__/review_service.cpython-310.pyc
+0
-0
review_adder.py
src/reviewsense/services/review_adder.py
+24
-0
review_fetcher.py
src/reviewsense/services/review_fetcher.py
+32
-0
review_rater.py
src/reviewsense/services/review_rater.py
+35
-0
No files found.
src/reviewsense/api/__pycache__/routes.cpython-310.pyc
View file @
35b8bac9
No preview for this file type
src/reviewsense/api/routes.py
View file @
35b8bac9
from
fastapi
import
APIRouter
from
typing
import
Dict
,
Optional
from
reviewsense.api.schemas.product_review_input
import
ProductReviewInput
from
reviewsense.service.ReviewService
import
ReviewService
from
reviewsense.service
s
.ReviewService
import
ReviewService
router
=
APIRouter
()
...
...
src/reviewsense/service/ReviewService.py
deleted
100644 → 0
View file @
91c75467
import
os
import
numpy
as
np
from
typing
import
List
,
Dict
,
Optional
from
langchain_community.docstore.document
import
Document
from
langchain_astradb
import
AstraDBVectorStore
from
langchain_huggingface
import
HuggingFaceEmbeddings
from
transformers
import
pipeline
from
reviewsense.core.config
import
get_settings
__all__
=
[
'ReviewService'
]
class
ReviewService
:
"""Service class for handling product reviews"""
def
__init__
(
self
,
collection_name
:
str
=
"android_phone_reviews"
,
embedding_model
:
str
=
"sentence-transformers/all-mpnet-base-v2"
):
"""
Initialize the review service with vector database and embeddings
Args:
collection_name (str): Name of the vector database collection
embedding_model (str): Hugging Face embedding model to use
"""
settings
=
get_settings
()
# Initialize embeddings
self
.
embeddings
=
HuggingFaceEmbeddings
(
model_name
=
embedding_model
)
# Initialize the vector store
self
.
vector_store
=
AstraDBVectorStore
(
collection_name
=
collection_name
,
embedding
=
self
.
embeddings
,
api_endpoint
=
settings
.
ASTRA_DB_API_ENDPOINT
,
token
=
settings
.
ASTRA_DB_APPLICATION_TOKEN
,
namespace
=
settings
.
ASTRA_DB_NAMESPACE
,
)
# Initialize sentiment analyzer
self
.
sentiment_analyzer
=
pipeline
(
"sentiment-analysis"
)
# [Rest of the methods remain the same as in the original implementation]
def
fetch_reviews
(
self
,
product_id
:
str
,
features
:
List
[
str
],
threshold
:
float
=
0.6
)
->
Dict
[
str
,
List
[
str
]]:
"""
Fetch reviews for specific product features
Args:
product_id (str): Identifier for the product
features (List[str]): Features to extract reviews for
threshold (float): Similarity threshold for review filtering
Returns:
Dict containing reviews for each feature
"""
feature_reviews
=
{}
for
feature
in
features
:
# Perform similarity search for the current feature
filter_criteria
=
{
"title"
:
product_id
}
documents
=
self
.
vector_store
.
similarity_search_with_score_id
(
query
=
feature
,
k
=
100
,
filter
=
filter_criteria
)
# Filter and collect reviews
filtered_reviews
=
[
doc
.
page_content
for
doc
,
score
,
_
in
documents
if
score
>
threshold
]
# Add the list of reviews to the result dictionary
feature_reviews
[
feature
]
=
filtered_reviews
return
feature_reviews
def
generate_feature_ratings
(
self
,
reviews_by_feature
:
Dict
[
str
,
List
[
str
]])
->
Dict
[
str
,
Optional
[
float
]]:
"""
Generate ratings for each feature based on sentiment analysis
Args:
reviews_by_feature (Dict): Dictionary of reviews grouped by feature
Returns:
Dictionary of feature ratings
"""
feature_ratings
=
{}
for
feature
,
reviews
in
reviews_by_feature
.
items
():
if
not
reviews
:
feature_ratings
[
feature
]
=
None
continue
# Analyze sentiment for each review
ratings
=
[]
for
review
in
reviews
:
sentiment
=
self
.
sentiment_analyzer
(
review
)[
0
]
# Map sentiment to numerical score
if
"positive"
in
sentiment
[
"label"
]
.
lower
():
ratings
.
append
(
5
)
elif
"negative"
in
sentiment
[
"label"
]
.
lower
():
ratings
.
append
(
1
)
else
:
# Neutral sentiment
ratings
.
append
(
3
)
# Compute average rating
feature_ratings
[
feature
]
=
round
(
np
.
mean
(
ratings
),
1
)
if
ratings
else
None
return
feature_ratings
def
add_review
(
self
,
product_id
:
str
,
review
:
str
)
->
str
:
"""
Add a new review to the vector database
Args:
product_id (str): Identifier for the product
review (str): Review text to add
Returns:
ID of the added document
"""
# Create a new document with the review
review_document
=
Document
(
page_content
=
review
,
metadata
=
{
"title"
:
product_id
}
)
# Add document to vector store
return
self
.
vector_store
.
add_documents
([
review_document
])[
0
]
src/reviewsense/service/__pycache__/ReviewService.cpython-310.pyc
deleted
100644 → 0
View file @
91c75467
File deleted
src/reviewsense/services/ReviewService.py
0 → 100644
View file @
35b8bac9
# services/review_service.py
from
typing
import
List
,
Dict
,
Optional
from
reviewsense.services.review_fetcher
import
ReviewFetcher
from
reviewsense.services.review_rater
import
ReviewRater
from
reviewsense.services.review_adder
import
ReviewAdder
class
ReviewService
:
"""Service class for handling product reviews"""
def
__init__
(
self
,
collection_name
:
str
=
"android_phone_reviews"
,
embedding_model
:
str
=
"sentence-transformers/all-mpnet-base-v2"
):
"""
Initialize the review service with vector database and embeddings
"""
self
.
fetcher
=
ReviewFetcher
(
collection_name
,
embedding_model
)
self
.
rater
=
ReviewRater
()
self
.
adder
=
ReviewAdder
(
collection_name
,
embedding_model
)
def
fetch_reviews
(
self
,
product_id
:
str
,
features
:
List
[
str
],
threshold
:
float
=
0.6
)
->
Dict
[
str
,
List
[
str
]]:
return
self
.
fetcher
.
fetch_reviews
(
product_id
,
features
,
threshold
)
def
generate_feature_ratings
(
self
,
reviews_by_feature
:
Dict
[
str
,
List
[
str
]])
->
Dict
[
str
,
Optional
[
float
]]:
return
self
.
rater
.
generate_feature_ratings
(
reviews_by_feature
)
def
add_review
(
self
,
product_id
:
str
,
review
:
str
)
->
str
:
return
self
.
adder
.
add_review
(
product_id
,
review
)
src/reviewsense/services/__pycache__/ReviewService.cpython-310.pyc
0 → 100644
View file @
35b8bac9
File added
src/reviewsense/services/__pycache__/review_adder.cpython-310.pyc
0 → 100644
View file @
35b8bac9
File added
src/reviewsense/services/__pycache__/review_fetcher.cpython-310.pyc
0 → 100644
View file @
35b8bac9
File added
src/reviewsense/services/__pycache__/review_rater.cpython-310.pyc
0 → 100644
View file @
35b8bac9
File added
src/reviewsense/services/__pycache__/review_service.cpython-310.pyc
0 → 100644
View file @
35b8bac9
File added
src/reviewsense/services/review_adder.py
0 → 100644
View file @
35b8bac9
# services/review_adder.py
from
langchain_community.docstore.document
import
Document
from
reviewsense.core.config
import
get_settings
from
langchain_huggingface
import
HuggingFaceEmbeddings
from
langchain_astradb
import
AstraDBVectorStore
class
ReviewAdder
:
"""Class for adding reviews to the vector store"""
def
__init__
(
self
,
collection_name
:
str
,
embedding_model
:
str
):
settings
=
get_settings
()
self
.
embeddings
=
HuggingFaceEmbeddings
(
model_name
=
embedding_model
)
self
.
vector_store
=
AstraDBVectorStore
(
collection_name
=
collection_name
,
embedding
=
self
.
embeddings
,
api_endpoint
=
settings
.
ASTRA_DB_API_ENDPOINT
,
token
=
settings
.
ASTRA_DB_APPLICATION_TOKEN
,
namespace
=
settings
.
ASTRA_DB_NAMESPACE
,
)
def
add_review
(
self
,
product_id
:
str
,
review
:
str
)
->
str
:
review_document
=
Document
(
page_content
=
review
,
metadata
=
{
"title"
:
product_id
})
return
self
.
vector_store
.
add_documents
([
review_document
])[
0
]
src/reviewsense/services/review_fetcher.py
0 → 100644
View file @
35b8bac9
# services/review_fetcher.py
from
typing
import
List
,
Dict
from
reviewsense.core.config
import
get_settings
from
langchain_astradb
import
AstraDBVectorStore
from
langchain_huggingface
import
HuggingFaceEmbeddings
class
ReviewFetcher
:
"""Class for fetching reviews from the vector store"""
def
__init__
(
self
,
collection_name
:
str
,
embedding_model
:
str
):
settings
=
get_settings
()
self
.
embeddings
=
HuggingFaceEmbeddings
(
model_name
=
embedding_model
)
self
.
vector_store
=
AstraDBVectorStore
(
collection_name
=
collection_name
,
embedding
=
self
.
embeddings
,
api_endpoint
=
settings
.
ASTRA_DB_API_ENDPOINT
,
token
=
settings
.
ASTRA_DB_APPLICATION_TOKEN
,
namespace
=
settings
.
ASTRA_DB_NAMESPACE
,
)
def
fetch_reviews
(
self
,
product_id
:
str
,
features
:
List
[
str
],
threshold
:
float
=
0.6
)
->
Dict
[
str
,
List
[
str
]]:
feature_reviews
=
{}
for
feature
in
features
:
filter_criteria
=
{
"title"
:
product_id
}
documents
=
self
.
vector_store
.
similarity_search_with_score_id
(
query
=
feature
,
k
=
100
,
filter
=
filter_criteria
)
filtered_reviews
=
[
doc
.
page_content
for
doc
,
score
,
_
in
documents
if
score
>
threshold
]
feature_reviews
[
feature
]
=
filtered_reviews
return
feature_reviews
src/reviewsense/services/review_rater.py
0 → 100644
View file @
35b8bac9
# services/review_rater.py
from
typing
import
Dict
,
List
,
Optional
import
numpy
as
np
from
langchain_huggingface
import
HuggingFaceEmbeddings
from
transformers
import
pipeline
class
ReviewRater
:
"""Class for generating feature-specific ratings based on sentiment analysis"""
def
__init__
(
self
):
self
.
sentiment_analyzer
=
pipeline
(
"sentiment-analysis"
)
def
generate_feature_ratings
(
self
,
reviews_by_feature
:
Dict
[
str
,
List
[
str
]])
->
Dict
[
str
,
Optional
[
float
]]:
feature_ratings
=
{}
for
feature
,
reviews
in
reviews_by_feature
.
items
():
if
not
reviews
:
feature_ratings
[
feature
]
=
None
continue
ratings
=
[]
for
review
in
reviews
:
sentiment
=
self
.
sentiment_analyzer
(
review
)[
0
]
if
"positive"
in
sentiment
[
"label"
]
.
lower
():
ratings
.
append
(
5
)
elif
"negative"
in
sentiment
[
"label"
]
.
lower
():
ratings
.
append
(
1
)
else
:
ratings
.
append
(
3
)
feature_ratings
[
feature
]
=
round
(
np
.
mean
(
ratings
),
1
)
if
ratings
else
None
return
feature_ratings
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment