File size: 1,715 Bytes
b3a6447
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import pickle
import gradio as gr
import numpy as np
from gensim.models import Word2Vec

# Load the logistic regression model
with open('llogistic_model.pkl', 'rb') as file:
    model = pickle.load(file)

# Load the Word2Vec model
word2vec_model = Word2Vec.load('word2vec_model.model')

def sentence_vector(tokens, model):
    """Calculate the sentence vector by averaging word vectors."""
    valid_words = [word for word in tokens if word in model.wv]
    if valid_words:
        return np.mean(model.wv[valid_words], axis=0)
    else:
        return np.zeros(model.vector_size)

def classify_comment(comment):
    """Classify the sentiment of a comment as bearish, bullish, or neutral."""
    try:
        # Tokenize the comment
        tokens = comment.lower().split()

        # Generate sentence vector using Word2Vec
        processed_comment = sentence_vector(tokens, word2vec_model).reshape(1, -1)
        
        # Predict sentiment
        prediction = model.predict(processed_comment)[0]
        
        # Map prediction to labels (ensure the model output aligns with these labels)
        sentiment_map = {0: "neutral", 1: "bullish", 2: "bearish"}
        sentiment = sentiment_map.get(prediction, "unknown")

        return sentiment
    except Exception as e:
        return f"Error: {str(e)}"

# Create Gradio interface
interface = gr.Interface(
    fn=classify_comment,
    inputs=gr.Textbox(label="Enter your comment (e.g., about BTC or stock markets):"),
    outputs=gr.Label(label="Sentiment"),
    title="BTC Sentiment Analyzer",
    description="Predict whether a comment is bullish, bearish, or neutral using a logistic regression model."
)

# Launch the Gradio interface
interface.launch()