File size: 1,340 Bytes
c455085
5553133
c455085
 
44cfbeb
 
5553133
44cfbeb
 
 
 
 
 
 
 
5553133
 
44cfbeb
5553133
 
44cfbeb
5553133
 
 
 
44cfbeb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
from utils import load_pipeline_from_huggingface


def predict_sentiment(text):
    """
    Predict sentiment of the input text using the loaded pipeline.
    
    Args:
        text (str): Input text to analyze
        
    Returns:
        str: Sentiment prediction
    """
    try:
        # Load pipeline
        sentiment_pipeline = load_pipeline_from_huggingface()
        
        # Get prediction using pipeline
        results = sentiment_pipeline(text)
        
        # Extract the highest confidence prediction
        best_result = max(results[0], key=lambda x: x['score'])
        sentiment = best_result['label']
        confidence = best_result['score']
        
        return f"Sentiment: {sentiment} (Confidence: {confidence:.2f})"
        
    except Exception as e:
        return f"Error: {str(e)}"


# Create Gradio interface
demo = gr.Interface(
    fn=predict_sentiment,
    inputs="text",
    outputs="text",
    title="Financial Sentiment Analysis",
    description="Enter financial text to analyze sentiment using the finetuned FinBERT model.",
    examples=[
        "The stock market is performing well today.",
        "The company's earnings report was disappointing.",
        "Investors are optimistic about the future prospects."
    ]
)

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