Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
"""
|
| 2 |
-
FomoFeed - Timing Optimizer AI
|
| 3 |
-
|
| 4 |
"""
|
| 5 |
|
| 6 |
from fastapi import FastAPI, HTTPException
|
|
@@ -10,7 +10,7 @@ from datetime import datetime, timedelta
|
|
| 10 |
from collections import Counter
|
| 11 |
import uvicorn
|
| 12 |
|
| 13 |
-
app = FastAPI(title="FomoFeed Timing Optimizer", version="
|
| 14 |
|
| 15 |
class UserEngagementHistory(BaseModel):
|
| 16 |
user_id: int
|
|
@@ -18,6 +18,8 @@ class UserEngagementHistory(BaseModel):
|
|
| 18 |
engagement_weights: list[float] # Corresponding weights (view=1, like=3, comment=5, save=7)
|
| 19 |
content_type: str = "post" # "post" or "moment"
|
| 20 |
timezone_offset: int = 3 # Turkey = +3
|
|
|
|
|
|
|
| 21 |
|
| 22 |
class TimingRecommendation(BaseModel):
|
| 23 |
optimal_hour: int # 0-23
|
|
@@ -28,6 +30,7 @@ class TimingRecommendation(BaseModel):
|
|
| 28 |
def calculate_optimal_time(history: UserEngagementHistory) -> dict:
|
| 29 |
"""
|
| 30 |
Analyze user's engagement patterns and recommend best posting time
|
|
|
|
| 31 |
"""
|
| 32 |
if not history.engagement_hours or not history.engagement_weights:
|
| 33 |
# No data - return generic best times
|
|
@@ -37,7 +40,9 @@ def calculate_optimal_time(history: UserEngagementHistory) -> dict:
|
|
| 37 |
"alternative_hours": [12, 13, 20, 21],
|
| 38 |
"reasoning": {
|
| 39 |
"method": "default",
|
| 40 |
-
"note": "Using generic peak hours (no user data)"
|
|
|
|
|
|
|
| 41 |
}
|
| 42 |
}
|
| 43 |
|
|
@@ -63,6 +68,35 @@ def calculate_optimal_time(history: UserEngagementHistory) -> dict:
|
|
| 63 |
if hour in time_bonuses:
|
| 64 |
hour_scores[hour] *= time_bonuses[hour]
|
| 65 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
# Add neighboring hour influence (smooth distribution)
|
| 67 |
smoothed_scores = {}
|
| 68 |
for hour in range(24):
|
|
@@ -80,9 +114,17 @@ def calculate_optimal_time(history: UserEngagementHistory) -> dict:
|
|
| 80 |
else:
|
| 81 |
optimal_hour = 19 # Default
|
| 82 |
|
| 83 |
-
# Calculate confidence based on data quality
|
| 84 |
total_engagements = len(history.engagement_hours)
|
| 85 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
|
| 87 |
# Get top 4 alternative hours
|
| 88 |
sorted_hours = sorted(smoothed_scores.items(), key=lambda x: x[1], reverse=True)
|
|
@@ -90,11 +132,15 @@ def calculate_optimal_time(history: UserEngagementHistory) -> dict:
|
|
| 90 |
|
| 91 |
# Reasoning
|
| 92 |
reasoning = {
|
| 93 |
-
"method": "
|
| 94 |
"total_engagements": total_engagements,
|
| 95 |
"unique_hours": len(set(history.engagement_hours)),
|
| 96 |
"peak_score": round(smoothed_scores[optimal_hour], 2),
|
| 97 |
-
"data_quality": "good" if total_engagements > 50 else "moderate" if total_engagements > 20 else "limited"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
}
|
| 99 |
|
| 100 |
return {
|
|
@@ -135,6 +181,14 @@ def calculate_next_optimal_times(history: UserEngagementHistory, count: int = 3)
|
|
| 135 |
time_bonuses = {7: 60, 8: 65, 12: 80, 13: 80, 18: 85, 19: 95, 20: 95, 21: 85, 22: 70}
|
| 136 |
score = time_bonuses.get(future_hour, 40)
|
| 137 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 138 |
opportunities.append({
|
| 139 |
"datetime": future_time.isoformat(),
|
| 140 |
"hour": future_hour,
|
|
@@ -151,7 +205,8 @@ def root():
|
|
| 151 |
return {
|
| 152 |
"service": "FomoFeed Timing Optimizer",
|
| 153 |
"status": "active",
|
| 154 |
-
"version": "
|
|
|
|
| 155 |
}
|
| 156 |
|
| 157 |
@app.get("/health")
|
|
@@ -162,6 +217,7 @@ def health():
|
|
| 162 |
def predict_optimal_time(history: UserEngagementHistory):
|
| 163 |
"""
|
| 164 |
Predict optimal posting time based on user's engagement history
|
|
|
|
| 165 |
"""
|
| 166 |
try:
|
| 167 |
result = calculate_optimal_time(history)
|
|
|
|
| 1 |
"""
|
| 2 |
+
FomoFeed - Timing Optimizer AI v2
|
| 3 |
+
WITH CLIP SCORE & VIDEO BOOST
|
| 4 |
"""
|
| 5 |
|
| 6 |
from fastapi import FastAPI, HTTPException
|
|
|
|
| 10 |
from collections import Counter
|
| 11 |
import uvicorn
|
| 12 |
|
| 13 |
+
app = FastAPI(title="FomoFeed Timing Optimizer", version="2.0.0")
|
| 14 |
|
| 15 |
class UserEngagementHistory(BaseModel):
|
| 16 |
user_id: int
|
|
|
|
| 18 |
engagement_weights: list[float] # Corresponding weights (view=1, like=3, comment=5, save=7)
|
| 19 |
content_type: str = "post" # "post" or "moment"
|
| 20 |
timezone_offset: int = 3 # Turkey = +3
|
| 21 |
+
clip_score: float = 0.0 # 🆕 CLIP visual quality score (0-10)
|
| 22 |
+
has_video: bool = False # 🆕 Is this video content?
|
| 23 |
|
| 24 |
class TimingRecommendation(BaseModel):
|
| 25 |
optimal_hour: int # 0-23
|
|
|
|
| 30 |
def calculate_optimal_time(history: UserEngagementHistory) -> dict:
|
| 31 |
"""
|
| 32 |
Analyze user's engagement patterns and recommend best posting time
|
| 33 |
+
WITH CLIP SCORE & VIDEO BOOST
|
| 34 |
"""
|
| 35 |
if not history.engagement_hours or not history.engagement_weights:
|
| 36 |
# No data - return generic best times
|
|
|
|
| 40 |
"alternative_hours": [12, 13, 20, 21],
|
| 41 |
"reasoning": {
|
| 42 |
"method": "default",
|
| 43 |
+
"note": "Using generic peak hours (no user data)",
|
| 44 |
+
"clip_boost": False,
|
| 45 |
+
"video_boost": False
|
| 46 |
}
|
| 47 |
}
|
| 48 |
|
|
|
|
| 68 |
if hour in time_bonuses:
|
| 69 |
hour_scores[hour] *= time_bonuses[hour]
|
| 70 |
|
| 71 |
+
# 🆕 CLIP SCORE BOOST
|
| 72 |
+
clip_boost_applied = False
|
| 73 |
+
if history.clip_score > 0:
|
| 74 |
+
# Yüksek kaliteli görseller prime-time'da daha iyi performans gösterir
|
| 75 |
+
prime_hours = [12, 13, 18, 19, 20, 21]
|
| 76 |
+
clip_multiplier = 1 + (history.clip_score / 10) * 0.3 # Max %30 boost
|
| 77 |
+
|
| 78 |
+
for hour in prime_hours:
|
| 79 |
+
if hour in hour_scores:
|
| 80 |
+
hour_scores[hour] *= clip_multiplier
|
| 81 |
+
clip_boost_applied = True
|
| 82 |
+
else:
|
| 83 |
+
# Hiç veri yoksa CLIP skoruna göre başlangıç puanı ver
|
| 84 |
+
hour_scores[hour] = history.clip_score * 5 * time_bonuses.get(hour, 1.0)
|
| 85 |
+
clip_boost_applied = True
|
| 86 |
+
|
| 87 |
+
# 🆕 VIDEO BOOST
|
| 88 |
+
video_boost_applied = False
|
| 89 |
+
if history.has_video:
|
| 90 |
+
# Videolar akşam saatlerinde daha iyi performans gösterir
|
| 91 |
+
evening_hours = [18, 19, 20, 21, 22]
|
| 92 |
+
for hour in evening_hours:
|
| 93 |
+
if hour in hour_scores:
|
| 94 |
+
hour_scores[hour] *= 1.2 # %20 video boost
|
| 95 |
+
video_boost_applied = True
|
| 96 |
+
else:
|
| 97 |
+
hour_scores[hour] = 50 * time_bonuses.get(hour, 1.0)
|
| 98 |
+
video_boost_applied = True
|
| 99 |
+
|
| 100 |
# Add neighboring hour influence (smooth distribution)
|
| 101 |
smoothed_scores = {}
|
| 102 |
for hour in range(24):
|
|
|
|
| 114 |
else:
|
| 115 |
optimal_hour = 19 # Default
|
| 116 |
|
| 117 |
+
# Calculate confidence based on data quality + boosts
|
| 118 |
total_engagements = len(history.engagement_hours)
|
| 119 |
+
base_confidence = min(0.95, 0.5 + (total_engagements / 200))
|
| 120 |
+
|
| 121 |
+
# Boost confidence if CLIP or video boosts were applied
|
| 122 |
+
if clip_boost_applied and history.clip_score >= 7:
|
| 123 |
+
base_confidence = min(0.95, base_confidence + 0.1)
|
| 124 |
+
if video_boost_applied:
|
| 125 |
+
base_confidence = min(0.95, base_confidence + 0.05)
|
| 126 |
+
|
| 127 |
+
confidence = base_confidence
|
| 128 |
|
| 129 |
# Get top 4 alternative hours
|
| 130 |
sorted_hours = sorted(smoothed_scores.items(), key=lambda x: x[1], reverse=True)
|
|
|
|
| 132 |
|
| 133 |
# Reasoning
|
| 134 |
reasoning = {
|
| 135 |
+
"method": "weighted_pattern_analysis_v2",
|
| 136 |
"total_engagements": total_engagements,
|
| 137 |
"unique_hours": len(set(history.engagement_hours)),
|
| 138 |
"peak_score": round(smoothed_scores[optimal_hour], 2),
|
| 139 |
+
"data_quality": "good" if total_engagements > 50 else "moderate" if total_engagements > 20 else "limited",
|
| 140 |
+
"clip_boost": clip_boost_applied,
|
| 141 |
+
"clip_score": history.clip_score,
|
| 142 |
+
"video_boost": video_boost_applied,
|
| 143 |
+
"prime_time": optimal_hour in [12, 13, 18, 19, 20, 21]
|
| 144 |
}
|
| 145 |
|
| 146 |
return {
|
|
|
|
| 181 |
time_bonuses = {7: 60, 8: 65, 12: 80, 13: 80, 18: 85, 19: 95, 20: 95, 21: 85, 22: 70}
|
| 182 |
score = time_bonuses.get(future_hour, 40)
|
| 183 |
|
| 184 |
+
# Boost if CLIP score is high and it's prime time
|
| 185 |
+
if history.clip_score >= 7 and future_hour in [12, 13, 18, 19, 20, 21]:
|
| 186 |
+
score = min(100, score + 10)
|
| 187 |
+
|
| 188 |
+
# Boost if video and evening
|
| 189 |
+
if history.has_video and future_hour in [18, 19, 20, 21, 22]:
|
| 190 |
+
score = min(100, score + 5)
|
| 191 |
+
|
| 192 |
opportunities.append({
|
| 193 |
"datetime": future_time.isoformat(),
|
| 194 |
"hour": future_hour,
|
|
|
|
| 205 |
return {
|
| 206 |
"service": "FomoFeed Timing Optimizer",
|
| 207 |
"status": "active",
|
| 208 |
+
"version": "2.0.0",
|
| 209 |
+
"features": ["clip_boost", "video_boost", "prime_time_optimization"]
|
| 210 |
}
|
| 211 |
|
| 212 |
@app.get("/health")
|
|
|
|
| 217 |
def predict_optimal_time(history: UserEngagementHistory):
|
| 218 |
"""
|
| 219 |
Predict optimal posting time based on user's engagement history
|
| 220 |
+
WITH CLIP SCORE & VIDEO BOOST
|
| 221 |
"""
|
| 222 |
try:
|
| 223 |
result = calculate_optimal_time(history)
|