Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,69 +1,44 @@
|
|
1 |
-
import zipfile
|
2 |
-
import os
|
3 |
-
import pandas as pd
|
4 |
-
import torch
|
5 |
-
from sentence_transformers import SentenceTransformer, util
|
6 |
-
from transformers import pipeline
|
7 |
import streamlit as st
|
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 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
corpus = data["text"].tolist()
|
52 |
-
corpus_embeddings = embedder.encode(corpus, convert_to_tensor=True)
|
53 |
-
query_embedding = embedder.encode(claim, convert_to_tensor=True)
|
54 |
-
|
55 |
-
hits = util.semantic_search(query_embedding, corpus_embeddings, top_k=3)[0]
|
56 |
-
top_passages = [corpus[hit['corpus_id']] for hit in hits]
|
57 |
-
|
58 |
-
combined = " ".join(top_passages)
|
59 |
-
if len(combined) > 1024:
|
60 |
-
combined = combined[:1024]
|
61 |
-
|
62 |
-
summary = summarizer(combined, max_length=150, min_length=40, do_sample=False)[0]["summary_text"]
|
63 |
-
|
64 |
-
st.markdown("### β
Summary Based on News")
|
65 |
-
st.success(summary)
|
66 |
-
|
67 |
-
with st.expander("π View Related News Snippets"):
|
68 |
-
for i, passage in enumerate(top_passages, 1):
|
69 |
-
st.markdown(f"**Snippet {i}:** {passage}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
+
import pandas as pd
|
3 |
+
from datasets import load_dataset
|
4 |
+
from transformers import RagTokenizer, RagRetriever, RagSequenceForGeneration
|
5 |
+
|
6 |
+
# Load AG News dataset from Hugging Face
|
7 |
+
dataset = load_dataset("kk0105/ag-news", split="train")
|
8 |
+
|
9 |
+
# Tokenizer and Model setup for RAG
|
10 |
+
tokenizer = RagTokenizer.from_pretrained("facebook/rag-token-nq")
|
11 |
+
retriever = RagRetriever.from_pretrained("facebook/rag-token-nq", index_name="default")
|
12 |
+
model = RagSequenceForGeneration.from_pretrained("facebook/rag-token-nq")
|
13 |
+
|
14 |
+
# Function to generate response using RAG
|
15 |
+
def generate_answer(query):
|
16 |
+
# Tokenize input query
|
17 |
+
inputs = tokenizer(query, return_tensors="pt")
|
18 |
+
|
19 |
+
# Retrieve relevant documents from dataset
|
20 |
+
input_ids = inputs["input_ids"]
|
21 |
+
question_embedding = retriever.compute_question_embeddings(input_ids)
|
22 |
+
context_input_ids = retriever.retrieve(input_ids, question_embedding)
|
23 |
+
|
24 |
+
# Generate an answer using the retrieved context
|
25 |
+
outputs = model.generate(input_ids=input_ids, context_input_ids=context_input_ids)
|
26 |
+
|
27 |
+
# Decode the answer and return it
|
28 |
+
answer = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
29 |
+
return answer
|
30 |
+
|
31 |
+
# Streamlit interface
|
32 |
+
st.title("News Fact Checker")
|
33 |
+
st.write("""
|
34 |
+
**Welcome to the News Fact Checker!**
|
35 |
+
Input a claim or question about a news topic, and we will verify or refute it based on recent news snippets.
|
36 |
+
""")
|
37 |
+
|
38 |
+
# User input for claim
|
39 |
+
user_claim = st.text_input("Enter your claim or question:")
|
40 |
+
|
41 |
+
if user_claim:
|
42 |
+
with st.spinner('Fetching relevant news snippets...'):
|
43 |
+
answer = generate_answer(user_claim)
|
44 |
+
st.write(f"**Fact Check Answer:** {answer}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|