File size: 2,551 Bytes
b490ee7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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
"""Prediction logic and output formatting."""

from __future__ import annotations

from typing import Any

from src.preprocess import predict_revenue


INPUT_ORDER = [
    "budget",
    "popularity",
    "runtime",
    "release_date",
    "original_language",
    "belongs_to_collection",
    "homepage",
    "title",
    "tagline",
    "overview",
    "num_of_cast",
    "num_of_crew",
    "gender_cast_1",
    "gender_cast_2",
    "count_cast_other",
    "genres",
    "production_companies",
    "keywords",
    "cast",
]


def format_currency(value: float) -> str:
    """Format a number as currency."""
    if value >= 1_000_000_000:
        return f"${value / 1_000_000_000:.2f}B"
    elif value >= 1_000_000:
        return f"${value / 1_000_000:.2f}M"
    else:
        return f"${value:,.0f}"


def predict_revenue_from_form(model: Any, *values: Any) -> tuple[str, str]:
    """
    Predict revenue from form inputs and return formatted results.
    
    Returns:
        Tuple of (prediction_text, profitability_text)
    """
    if model is None:
        return "❌ Model not available", ""
    
    # Build payload from form values
    payload = dict(zip(INPUT_ORDER, values))
    payload["belongs_to_collection"] = int(bool(payload.get("belongs_to_collection")))
    payload["homepage"] = int(bool(payload.get("homepage")))
    payload["has_tagline"] = 1 if str(payload.get("tagline") or "").strip() else 0
    
    try:
        prediction = predict_revenue(model, payload)
        budget = float(payload.get("budget") or 0.0)
        
        # Format prediction
        prediction_text = f"## 💰 Predicted Revenue\n### {format_currency(prediction)}"
        
        # Calculate ROI
        if budget > 0:
            roi = (prediction - budget) / budget * 100
            multiple = prediction / budget
            
            if roi > 100:
                status = "🟢 **Highly Profitable**"
            elif roi > 0:
                status = "🟡 **Profitable**"
            else:
                status = "🔴 **Loss Expected**"
            
            profitability_text = f"""
{status}

- **Budget:** {format_currency(budget)}
- **Revenue Multiple:** {multiple:.2f}x
- **ROI:** {roi:+.1f}%
- **Estimated Profit:** {format_currency(prediction - budget)}
"""
        else:
            profitability_text = "ℹ️ Enter a budget to see profitability analysis"
        
        return prediction_text, profitability_text
        
    except Exception as exc:
        return f"❌ Prediction Error\n```\n{str(exc)}\n```", ""