blackmistcode commited on
Commit
87e8314
·
verified ·
1 Parent(s): 318e797

Initial ModernFinBERT space setup

Browse files
Files changed (3) hide show
  1. README.md +19 -0
  2. app.py +61 -0
  3. requirements.txt +4 -0
README.md ADDED
@@ -0,0 +1,19 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # ModernFinBERT Space
2
+
3
+ A lightweight Hugging Face Space that hosts [`tabularisai/ModernFinBERT`](https://huggingface.co/tabularisai/ModernFinBERT) for financial sentiment analysis.
4
+
5
+ - **Model size**: 0.1B parameters (fits on CPU)
6
+ - **Hardware**: CPU Basic (no GPU required)
7
+ - **SDK**: Gradio
8
+
9
+ ## API usage
10
+
11
+ The Gradio API is exposed automatically. You can call it from your backend with a simple HTTP `POST` to `/run/predict`.
12
+
13
+ Example payload:
14
+ ```json
15
+ {
16
+ "fn_index": 0,
17
+ "data": ["Revenue grew 15% YoY\nFed raises rates by 0.75bps"]
18
+ }
19
+ ```
app.py ADDED
@@ -0,0 +1,61 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import pipeline
3
+
4
+ print("Loading tabularisai/ModernFinBERT on CPU...")
5
+ classifier = pipeline(
6
+ "text-classification",
7
+ model="tabularisai/ModernFinBERT",
8
+ device=-1, # CPU only — 0.1B params fits comfortably
9
+ )
10
+ print("Model ready.")
11
+
12
+
13
+ def predict_sentiment(text_block):
14
+ """
15
+ Accepts multiple lines of text, classifies each one.
16
+ Returns a JSON list of {label, score} dicts.
17
+ """
18
+ if not text_block:
19
+ return []
20
+
21
+ # Split by newline, strip, drop empties
22
+ texts = [t.strip() for t in text_block.splitlines() if t.strip()]
23
+ if not texts:
24
+ return []
25
+
26
+ # Batch inference
27
+ raw_results = classifier(texts, batch_size=32)
28
+
29
+ # Normalise output
30
+ results = [
31
+ {"label": r["label"], "score": float(r["score"])}
32
+ for r in raw_results
33
+ ]
34
+ return results
35
+
36
+
37
+ with gr.Blocks(title="ModernFinBERT") as demo:
38
+ gr.Markdown("""
39
+ # ModernFinBERT Sentiment Analysis
40
+ Financial sentiment classifier powered by
41
+ [`tabularisai/ModernFinBERT`](https://huggingface.co/tabularisai/ModernFinBERT).
42
+ Runs on CPU — no GPU required.
43
+ """)
44
+
45
+ with gr.Row():
46
+ with gr.Column(scale=1):
47
+ input_box = gr.Textbox(
48
+ lines=10,
49
+ label="Input texts",
50
+ placeholder="Paste one headline / sentence per line...",
51
+ )
52
+ submit_btn = gr.Button("Analyze", variant="primary")
53
+ with gr.Column(scale=1):
54
+ output_json = gr.JSON(label="Results")
55
+
56
+ submit_btn.click(fn=predict_sentiment, inputs=input_box, outputs=output_json)
57
+
58
+ # Also expose a direct API at /run/predict (fn_index 0)
59
+
60
+ if __name__ == "__main__":
61
+ demo.launch(server_name="0.0.0.0", server_port=7860)
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ gradio>=5.0,<6.0
2
+ transformers>=4.36
3
+ torch>=2.0
4
+ numpy