Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,94 +1,39 @@
|
|
| 1 |
-
import requests
|
| 2 |
-
from transformers import AutoTokenizer, AutoModelForSequenceClassification, pipeline
|
| 3 |
-
import csv
|
| 4 |
-
from datetime import datetime
|
| 5 |
import gradio as gr
|
|
|
|
|
|
|
| 6 |
|
| 7 |
-
#
|
| 8 |
-
#
|
| 9 |
-
# ==========================
|
| 10 |
-
MODEL_NAME = "cardiffnlp/twitter-roberta-base-financial-sentiment"
|
| 11 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
| 12 |
model = AutoModelForSequenceClassification.from_pretrained(MODEL_NAME)
|
| 13 |
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
-
#
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
def fetch_reuters_articles(section="/business/", size=10):
|
| 20 |
-
url = "https://www.reuters.com/pf/api/v3/content/fetch/articles-by-section-alias-or-id-v1"
|
| 21 |
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
return articles
|
| 45 |
-
|
| 46 |
-
# ==========================
|
| 47 |
-
# 3️⃣ SENTIMENT SCORING
|
| 48 |
-
# ==========================
|
| 49 |
-
def score_articles(articles):
|
| 50 |
-
scored = []
|
| 51 |
-
for art in articles:
|
| 52 |
-
text = art["headline"] + ". " + art["summary"]
|
| 53 |
-
result = nlp(text)[0] # cardiffnlp döndürür: {'label': 'positive', 'score': 0.95}
|
| 54 |
-
art.update(result)
|
| 55 |
-
scored.append(art)
|
| 56 |
-
return scored
|
| 57 |
-
|
| 58 |
-
# ==========================
|
| 59 |
-
# 4️⃣ CSV SAVE
|
| 60 |
-
# ==========================
|
| 61 |
-
def save_csv(scored_articles):
|
| 62 |
-
filename = f"reuters_sentiment_{datetime.today().date()}.csv"
|
| 63 |
-
keys = scored_articles[0].keys()
|
| 64 |
-
with open(filename, "w", newline="", encoding="utf-8") as f:
|
| 65 |
-
dict_writer = csv.DictWriter(f, fieldnames=keys)
|
| 66 |
-
dict_writer.writeheader()
|
| 67 |
-
dict_writer.writerows(scored_articles)
|
| 68 |
-
return filename
|
| 69 |
-
|
| 70 |
-
# ==========================
|
| 71 |
-
# 5️⃣ PIPELINE
|
| 72 |
-
# ==========================
|
| 73 |
-
def run_pipeline(dummy_input):
|
| 74 |
-
articles = fetch_reuters_articles(size=10)
|
| 75 |
-
scored = score_articles(articles)
|
| 76 |
-
csv_file = save_csv(scored)
|
| 77 |
-
|
| 78 |
-
# Terminal için tablo görünümü
|
| 79 |
-
display_text = "\n".join([f"{a['headline']} -> {a['label']} ({a['score']:.2f})" for a in scored])
|
| 80 |
-
return display_text, f"Saved CSV: {csv_file}"
|
| 81 |
-
|
| 82 |
-
# ==========================
|
| 83 |
-
# 6️⃣ GRADIO WEB APP
|
| 84 |
-
# ==========================
|
| 85 |
-
iface = gr.Interface(
|
| 86 |
-
fn=run_pipeline,
|
| 87 |
-
inputs=gr.Textbox(label="Run Scraper (any text)"),
|
| 88 |
-
outputs=[gr.Textbox(label="Top Articles"), gr.Textbox(label="CSV Status")],
|
| 89 |
-
title="Reuters Financial Sentiment (CardiffNLP)",
|
| 90 |
-
description="Fetch latest Reuters Business news + sentiment via CardiffNLP Roberta model (CPU-friendly, no pandas)"
|
| 91 |
-
)
|
| 92 |
-
|
| 93 |
-
if __name__ == "__main__":
|
| 94 |
-
iface.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 3 |
+
import torch
|
| 4 |
|
| 5 |
+
# Model ve Tokenizer'ı yükleme
|
| 6 |
+
MODEL_NAME = "yiyanghkust/finbert" # Bu FinBERT modelini kullanacağız
|
|
|
|
|
|
|
| 7 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
| 8 |
model = AutoModelForSequenceClassification.from_pretrained(MODEL_NAME)
|
| 9 |
|
| 10 |
+
# Sentiment analizi fonksiyonu
|
| 11 |
+
def analyze_sentiment(text):
|
| 12 |
+
# Verilen metni tokenize et
|
| 13 |
+
inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True, max_length=512)
|
| 14 |
|
| 15 |
+
# CPU'da çalışacak şekilde modelin inference yapmasını sağlıyoruz
|
| 16 |
+
with torch.no_grad():
|
| 17 |
+
outputs = model(**inputs)
|
|
|
|
|
|
|
| 18 |
|
| 19 |
+
# Modelin çıktısını al (logits)
|
| 20 |
+
logits = outputs.logits
|
| 21 |
+
predicted_class = torch.argmax(logits, dim=-1).item()
|
| 22 |
+
|
| 23 |
+
# Sentiment sınıfını belirle
|
| 24 |
+
if predicted_class == 0:
|
| 25 |
+
return "Negative"
|
| 26 |
+
elif predicted_class == 1:
|
| 27 |
+
return "Neutral"
|
| 28 |
+
else:
|
| 29 |
+
return "Positive"
|
| 30 |
+
|
| 31 |
+
# Gradio arayüzü
|
| 32 |
+
iface = gr.Interface(fn=analyze_sentiment,
|
| 33 |
+
inputs="text",
|
| 34 |
+
outputs="text",
|
| 35 |
+
title="Financial Sentiment Analysis",
|
| 36 |
+
description="Enter a financial text, and the model will analyze its sentiment (Positive, Neutral, or Negative).")
|
| 37 |
+
|
| 38 |
+
# Uygulamayı başlat
|
| 39 |
+
iface.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|