Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import pandas as pd
|
3 |
+
import faiss
|
4 |
+
from sentence_transformers import SentenceTransformer
|
5 |
+
from transformers import pipeline
|
6 |
+
|
7 |
+
# Load abstracts
|
8 |
+
df = pd.read_csv("abstracts.csv")
|
9 |
+
abstracts = df["abstract"].tolist()
|
10 |
+
|
11 |
+
# Load embedding model
|
12 |
+
embedder = SentenceTransformer("all-MiniLM-L6-v2")
|
13 |
+
abstract_embeddings = embedder.encode(abstracts, show_progress_bar=True)
|
14 |
+
|
15 |
+
# Build FAISS index
|
16 |
+
index = faiss.IndexFlatL2(abstract_embeddings.shape[1])
|
17 |
+
index.add(abstract_embeddings)
|
18 |
+
|
19 |
+
# Load LLM from Hugging Face Hub
|
20 |
+
llm = pipeline("text-generation", model="tiiuae/falcon-7b-instruct", max_new_tokens=300)
|
21 |
+
|
22 |
+
def verify_claim(claim):
|
23 |
+
query_vec = embedder.encode([claim])
|
24 |
+
D, I = index.search(query_vec, 3)
|
25 |
+
|
26 |
+
top_abstracts = df.iloc[I[0]]["abstract"].tolist()
|
27 |
+
context = "\n".join(top_abstracts)
|
28 |
+
|
29 |
+
prompt = f"Claim: {claim}\n\nEvidence:\n{context}\n\nAnswer True, False, or Uncertain. Then explain why:\n"
|
30 |
+
output = llm(prompt)[0]["generated_text"]
|
31 |
+
|
32 |
+
return f"🔍 **Top Abstracts:**\n{context}\n\n🧠 **LLM Response:**\n{output}"
|
33 |
+
|
34 |
+
# Gradio UI
|
35 |
+
gr.Interface(
|
36 |
+
fn=verify_claim,
|
37 |
+
inputs=gr.Textbox(label="Enter a scientific claim"),
|
38 |
+
outputs=gr.Markdown(),
|
39 |
+
title="🔬 Scientific Claim Verifier",
|
40 |
+
description="Checks the validity of a scientific claim using PubMed abstracts + LLM"
|
41 |
+
).launch()
|