yigitcanozdemir
commited on
Commit
·
20ce711
1
Parent(s):
58b5538
Commit fix
Browse files- .gitignore +1 -2
- .gradio/certificate.pem +0 -31
- models/embedding_model.py +13 -0
- models/pydantic_schemas.py +27 -0
- models/recommendation_engine.py +120 -0
.gitignore
CHANGED
|
@@ -107,7 +107,7 @@ ipython_config.py
|
|
| 107 |
|
| 108 |
# PEP 582
|
| 109 |
__pypackages__/
|
| 110 |
-
|
| 111 |
# Celery stuff
|
| 112 |
celerybeat-schedule
|
| 113 |
celerybeat.pid
|
|
@@ -160,5 +160,4 @@ cython_debug/
|
|
| 160 |
*.rdb
|
| 161 |
|
| 162 |
# Exclude trained models
|
| 163 |
-
/models/
|
| 164 |
=*.0*
|
|
|
|
| 107 |
|
| 108 |
# PEP 582
|
| 109 |
__pypackages__/
|
| 110 |
+
.gradio/
|
| 111 |
# Celery stuff
|
| 112 |
celerybeat-schedule
|
| 113 |
celerybeat.pid
|
|
|
|
| 160 |
*.rdb
|
| 161 |
|
| 162 |
# Exclude trained models
|
|
|
|
| 163 |
=*.0*
|
.gradio/certificate.pem
DELETED
|
@@ -1,31 +0,0 @@
|
|
| 1 |
-
-----BEGIN CERTIFICATE-----
|
| 2 |
-
MIIFazCCA1OgAwIBAgIRAIIQz7DSQONZRGPgu2OCiwAwDQYJKoZIhvcNAQELBQAw
|
| 3 |
-
TzELMAkGA1UEBhMCVVMxKTAnBgNVBAoTIEludGVybmV0IFNlY3VyaXR5IFJlc2Vh
|
| 4 |
-
cmNoIEdyb3VwMRUwEwYDVQQDEwxJU1JHIFJvb3QgWDEwHhcNMTUwNjA0MTEwNDM4
|
| 5 |
-
WhcNMzUwNjA0MTEwNDM4WjBPMQswCQYDVQQGEwJVUzEpMCcGA1UEChMgSW50ZXJu
|
| 6 |
-
ZXQgU2VjdXJpdHkgUmVzZWFyY2ggR3JvdXAxFTATBgNVBAMTDElTUkcgUm9vdCBY
|
| 7 |
-
MTCCAiIwDQYJKoZIhvcNAQEBBQADggIPADCCAgo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ransformer
|
| 2 |
+
from config import Config
|
| 3 |
+
|
| 4 |
+
|
| 5 |
+
class EmbeddingModel:
|
| 6 |
+
def __init__(self):
|
| 7 |
+
self.config = Config()
|
| 8 |
+
self.model = SentenceTransformer(
|
| 9 |
+
self.config.EMBEDDING_MODEL, trust_remote_code=True
|
| 10 |
+
)
|
| 11 |
+
|
| 12 |
+
def encode(self, texts):
|
| 13 |
+
return self.model.encode(texts)
|
models/pydantic_schemas.py
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from pydantic import BaseModel, Field
|
| 2 |
+
from typing import Literal, Optional
|
| 3 |
+
from config import GENRE_LIST
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
class Features(BaseModel):
|
| 7 |
+
movie_or_series: Literal["movie", "tvSeries"] = Field(
|
| 8 |
+
description="Specify if the user wants a movie or a TV series"
|
| 9 |
+
)
|
| 10 |
+
genres: list[GENRE_LIST] = Field(
|
| 11 |
+
description="List of genres from the predefined list"
|
| 12 |
+
)
|
| 13 |
+
quality_level: str = Field(
|
| 14 |
+
description="Quality expectation: legendary, classic, popular, any"
|
| 15 |
+
)
|
| 16 |
+
themes: list[str] = Field(
|
| 17 |
+
description="Actual thematic content (not quality descriptors)"
|
| 18 |
+
)
|
| 19 |
+
date_range: list[int] = Field(description="Date range [min_year, max_year]")
|
| 20 |
+
negative_keywords: list[str] = Field(description="List of negative keywords")
|
| 21 |
+
production_region: list[str] = Field(description="Production region")
|
| 22 |
+
min_rating: Optional[float] = Field(
|
| 23 |
+
description="Minimum rating expectation", default=None
|
| 24 |
+
)
|
| 25 |
+
min_votes: Optional[int] = Field(
|
| 26 |
+
description="Minimum number of votes", default=None
|
| 27 |
+
)
|
models/recommendation_engine.py
ADDED
|
@@ -0,0 +1,120 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pandas as pd
|
| 2 |
+
import time
|
| 3 |
+
from openai import OpenAI
|
| 4 |
+
from config import Config
|
| 5 |
+
from models.pydantic_schemas import Features
|
| 6 |
+
from components.similarity import SimilarityCalculator
|
| 7 |
+
from components.filters import MovieFilter
|
| 8 |
+
from sentence_transformers import SentenceTransformer
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
class RecommendationEngine:
|
| 12 |
+
def __init__(self):
|
| 13 |
+
self.config = Config()
|
| 14 |
+
self.model = SentenceTransformer(
|
| 15 |
+
self.config.EMBEDDING_MODEL, trust_remote_code=True
|
| 16 |
+
)
|
| 17 |
+
self.client = OpenAI(api_key=self.config.OPENAI_API_KEY)
|
| 18 |
+
self.data = pd.read_parquet(self.config.DATA_FILE)
|
| 19 |
+
|
| 20 |
+
self.similarity_calc = SimilarityCalculator(self.model)
|
| 21 |
+
self.filter = MovieFilter()
|
| 22 |
+
|
| 23 |
+
print(f"✅ Recommendation engine initialized with {len(self.data)} items.")
|
| 24 |
+
|
| 25 |
+
def get_recommendations(self, user_query: str, top_k: int = 10):
|
| 26 |
+
|
| 27 |
+
if not user_query.strip():
|
| 28 |
+
return "⚠️ Please enter some text.", None
|
| 29 |
+
|
| 30 |
+
try:
|
| 31 |
+
features = self._parse_user_query(user_query)
|
| 32 |
+
|
| 33 |
+
filtered_data = self.filter.apply_filters(self.data, features)
|
| 34 |
+
|
| 35 |
+
search_results = self.similarity_calc.calculate_similarity(
|
| 36 |
+
user_query, filtered_data, top_k
|
| 37 |
+
)
|
| 38 |
+
|
| 39 |
+
formatted_results = self._format_results(search_results)
|
| 40 |
+
|
| 41 |
+
return formatted_results, self._create_results_dataframe(search_results)
|
| 42 |
+
|
| 43 |
+
except Exception as e:
|
| 44 |
+
return f"❌ Error: {str(e)}", None
|
| 45 |
+
|
| 46 |
+
def _parse_user_query(self, query: str) -> Features:
|
| 47 |
+
"""GPT ile kullanıcı sorgusu parse et"""
|
| 48 |
+
try:
|
| 49 |
+
response = self.client.beta.chat.completions.parse(
|
| 50 |
+
model="gpt-4o-mini",
|
| 51 |
+
messages=[
|
| 52 |
+
{
|
| 53 |
+
"role": "system",
|
| 54 |
+
"content": "You are an AI that converts user requests into structured movie/TV-series features. Be smart about interpreting user preferences.",
|
| 55 |
+
},
|
| 56 |
+
{"role": "user", "content": query},
|
| 57 |
+
],
|
| 58 |
+
response_format=Features,
|
| 59 |
+
)
|
| 60 |
+
return response.choices[0].message.parsed
|
| 61 |
+
except Exception as e:
|
| 62 |
+
return Features(
|
| 63 |
+
movie_or_series="both",
|
| 64 |
+
genres=[],
|
| 65 |
+
quality_level="any",
|
| 66 |
+
themes=[],
|
| 67 |
+
date_range=[2000, 2025],
|
| 68 |
+
negative_keywords=[],
|
| 69 |
+
production_region=[],
|
| 70 |
+
)
|
| 71 |
+
|
| 72 |
+
def _format_results(self, search_results: dict) -> str:
|
| 73 |
+
if not search_results["results"]:
|
| 74 |
+
return search_results["status"]
|
| 75 |
+
|
| 76 |
+
output = []
|
| 77 |
+
output.append(f"🎬 {search_results['status']}")
|
| 78 |
+
output.append(
|
| 79 |
+
f"🔍 Search completed in {search_results['search_time']:.4f} seconds"
|
| 80 |
+
)
|
| 81 |
+
output.append(
|
| 82 |
+
f"📊 Found {len(search_results['results'])} results from {search_results['total_candidates']} candidates"
|
| 83 |
+
)
|
| 84 |
+
output.append("=" * 50)
|
| 85 |
+
|
| 86 |
+
for i, result in enumerate(search_results["results"], 1):
|
| 87 |
+
output.append(f"{i}. **{result['title']}** ({result['year']})")
|
| 88 |
+
output.append(f" 📝 Type: {result['type'].title()}")
|
| 89 |
+
output.append(
|
| 90 |
+
f" ⭐ Rating: {result['rating']}/10 ({result['votes']:,} votes)"
|
| 91 |
+
)
|
| 92 |
+
output.append(f" 🎭 Genres: {result['genres']}")
|
| 93 |
+
output.append(f" 📊 Similarity: {result['similarity_score']:.4f}")
|
| 94 |
+
output.append(f" 🏆 Hybrid Score: {result['hybrid_score']:.4f}")
|
| 95 |
+
output.append(f" 📄 {result['overview']}")
|
| 96 |
+
output.append("")
|
| 97 |
+
|
| 98 |
+
return "\n".join(output)
|
| 99 |
+
|
| 100 |
+
def _create_results_dataframe(self, search_results: dict) -> pd.DataFrame:
|
| 101 |
+
if not search_results["results"]:
|
| 102 |
+
return pd.DataFrame()
|
| 103 |
+
|
| 104 |
+
df_data = []
|
| 105 |
+
for result in search_results["results"]:
|
| 106 |
+
df_data.append(
|
| 107 |
+
{
|
| 108 |
+
"Title": result["title"],
|
| 109 |
+
"Type": result["type"],
|
| 110 |
+
"Year": result["year"],
|
| 111 |
+
"Rating": result["rating"],
|
| 112 |
+
"Votes": result["votes"],
|
| 113 |
+
"Genres": result["genres"],
|
| 114 |
+
"Similarity": f"{result['similarity_score']:.4f}",
|
| 115 |
+
"Hybrid Score": f"{result['hybrid_score']:.4f}",
|
| 116 |
+
"Overview": result["overview"],
|
| 117 |
+
}
|
| 118 |
+
)
|
| 119 |
+
|
| 120 |
+
return pd.DataFrame(df_data)
|