Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions .github/workflows/build-deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,21 @@ jobs:
--health-timeout 5s
--health-retries 5

runs-on: ubuntu-latest
steps:
- name: pulling git repo
uses: actions/checkout@v2
runs-on: ubuntu-22.04-large
steps:
- name: Reclaim disk space on runner
shell: bash
run: |
echo "==> Removing unused SDKs and cached archives to free space"
sudo rm -rf /usr/share/dotnet
sudo rm -rf /usr/local/lib/android
sudo rm -rf /opt/ghc
sudo rm -rf /usr/local/share/boost
sudo rm -rf /home/linuxbrew/.linuxbrew
docker system prune --all --force --volumes || true
df -h
- name: pulling git repo
uses: actions/checkout@v2

- name: Create RSA private key file
run: echo "${{ secrets.RSA_PRIVATE_KEY }}" > private_key.pem
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ public_key
private_key
env.
venv
test_app.db
80 changes: 50 additions & 30 deletions app/analytics.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,42 @@
from sqlalchemy import func
from datetime import (
datetime,
timedelta,
timezone,
) # Added timezone for correct UTC usage
from . import models
from transformers import pipeline, AutoTokenizer, AutoModelForSequenceClassification
import torch
from .config import settings
import matplotlib.pyplot as plt
import seaborn as sns
import io
import base64
from .models import SearchStatistics, User
from sqlalchemy.orm import Session

# NOTE: Ensure that get_db() is defined in your project or import it accordingly.
# from .database import get_db

# Initialize the tokenizer and model for sentiment analysis
tokenizer = AutoTokenizer.from_pretrained(
"distilbert-base-uncased-finetuned-sst-2-english"
)
model = AutoModelForSequenceClassification.from_pretrained(
"distilbert-base-uncased-finetuned-sst-2-english"
)
sentiment_pipeline = pipeline("sentiment-analysis", model=model, tokenizer=tokenizer)
from sqlalchemy import func
from datetime import (
datetime,
timedelta,
timezone,
) # Added timezone for correct UTC usage
import logging
from . import models
from transformers import pipeline, AutoTokenizer, AutoModelForSequenceClassification
from .config import settings
import matplotlib.pyplot as plt
import seaborn as sns
import io
import base64
from .models import SearchStatistics, User
from sqlalchemy.orm import Session
from .utils import keyword_sentiment

# NOTE: Ensure that get_db() is defined in your project or import it accordingly.
# from .database import get_db

logger = logging.getLogger(__name__)

# Initialize the tokenizer and model for sentiment analysis with graceful fallback
try:
tokenizer = AutoTokenizer.from_pretrained(
"distilbert-base-uncased-finetuned-sst-2-english"
)
model = AutoModelForSequenceClassification.from_pretrained(
"distilbert-base-uncased-finetuned-sst-2-english"
)
sentiment_pipeline = pipeline("sentiment-analysis", model=model, tokenizer=tokenizer)
except Exception as exc: # pragma: no cover - defensive fallback
logger.warning(
"Analytics sentiment pipeline unavailable, using keyword heuristic: %s",
exc,
)
tokenizer = model = None
sentiment_pipeline = None

# ------------------------- Content Analysis Functions -------------------------

Expand All @@ -35,8 +46,17 @@ def analyze_sentiment(text):
Analyze the sentiment of the given text using a pre-trained transformer model.
Returns a dictionary with sentiment label and score.
"""
result = sentiment_pipeline(text)[0]
return {"sentiment": result["label"], "score": result["score"]}
if sentiment_pipeline is not None:
try:
result = sentiment_pipeline(text)[0]
return {"sentiment": result["label"], "score": float(result["score"])}
except Exception as exc: # pragma: no cover - defensive fallback
logger.warning(
"Analytics sentiment pipeline failed, using keyword heuristic: %s",
exc,
)
label, score = keyword_sentiment(text)
return {"sentiment": label, "score": score}


def suggest_improvements(text, sentiment):
Expand Down
Loading
Loading