Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import zipfile
|
3 |
+
import pandas as pd
|
4 |
+
import gradio as gr
|
5 |
+
from sentence_transformers import SentenceTransformer
|
6 |
+
import faiss
|
7 |
+
import numpy as np
|
8 |
+
|
9 |
+
# Step 1: Unzip the dataset
|
10 |
+
zip_file = "climate.zip" # your uploaded zip file
|
11 |
+
extracted_path = "climate_data"
|
12 |
+
|
13 |
+
if not os.path.exists(extracted_path):
|
14 |
+
with zipfile.ZipFile(zip_file, 'r') as zip_ref:
|
15 |
+
zip_ref.extractall(extracted_path)
|
16 |
+
|
17 |
+
# Step 2: Load train.csv
|
18 |
+
csv_path = os.path.join(extracted_path, "train.csv")
|
19 |
+
df = pd.read_csv(csv_path, header=None, names=["label", "title", "description"])
|
20 |
+
|
21 |
+
# Combine title and description for semantic search
|
22 |
+
df["content"] = df["title"].fillna("") + ". " + df["description"].fillna("")
|
23 |
+
|
24 |
+
# Step 3: Encode using SentenceTransformer
|
25 |
+
model = SentenceTransformer('all-MiniLM-L6-v2')
|
26 |
+
corpus_embeddings = model.encode(df["content"].tolist(), show_progress_bar=True)
|
27 |
+
|
28 |
+
# Step 4: Create FAISS index
|
29 |
+
embedding_dim = corpus_embeddings.shape[1]
|
30 |
+
index = faiss.IndexFlatL2(embedding_dim)
|
31 |
+
index.add(corpus_embeddings)
|
32 |
+
|
33 |
+
def retrieve_and_respond(claim, k=5):
|
34 |
+
query_embedding = model.encode([claim])
|
35 |
+
D, I = index.search(np.array(query_embedding), k)
|
36 |
+
|
37 |
+
results = []
|
38 |
+
for idx in I[0]:
|
39 |
+
row = df.iloc[idx]
|
40 |
+
results.append(f"*Title:* {row['title']}\n*Description:* {row['description']}\n*Label:* {row['label']}\n")
|
41 |
+
|
42 |
+
return "\n\n".join(results)
|
43 |
+
|
44 |
+
# Step 5: Gradio Interface
|
45 |
+
iface = gr.Interface(
|
46 |
+
fn=retrieve_and_respond,
|
47 |
+
inputs=gr.Textbox(lines=2, placeholder="Enter a news-related claim here..."),
|
48 |
+
outputs="markdown",
|
49 |
+
title="Claim Verifier using RAG (AG News)",
|
50 |
+
description="Enter a claim and retrieve the most relevant AG News articles to verify or refute it."
|
51 |
+
)
|
52 |
+
|
53 |
+
iface.launch()
|