Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,62 +1,43 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from transformers import pipeline
|
| 3 |
|
| 4 |
-
#
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
#
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 11 |
-
|
| 12 |
-
# Load sentiment pipeline
|
| 13 |
-
sentiment_pipeline = pipeline(
|
| 14 |
-
"text-classification",
|
| 15 |
-
model=model,
|
| 16 |
-
tokenizer=tokenizer
|
| 17 |
-
)
|
| 18 |
-
except Exception as e:
|
| 19 |
-
print(f"Error loading model: {e}")
|
| 20 |
-
exit(1)
|
| 21 |
|
| 22 |
-
# Sentiment analysis function with keyword overrides
|
| 23 |
def analyze(text):
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
# Force "negative" for bearish terms
|
| 40 |
-
bearish_keywords = ["sec lawsuit", "hack", "fud", "sell-off", "delist"]
|
| 41 |
-
if any(keyword in text_lower for keyword in bearish_keywords):
|
| 42 |
-
return {"label": "negative", "score": 0.99}
|
| 43 |
-
|
| 44 |
-
# Return original prediction if no keywords matched
|
| 45 |
-
return {"label": result["label"], "score": result["score"]}
|
| 46 |
-
|
| 47 |
-
except Exception as e:
|
| 48 |
-
return {"error": str(e)}
|
| 49 |
|
| 50 |
# Configure Gradio interface for API compatibility
|
| 51 |
-
gr.Interface(
|
| 52 |
fn=analyze,
|
| 53 |
inputs=gr.Textbox(placeholder="Enter crypto news headline..."),
|
| 54 |
-
outputs=gr.JSON(),
|
| 55 |
-
title="Crypto Sentiment Analysis",
|
| 56 |
-
description="Analyzes sentiment of crypto
|
| 57 |
-
flagging_mode="never"
|
| 58 |
-
).launch(
|
| 59 |
-
server_name="0.0.0.0",
|
| 60 |
-
server_port=7860
|
| 61 |
)
|
| 62 |
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
| 3 |
|
| 4 |
+
# Load a crypto-specific sentiment model (e.g., ElKulako/cryptobert)
|
| 5 |
+
sentiment_pipeline = pipeline(
|
| 6 |
+
"text-classification",
|
| 7 |
+
model="ElKulako/cryptobert", # Pre-trained on crypto data
|
| 8 |
+
tokenizer="ElKulako/cryptobert"
|
| 9 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
|
|
|
| 11 |
def analyze(text):
|
| 12 |
+
# Get the model's initial prediction
|
| 13 |
+
result = sentiment_pipeline(text)[0]
|
| 14 |
+
|
| 15 |
+
# Override logic for crypto-specific keywords (case-insensitive)
|
| 16 |
+
text_lower = text.lower()
|
| 17 |
+
|
| 18 |
+
# Force "positive" for bullish terms
|
| 19 |
+
bullish_keywords = ["etf approved", "bullish", "halving", "burn", "greenlighted"]
|
| 20 |
+
if any(keyword in text_lower for keyword in bullish_keywords):
|
| 21 |
+
return {"label": "positive", "score": 0.99}
|
| 22 |
|
| 23 |
+
# Force "negative" for bearish terms
|
| 24 |
+
bearish_keywords = ["sec lawsuit", "hack", "fud", "sell-off", "delist"]
|
| 25 |
+
if any(keyword in text_lower for keyword in bearish_keywords):
|
| 26 |
+
return {"label": "negative", "score": 0.99}
|
| 27 |
+
|
| 28 |
+
# Return original prediction if no keywords matched
|
| 29 |
+
return {"label": result["label"], "score": result["score"]}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
# Configure Gradio interface for API compatibility
|
| 32 |
+
app = gr.Interface(
|
| 33 |
fn=analyze,
|
| 34 |
inputs=gr.Textbox(placeholder="Enter crypto news headline..."),
|
| 35 |
+
outputs=gr.JSON(), # JSON output for n8n integration
|
| 36 |
+
title="Crypto-Specific Sentiment Analysis",
|
| 37 |
+
description="Analyzes sentiment of crypto news headlines. Overrides neutral predictions for key terms like 'ETF approved' or 'SEC lawsuit'.",
|
| 38 |
+
flagging_mode="never"
|
|
|
|
|
|
|
|
|
|
| 39 |
)
|
| 40 |
|
| 41 |
+
# Enable sharing and API
|
| 42 |
+
app.launch(share=True, api_name="predict")
|
| 43 |
+
|