nadzo commited on
Commit
4be3c71
·
verified ·
1 Parent(s): f5e51d8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -28
app.py CHANGED
@@ -1,42 +1,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
  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
  ).launch(
40
  server_name="0.0.0.0",
41
- server_port=7860
42
- )
 
 
1
  import gradio as gr
2
+ from transformers import pipeline, AutoModelForSequenceClassification, AutoTokenizer
3
 
4
+ # Model name
5
+ model_name = "ElKulako/cryptobert"
6
+
7
+ # Load model and tokenizer explicitly to prevent errors
8
+ try:
9
+ model = AutoModelForSequenceClassification.from_pretrained(model_name)
10
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
11
 
12
+ # Load the 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
+ if not text or text.strip() == "":
25
+ return {"error": "No text provided."}
26
+
27
+ try:
28
+ # Get the model's initial prediction
29
+ result = sentiment_pipeline(text)[0]
 
 
 
 
30
 
31
+ # Override logic for crypto-specific keywords (case-insensitive)
32
+ text_lower = text.lower()
33
+
34
+ # Force "positive" for bullish terms
35
+ bullish_keywords = ["etf approved", "bullish", "halving", "burn", "greenlighted"]
36
+ if any(keyword in text_lower for keyword in bullish_keywords):
37
+ return {"label": "positive", "score": 0.99}
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-Specific Sentiment Analysis",
56
  description="Analyzes sentiment of crypto news headlines. Overrides neutral predictions for key terms like 'ETF approved' or 'SEC lawsuit'.",
57
+ allow_flagging="never"
58
  ).launch(
59
  server_name="0.0.0.0",
60
+ server_port=7860,
61
+ enable_queue=True # Ensures API requests are handled properly
62
+ )