Spaces:
Sleeping
Sleeping
import gradio as gr | |
from transformers import pipeline | |
# Load a crypto-specific sentiment model (e.g., ElKulako/cryptobert) | |
sentiment_pipeline = pipeline( | |
"text-classification", | |
model="ElKulako/cryptobert", # Pre-trained on crypto data | |
tokenizer="ElKulako/cryptobert" | |
) | |
def analyze(text): | |
# Get the model's initial prediction | |
result = sentiment_pipeline(text)[0] | |
# Override logic for crypto-specific keywords (case-insensitive) | |
text_lower = text.lower() | |
# Force "positive" for bullish terms | |
bullish_keywords = ["etf approved", "bullish", "halving", "burn", "greenlighted"] | |
if any(keyword in text_lower for keyword in bullish_keywords): | |
return {"label": "positive", "score": 0.99} | |
# Force "negative" for bearish terms | |
bearish_keywords = ["sec lawsuit", "hack", "fud", "sell-off", "delist"] | |
if any(keyword in text_lower for keyword in bearish_keywords): | |
return {"label": "negative", "score": 0.99} | |
# Return original prediction if no keywords matched | |
return {"label": result["label"], "score": result["score"]} | |
# Configure Gradio interface for API compatibility | |
app = gr.Interface( | |
fn=analyze, | |
inputs=gr.Textbox(placeholder="Enter crypto news headline..."), | |
outputs=gr.JSON(), # JSON output for n8n integration | |
title="Crypto-Specific Sentiment Analysis", | |
description="Analyzes sentiment of crypto news headlines. Overrides neutral predictions for key terms like 'ETF approved' or 'SEC lawsuit'.", | |
flagging_mode="never" | |
) | |
# Launch Gradio app | |
app.launch(share=True) # 'share' parameter will generate a public link | |