Spaces:
Runtime error
Runtime error
Update src/mistral_llm.py
Browse files- src/mistral_llm.py +37 -21
src/mistral_llm.py
CHANGED
|
@@ -1,28 +1,33 @@
|
|
|
|
|
| 1 |
from transformers import pipeline
|
| 2 |
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
|
|
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
|
|
|
| 15 |
|
| 16 |
-
|
| 17 |
-
def
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
-
# βοΈ Summarize list of texts
|
| 26 |
def summarize_texts(texts):
|
| 27 |
summaries = []
|
| 28 |
for text in texts:
|
|
@@ -33,7 +38,6 @@ def summarize_texts(texts):
|
|
| 33 |
summaries.append("β οΈ Summary failed")
|
| 34 |
return summaries
|
| 35 |
|
| 36 |
-
# π Analyze sentiment
|
| 37 |
def analyze_sentiment(texts):
|
| 38 |
results = []
|
| 39 |
for text in texts:
|
|
@@ -43,3 +47,15 @@ def analyze_sentiment(texts):
|
|
| 43 |
except Exception:
|
| 44 |
results.append("Unknown")
|
| 45 |
return results
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
from transformers import pipeline
|
| 3 |
|
| 4 |
+
@st.cache_resource
|
| 5 |
+
def load_summarizer():
|
| 6 |
+
return pipeline(
|
| 7 |
+
"summarization",
|
| 8 |
+
model="sshleifer/distilbart-cnn-12-6",
|
| 9 |
+
tokenizer="sshleifer/distilbart-cnn-12-6"
|
| 10 |
+
)
|
| 11 |
|
| 12 |
+
@st.cache_resource
|
| 13 |
+
def load_sentiment():
|
| 14 |
+
return pipeline(
|
| 15 |
+
"sentiment-analysis",
|
| 16 |
+
model="distilbert-base-uncased-finetuned-sst-2-english"
|
| 17 |
+
)
|
| 18 |
|
| 19 |
+
@st.cache_resource
|
| 20 |
+
def load_fake_news_detector():
|
| 21 |
+
return pipeline(
|
| 22 |
+
"text-classification",
|
| 23 |
+
model="mrm8488/bert-tiny-finetuned-fake-news-detection",
|
| 24 |
+
tokenizer="mrm8488/bert-tiny-finetuned-fake-news-detection"
|
| 25 |
+
)
|
| 26 |
+
|
| 27 |
+
summarizer = load_summarizer()
|
| 28 |
+
sentiment_pipe = load_sentiment()
|
| 29 |
+
fake_news_pipe = load_fake_news_detector()
|
| 30 |
|
|
|
|
| 31 |
def summarize_texts(texts):
|
| 32 |
summaries = []
|
| 33 |
for text in texts:
|
|
|
|
| 38 |
summaries.append("β οΈ Summary failed")
|
| 39 |
return summaries
|
| 40 |
|
|
|
|
| 41 |
def analyze_sentiment(texts):
|
| 42 |
results = []
|
| 43 |
for text in texts:
|
|
|
|
| 47 |
except Exception:
|
| 48 |
results.append("Unknown")
|
| 49 |
return results
|
| 50 |
+
|
| 51 |
+
def detect_fake_news(texts):
|
| 52 |
+
results = []
|
| 53 |
+
for text in texts:
|
| 54 |
+
try:
|
| 55 |
+
prediction = fake_news_pipe(text[:512])[0]
|
| 56 |
+
label = prediction["label"]
|
| 57 |
+
score = prediction["score"]
|
| 58 |
+
results.append(f"{label} ({score:.2f})")
|
| 59 |
+
except Exception:
|
| 60 |
+
results.append("Unknown")
|
| 61 |
+
return results
|