Upload RAG_streamlit_app.py
Browse files- RAG_streamlit_app.py +57 -60
RAG_streamlit_app.py
CHANGED
|
@@ -1,61 +1,58 @@
|
|
| 1 |
-
import streamlit as st
|
| 2 |
-
from transformers import pipeline, AutoTokenizer, AutoModelForSeq2SeqLM, AutoModelForSequenceClassification
|
| 3 |
-
from sentence_transformers import SentenceTransformer
|
| 4 |
-
import faiss, pickle
|
| 5 |
-
import
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
st.
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
st.subheader("
|
| 58 |
-
for r in rag_output['retrieved']:
|
| 59 |
-
st.write("-", r)
|
| 60 |
-
st.subheader("Explanation")
|
| 61 |
st.write(rag_output['explanation'])
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from transformers import pipeline, AutoTokenizer, AutoModelForSeq2SeqLM, AutoModelForSequenceClassification
|
| 3 |
+
from sentence_transformers import SentenceTransformer
|
| 4 |
+
import faiss, pickle
|
| 5 |
+
from PIL import Image
|
| 6 |
+
|
| 7 |
+
tokenizer = AutoTokenizer.from_pretrained("sentiment_model")
|
| 8 |
+
model = AutoModelForSequenceClassification.from_pretrained("sentiment_model")
|
| 9 |
+
clf = pipeline("text-classification", model=model, tokenizer=tokenizer, device=-1)
|
| 10 |
+
embedder = SentenceTransformer("all-MiniLM-L6-v2", device="cpu")
|
| 11 |
+
gen_tok = AutoTokenizer.from_pretrained("google/flan-t5-small")
|
| 12 |
+
gen_model = AutoModelForSeq2SeqLM.from_pretrained("google/flan-t5-base")
|
| 13 |
+
index = faiss.read_index("faiss_index.index")
|
| 14 |
+
with open("passages.pkl", "rb") as f:
|
| 15 |
+
train_texts = pickle.load(f)
|
| 16 |
+
|
| 17 |
+
def retrieve_passages(query, k=5):
|
| 18 |
+
q_emb = embedder.encode([query])
|
| 19 |
+
D, I = index.search(q_emb, k)
|
| 20 |
+
return [train_texts[i] for i in I[0]]
|
| 21 |
+
|
| 22 |
+
def explain_sentiment(text, predicted_label, k=3):
|
| 23 |
+
retrieved = retrieve_passages(text, k=k)
|
| 24 |
+
retrieved_str = "\n- ".join(retrieved)
|
| 25 |
+
prompt = f"""
|
| 26 |
+
Text to analyze: "{text}"
|
| 27 |
+
Predicted sentiment: {predicted_label}
|
| 28 |
+
Retrieved examples:
|
| 29 |
+
- {retrieved_str}
|
| 30 |
+
Task: Write a clear explanation (1–2 sentences) about why the text is {predicted_label}.
|
| 31 |
+
Focus on emotional words, tone, and context. Do NOT just repeat the label.
|
| 32 |
+
"""
|
| 33 |
+
inputs = gen_tok(prompt, return_tensors="pt", truncation=True, max_length=512)
|
| 34 |
+
outputs = gen_model.generate(**inputs, max_length=80)
|
| 35 |
+
explanation = gen_tok.decode(outputs[0], skip_special_tokens=True)
|
| 36 |
+
return {"retrieved": retrieved, "explanation": explanation}
|
| 37 |
+
|
| 38 |
+
st.title("Sentiment Analyzer")
|
| 39 |
+
image = Image.open(r"Social-Sentiment-Tracking.png")
|
| 40 |
+
st.image(image, width='stretch')
|
| 41 |
+
text = st.text_area("Enter text to analyze:")
|
| 42 |
+
if st.button("Predict"):
|
| 43 |
+
out = clf(text)[0]
|
| 44 |
+
pred = out['label']
|
| 45 |
+
score = out['score']
|
| 46 |
+
if pred == "LABEL_0":
|
| 47 |
+
human_pred = 'Negative'
|
| 48 |
+
elif pred == "LABEL_1":
|
| 49 |
+
human_pred = 'Neutral'
|
| 50 |
+
else:
|
| 51 |
+
human_pred = 'Positive'
|
| 52 |
+
st.write(f"**Prediction:** {human_pred} (confidence {score*100:.2f}%)")
|
| 53 |
+
rag_output = explain_sentiment(text, pred)
|
| 54 |
+
st.subheader("Retrieved evidence")
|
| 55 |
+
for r in rag_output['retrieved']:
|
| 56 |
+
st.write("-", r)
|
| 57 |
+
st.subheader("Explanation")
|
|
|
|
|
|
|
|
|
|
| 58 |
st.write(rag_output['explanation'])
|