MalaikahAamer0315 commited on
Commit
05172f4
Β·
verified Β·
1 Parent(s): e5a670e

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -0
app.py ADDED
@@ -0,0 +1,51 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from PyPDF2 import PdfReader
3
+ from sentence_transformers import SentenceTransformer
4
+ from transformers import pipeline
5
+ import faiss
6
+ import numpy as np
7
+
8
+ # Load models
9
+ embedder = SentenceTransformer('all-MiniLM-L6-v2')
10
+ qa_pipeline = pipeline('question-answering', model='distilbert-base-uncased-distilled-squad')
11
+
12
+ st.set_page_config(page_title="QuickLit - AI Research Assistant")
13
+ st.title("πŸ“š QuickLit: Literature Q&A Assistant")
14
+
15
+ # File upload
16
+ uploaded_file = st.file_uploader("Upload a research paper (PDF)", type=["pdf"])
17
+
18
+ if uploaded_file:
19
+ reader = PdfReader(uploaded_file)
20
+ full_text = ""
21
+ for page in reader.pages:
22
+ full_text += page.extract_text()
23
+
24
+ # Split text into chunks
25
+ sentences = full_text.split('. ')
26
+ chunks = ['. '.join(sentences[i:i+3]) for i in range(0, len(sentences), 3)]
27
+
28
+ # Generate embeddings
29
+ st.info("πŸ”Ž Generating embeddings...")
30
+ embeddings = embedder.encode(chunks)
31
+
32
+ # Create FAISS index
33
+ index = faiss.IndexFlatL2(embeddings[0].shape[0])
34
+ index.add(np.array(embeddings))
35
+
36
+ # Input question
37
+ question = st.text_input("Ask a question about the paper:")
38
+
39
+ if question:
40
+ # Embed the question
41
+ q_embedding = embedder.encode([question])
42
+
43
+ # Retrieve top 3 similar chunks
44
+ D, I = index.search(np.array(q_embedding), k=3)
45
+ retrieved_contexts = [chunks[i] for i in I[0]]
46
+ context = " ".join(retrieved_contexts)
47
+
48
+ # Answer using transformer
49
+ st.info("πŸ’‘ Answering with AI...")
50
+ answer = qa_pipeline(question=question, context=context)
51
+ st.success(f"**Answer:** {answer['answer']}")