Mujtaba29 commited on
Commit
704d3c5
Β·
verified Β·
1 Parent(s): 780bfa8

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +84 -0
app.py ADDED
@@ -0,0 +1,84 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import faiss
3
+ import streamlit as st
4
+ from groq import Groq
5
+ from PyPDF2 import PdfReader
6
+ from langchain.text_splitter import RecursiveCharacterTextSplitter
7
+ from sentence_transformers import SentenceTransformer
8
+ import numpy as np
9
+ from streamlit_option_menu import option_menu
10
+
11
+ # Groq API Key
12
+ client = Groq(api_key="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")
13
+
14
+ # Load embedding model
15
+ embedding_model = SentenceTransformer('all-MiniLM-L6-v2')
16
+
17
+ # FAISS Index
18
+ dimension = 384
19
+ index = faiss.IndexFlatL2(dimension)
20
+
21
+ # Sidebar navigation
22
+ with st.sidebar:
23
+ selected = option_menu(
24
+ "Research Article Helper",
25
+ ["Home", "Upload PDF", "Summary", "About"],
26
+ icons=["house", "file-earmark-arrow-up", "file-earmark-text", "info-circle"],
27
+ menu_icon="cast",
28
+ default_index=0,
29
+ )
30
+
31
+ # App title
32
+ st.title("πŸ“š Research Article Helper")
33
+ st.write("Interact with research articles by uploading PDFs and asking questions.")
34
+
35
+ # "Upload PDF" Section
36
+ if selected == "Upload PDF":
37
+ st.subheader("πŸ“‚ Upload a PDF")
38
+ uploaded_file = st.file_uploader("Upload a PDF file", type=["pdf"])
39
+ if uploaded_file:
40
+ pdf_reader = PdfReader(uploaded_file)
41
+ text = ""
42
+ for page in pdf_reader.pages:
43
+ text += page.extract_text()
44
+
45
+ # Split text into chunks
46
+ text_splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=50)
47
+ chunks = text_splitter.split_text(text)
48
+
49
+ # Encode chunks and store embeddings in FAISS
50
+ embeddings = embedding_model.encode(chunks)
51
+ for i, embedding in enumerate(embeddings):
52
+ index.add(np.array([embedding]))
53
+
54
+ st.success(f"βœ… Processed {len(chunks)} chunks and stored embeddings in FAISS.")
55
+ st.session_state['chunks'] = chunks
56
+
57
+ # "Summary" Section
58
+ if selected == "Summary":
59
+ st.subheader("πŸ“‘ Get a Summary of the Uploaded PDF")
60
+ if 'chunks' in st.session_state:
61
+ if st.button("Generate Summary"):
62
+ full_text = " ".join(st.session_state['chunks'][:5]) # Summarizing first 5 chunks as an example
63
+ chat_completion = client.chat.completions.create(
64
+ messages=[
65
+ {
66
+ "role": "user",
67
+ "content": f"Provide a concise summary of the following text:\n\n{full_text}",
68
+ }
69
+ ],
70
+ model="llama3-8b-8192",
71
+ )
72
+ summary = chat_completion.choices[0].message.content
73
+ st.write("### πŸ“‹ Summary:")
74
+ st.write(summary)
75
+ else:
76
+ st.info("Click the button above to generate a summary.")
77
+ else:
78
+ st.warning("Please upload a PDF in the 'Upload PDF' section first.")
79
+
80
+ # "About" Section
81
+ if selected == "About":
82
+ st.subheader("ℹ️ About")
83
+ st.write("**Research Article Helper** is a tool to interact with research articles by uploading PDFs. Use it to ask questions, generate summaries, and gain insights from scientific documents.")
84
+ st.write("Built using Streamlit, FAISS, and Groq AI.")