Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import pickle
|
2 |
+
import gradio as gr
|
3 |
+
import numpy as np
|
4 |
+
from gensim.models import Word2Vec
|
5 |
+
|
6 |
+
# Load the logistic regression model
|
7 |
+
with open('llogistic_model.pkl', 'rb') as file:
|
8 |
+
model = pickle.load(file)
|
9 |
+
|
10 |
+
# Load the Word2Vec model
|
11 |
+
word2vec_model = Word2Vec.load('word2vec_model.model')
|
12 |
+
|
13 |
+
def sentence_vector(tokens, model):
|
14 |
+
"""Calculate the sentence vector by averaging word vectors."""
|
15 |
+
valid_words = [word for word in tokens if word in model.wv]
|
16 |
+
if valid_words:
|
17 |
+
return np.mean(model.wv[valid_words], axis=0)
|
18 |
+
else:
|
19 |
+
return np.zeros(model.vector_size)
|
20 |
+
|
21 |
+
def classify_comment(comment):
|
22 |
+
"""Classify the sentiment of a comment as bearish, bullish, or neutral."""
|
23 |
+
try:
|
24 |
+
# Tokenize the comment
|
25 |
+
tokens = comment.lower().split()
|
26 |
+
|
27 |
+
# Generate sentence vector using Word2Vec
|
28 |
+
processed_comment = sentence_vector(tokens, word2vec_model).reshape(1, -1)
|
29 |
+
|
30 |
+
# Predict sentiment
|
31 |
+
prediction = model.predict(processed_comment)[0]
|
32 |
+
|
33 |
+
# Map prediction to labels (ensure the model output aligns with these labels)
|
34 |
+
sentiment_map = {0: "neutral", 1: "bullish", 2: "bearish"}
|
35 |
+
sentiment = sentiment_map.get(prediction, "unknown")
|
36 |
+
|
37 |
+
return sentiment
|
38 |
+
except Exception as e:
|
39 |
+
return f"Error: {str(e)}"
|
40 |
+
|
41 |
+
# Create Gradio interface
|
42 |
+
interface = gr.Interface(
|
43 |
+
fn=classify_comment,
|
44 |
+
inputs=gr.Textbox(label="Enter your comment (e.g., about BTC or stock markets):"),
|
45 |
+
outputs=gr.Label(label="Sentiment"),
|
46 |
+
title="BTC Sentiment Analyzer",
|
47 |
+
description="Predict whether a comment is bullish, bearish, or neutral using a logistic regression model."
|
48 |
+
)
|
49 |
+
|
50 |
+
# Launch the Gradio interface
|
51 |
+
interface.launch()
|