Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,45 +1,46 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
import
|
| 3 |
-
from datasets import load_dataset
|
| 4 |
from sentence_transformers import SentenceTransformer
|
| 5 |
-
from transformers import
|
| 6 |
|
| 7 |
-
#
|
| 8 |
-
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
# Load models
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
|
|
|
|
|
|
| 17 |
|
| 18 |
-
# Main function for the Streamlit app
|
| 19 |
def main():
|
| 20 |
st.title("Multi-Stage Text Retrieval Pipeline for QA")
|
| 21 |
-
|
| 22 |
-
# Load dataset
|
| 23 |
-
dataset = load_nq_dataset()
|
| 24 |
-
|
| 25 |
-
# User input
|
| 26 |
question = st.text_input("Enter a question:")
|
| 27 |
|
| 28 |
if question:
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
|
|
|
| 43 |
|
| 44 |
if __name__ == "__main__":
|
| 45 |
main()
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
import numpy as np
|
|
|
|
| 3 |
from sentence_transformers import SentenceTransformer
|
| 4 |
+
from transformers import CrossEncoder
|
| 5 |
|
| 6 |
+
# Sample passages
|
| 7 |
+
passages = [
|
| 8 |
+
"The sky is blue.",
|
| 9 |
+
"The grass is green.",
|
| 10 |
+
"The sun is bright.",
|
| 11 |
+
"Rain falls from the sky.",
|
| 12 |
+
"Flowers bloom in spring."
|
| 13 |
+
]
|
| 14 |
|
| 15 |
# Load models
|
| 16 |
+
embedding_model = SentenceTransformer('sentence-transformers/all-MiniLM-L6-v2')
|
| 17 |
+
ranking_model = CrossEncoder('cross-encoder/ms-marco-MiniLM-L-12-v2')
|
| 18 |
+
|
| 19 |
+
def get_relevant_passages(question, passages):
|
| 20 |
+
keywords = question.lower().split()
|
| 21 |
+
relevant_passages = [p for p in passages if any(keyword in p.lower() for keyword in keywords)]
|
| 22 |
+
return relevant_passages if relevant_passages else passages # Return all if no match
|
| 23 |
|
|
|
|
| 24 |
def main():
|
| 25 |
st.title("Multi-Stage Text Retrieval Pipeline for QA")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
question = st.text_input("Enter a question:")
|
| 27 |
|
| 28 |
if question:
|
| 29 |
+
relevant_passages = get_relevant_passages(question, passages)
|
| 30 |
+
st.write("Relevant passages:")
|
| 31 |
+
for p in relevant_passages:
|
| 32 |
+
st.write(f"- {p}")
|
| 33 |
+
|
| 34 |
+
# Embedding and ranking
|
| 35 |
+
if st.button("Retrieve Answers"):
|
| 36 |
+
passage_embeddings = embedding_model.encode(relevant_passages)
|
| 37 |
+
question_embedding = embedding_model.encode(question)
|
| 38 |
+
scores = np.dot(passage_embeddings, question_embedding.T)
|
| 39 |
+
ranked_indices = np.argsort(scores)[::-1]
|
| 40 |
+
|
| 41 |
+
st.write("Ranked passages:")
|
| 42 |
+
for idx in ranked_indices:
|
| 43 |
+
st.write(f"- {relevant_passages[idx]} (Score: {scores[idx]:.2f})")
|
| 44 |
|
| 45 |
if __name__ == "__main__":
|
| 46 |
main()
|