GoshawkVortexAI commited on
Commit
ec4e1ae
·
verified ·
1 Parent(s): 9eaeee1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -42
app.py CHANGED
@@ -4,11 +4,12 @@ from transformers import AutoTokenizer, AutoModelForSequenceClassification
4
  import torch
5
  import torch.nn.functional as F
6
  from datetime import datetime
 
7
 
8
  # ==========================
9
- # 1️⃣ FINBERT LOAD
10
  # ==========================
11
- MODEL_NAME = "ProsusAI/finbert"
12
 
13
  tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
14
  model = AutoModelForSequenceClassification.from_pretrained(MODEL_NAME)
@@ -17,26 +18,12 @@ model.eval()
17
  # ==========================
18
  # 2️⃣ REUTERS SCRAPER
19
  # ==========================
20
- def fetch_reuters_articles(section="/business/", size=20):
21
- """
22
- Reuters unofficial JSON endpoint
23
- """
24
  url = "https://www.reuters.com/pf/api/v3/content/fetch/articles-by-section-alias-or-id-v1"
25
 
26
- query = {
27
- "query": {
28
- "section_id": section,
29
- "size": size
30
- }
31
- }
32
-
33
- params = {
34
- "query": str(query).replace("'", '"')
35
- }
36
-
37
- headers = {
38
- "User-Agent": "Mozilla/5.0"
39
- }
40
 
41
  response = requests.get(url, params=params, headers=headers)
42
  data = response.json()
@@ -58,35 +45,23 @@ def fetch_reuters_articles(section="/business/", size=20):
58
 
59
  return pd.DataFrame(articles)
60
 
61
-
62
  # ==========================
63
- # 3️⃣ FINBERT SENTIMENT
64
  # ==========================
65
  def finbert_score(text):
66
- inputs = tokenizer(
67
- text,
68
- return_tensors="pt",
69
- truncation=True,
70
- padding=True,
71
- max_length=512
72
- )
73
-
74
  with torch.no_grad():
75
  outputs = model(**inputs)
76
  probs = F.softmax(outputs.logits, dim=1)
77
-
78
  labels = ["negative", "neutral", "positive"]
79
  scores = probs[0].tolist()
80
-
81
  return dict(zip(labels, scores))
82
 
83
-
84
  # ==========================
85
  # 4️⃣ PIPELINE
86
  # ==========================
87
  def run_pipeline():
88
- df = fetch_reuters_articles(section="/business/", size=25)
89
-
90
  sentiments = []
91
  for _, row in df.iterrows():
92
  text = row["headline"] + ". " + row["summary"]
@@ -95,17 +70,26 @@ def run_pipeline():
95
 
96
  sentiment_df = pd.DataFrame(sentiments)
97
  final_df = pd.concat([df, sentiment_df], axis=1)
98
-
99
- # Hawkish score (neg - pos)
100
  final_df["hawkish_score"] = final_df["negative"] - final_df["positive"]
101
 
102
  filename = f"reuters_finbert_{datetime.today().date()}.csv"
103
  final_df.to_csv(filename, index=False)
 
104
 
105
- print("Saved:", filename)
106
- print(final_df.head())
107
-
108
-
109
  # ==========================
 
 
 
 
 
 
 
 
 
 
 
 
 
 
110
  if __name__ == "__main__":
111
- run_pipeline()
 
4
  import torch
5
  import torch.nn.functional as F
6
  from datetime import datetime
7
+ import gradio as gr
8
 
9
  # ==========================
10
+ # 1️⃣ MODEL LOAD (DistilFinBERT)
11
  # ==========================
12
+ MODEL_NAME = "yiyanghkust/finbert-tone-distil" # DistilFinBERT, CPU-friendly
13
 
14
  tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
15
  model = AutoModelForSequenceClassification.from_pretrained(MODEL_NAME)
 
18
  # ==========================
19
  # 2️⃣ REUTERS SCRAPER
20
  # ==========================
21
+ def fetch_reuters_articles(section="/business/", size=10):
 
 
 
22
  url = "https://www.reuters.com/pf/api/v3/content/fetch/articles-by-section-alias-or-id-v1"
23
 
24
+ query = {"query":{"section_id": section, "size": size}}
25
+ params = {"query": str(query).replace("'", '"')}
26
+ headers = {"User-Agent": "Mozilla/5.0"}
 
 
 
 
 
 
 
 
 
 
 
27
 
28
  response = requests.get(url, params=params, headers=headers)
29
  data = response.json()
 
45
 
46
  return pd.DataFrame(articles)
47
 
 
48
  # ==========================
49
+ # 3️⃣ SENTIMENT SCORING
50
  # ==========================
51
  def finbert_score(text):
52
+ inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True, max_length=512)
 
 
 
 
 
 
 
53
  with torch.no_grad():
54
  outputs = model(**inputs)
55
  probs = F.softmax(outputs.logits, dim=1)
 
56
  labels = ["negative", "neutral", "positive"]
57
  scores = probs[0].tolist()
 
58
  return dict(zip(labels, scores))
59
 
 
60
  # ==========================
61
  # 4️⃣ PIPELINE
62
  # ==========================
63
  def run_pipeline():
64
+ df = fetch_reuters_articles(size=10)
 
65
  sentiments = []
66
  for _, row in df.iterrows():
67
  text = row["headline"] + ". " + row["summary"]
 
70
 
71
  sentiment_df = pd.DataFrame(sentiments)
72
  final_df = pd.concat([df, sentiment_df], axis=1)
 
 
73
  final_df["hawkish_score"] = final_df["negative"] - final_df["positive"]
74
 
75
  filename = f"reuters_finbert_{datetime.today().date()}.csv"
76
  final_df.to_csv(filename, index=False)
77
+ return final_df.head(), filename
78
 
 
 
 
 
79
  # ==========================
80
+ # 5️⃣ GRADIO WEB APP
81
+ # ==========================
82
+ def gradio_interface(dummy_input):
83
+ table, fname = run_pipeline()
84
+ return table.to_string(), f"Saved CSV: {fname}"
85
+
86
+ iface = gr.Interface(
87
+ fn=gradio_interface,
88
+ inputs=gr.Textbox(label="Run Scraper (any text)"),
89
+ outputs=[gr.Textbox(label="Top Articles"), gr.Textbox(label="CSV Status")],
90
+ title="Reuters DistilFinBERT Scraper",
91
+ description="Fetch latest Reuters Business news + sentiment via DistilFinBERT"
92
+ )
93
+
94
  if __name__ == "__main__":
95
+ iface.launch()