File size: 3,127 Bytes
82c705b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
94
import gradio as gr
import sys
import os

# Add the current directory to Python path
sys.path.append(os.path.dirname(os.path.abspath(__file__)))

# Import your advanced model system
try:
    from app.advanced_model import predict_advanced, get_advanced_analyzer
    ADVANCED_AVAILABLE = True
except ImportError:
    # Fallback to basic model if advanced isn't available
    from app.model import predict
    ADVANCED_AVAILABLE = False

def analyze_sentiment(text):
    """Analyze sentiment using the advanced multi-model system"""
    if not text.strip():
        return "Please enter some text to analyze!"
    
    try:
        if ADVANCED_AVAILABLE:
            # Use advanced multi-model system
            result = predict_advanced(text)
            
            # Format results for display
            model_results = []
            for model_result in result.results:
                model_results.append(f"**{model_result.model_name}**: {model_result.sentiment} ({model_result.confidence:.3f})")
            
            output = f"""
## 🎯 Consensus Result
**Sentiment**: {result.consensus_sentiment}  
**Confidence**: {result.average_confidence:.3f}  
**Agreement Score**: {result.agreement_score:.3f}  
**Processing Time**: {result.processing_time:.3f}s

## πŸ€– Individual Model Results
{chr(10).join(model_results)}

---
*Powered by 4 AI models working together for superior accuracy!*
            """
            return output
        else:
            # Fallback to basic model
            sentiment, confidence = predict(text)
            return f"""
## πŸ“Š Sentiment Analysis Result
**Sentiment**: {sentiment}  
**Confidence**: {confidence:.3f}

*Using YelpReviewsAnalyzer model*
            """
    
    except Exception as e:
        return f"❌ Error analyzing sentiment: {str(e)}"

# Create Gradio interface
demo = gr.Interface(
    fn=analyze_sentiment,
    inputs=gr.Textbox(
        label="πŸ“ Enter Text for Sentiment Analysis",
        placeholder="Type your text here... (e.g., 'This restaurant has amazing food!')",
        lines=3
    ),
    outputs=gr.Markdown(label="🎯 Analysis Results"),
    title="πŸš€ Advanced Sentiment Analyzer",
    description="""
    **Multi-Model AI System for Superior Sentiment Analysis**
    
    This system uses up to 4 different AI models working together to provide more accurate sentiment predictions:
    - 🎯 YelpReviewsAnalyzer (custom fine-tuned model)
    - πŸ€– DistilBERT (general-purpose)
    - 🐦 Twitter-RoBERTa (social media optimized)  
    - πŸ’° FinBERT (financial sentiment)
    
    The models vote on the final prediction using a consensus algorithm for higher accuracy!
    """,
    examples=[
        ["This restaurant has absolutely amazing food and incredible service!"],
        ["The food was terrible and the service was slow."],
        ["It's an okay place, nothing special but not bad either."],
        ["I love this product! Best purchase I've ever made."],
        ["This movie was boring and way too long."]
    ],
    theme=gr.themes.Soft(),
    allow_flagging="never"
)

if __name__ == "__main__":
    demo.launch()