JaveriaZia commited on
Commit
9fc215e
Β·
verified Β·
1 Parent(s): 84f27a7

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +122 -0
app.py ADDED
@@ -0,0 +1,122 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import fitz # PyMuPDF
3
+ import os
4
+ from sentence_transformers import SentenceTransformer
5
+ import numpy as np
6
+ import faiss
7
+ import uuid
8
+ from groq import Groq
9
+
10
+ # Load embedding model
11
+ embedder = SentenceTransformer("all-MiniLM-L6-v2")
12
+
13
+ # Initialize vector store and document store
14
+ document_chunks = []
15
+ doc_embeddings = []
16
+ doc_ids = []
17
+ index = None
18
+
19
+ # Get Groq API key from environment variable
20
+ GROQ_API_KEY = os.getenv("GROQ_API_KEY")
21
+
22
+ client = Groq(api_key=GROQ_API_KEY)
23
+
24
+ # Load and split PDF
25
+ def extract_text_from_pdf(pdf_path):
26
+ doc = fitz.open(pdf_path)
27
+ text = ""
28
+ for page in doc:
29
+ text += page.get_text()
30
+ return text
31
+
32
+ # Chunking logic
33
+ def chunk_text(text, max_tokens=500):
34
+ import re
35
+ sentences = re.split(r'(?<=[.!?]) +', text)
36
+ chunks = []
37
+ chunk = ""
38
+ tokens = 0
39
+
40
+ for sentence in sentences:
41
+ sentence_tokens = len(sentence.split())
42
+ if tokens + sentence_tokens > max_tokens:
43
+ chunks.append(chunk.strip())
44
+ chunk = sentence
45
+ tokens = sentence_tokens
46
+ else:
47
+ chunk += " " + sentence
48
+ tokens += sentence_tokens
49
+
50
+ if chunk:
51
+ chunks.append(chunk.strip())
52
+ return chunks
53
+
54
+ # Indexing
55
+ def index_pdf(pdf_file):
56
+ global document_chunks, doc_embeddings, doc_ids, index
57
+
58
+ if not pdf_file:
59
+ return "❌ Please upload a PDF file."
60
+
61
+ text = extract_text_from_pdf(pdf_file.name)
62
+ document_chunks = chunk_text(text)
63
+
64
+ doc_embeddings = embedder.encode(document_chunks)
65
+ doc_embeddings = np.array(doc_embeddings).astype("float32")
66
+
67
+ dimension = doc_embeddings.shape[1]
68
+ index = faiss.IndexFlatL2(dimension)
69
+ index.add(doc_embeddings)
70
+
71
+ doc_ids = [str(uuid.uuid4()) for _ in range(len(document_chunks))]
72
+
73
+ return "βœ… PDF indexed successfully. You can now ask questions."
74
+
75
+ # Retrieve top chunks
76
+ def retrieve_relevant_chunks(query, k=3):
77
+ query_embedding = embedder.encode([query]).astype("float32")
78
+ distances, indices = index.search(query_embedding, k)
79
+ return [document_chunks[i] for i in indices[0]]
80
+
81
+ # Generate answer using Groq
82
+ def generate_answer(user_query):
83
+ if index is None:
84
+ return "❌ Please upload and index a PDF first."
85
+
86
+ top_chunks = retrieve_relevant_chunks(user_query, k=3)
87
+ context = "\n\n".join(top_chunks)
88
+
89
+ messages = [
90
+ {"role": "system", "content": "You are a helpful academic assistant who answers questions based on uploaded PDF papers."},
91
+ {"role": "user", "content": f"Context: {context}\n\nQuestion: {user_query}"}
92
+ ]
93
+
94
+ try:
95
+ response = client.chat.completions.create(
96
+ messages=messages,
97
+ model="llama3-8b-8192",
98
+ )
99
+ return response.choices[0].message.content.strip()
100
+ except Exception as e:
101
+ return f"❌ Error generating response: {e}"
102
+
103
+ # Gradio UI
104
+ with gr.Blocks(title="πŸ“˜ PDF Question Assistant") as demo:
105
+ gr.Markdown("# πŸ“˜ Ask Questions About Your PDF")
106
+ with gr.Tab("πŸ“„ Upload & Index"):
107
+ with gr.Row():
108
+ pdf_input = gr.File(label="Upload PDF File", type="filepath", file_types=[".pdf"])
109
+ upload_btn = gr.Button("πŸ” Index PDF", variant="primary")
110
+ upload_status = gr.Textbox(label="", interactive=False, placeholder="Status will appear here...")
111
+
112
+ with gr.Tab("❓ Ask a Question"):
113
+ with gr.Row():
114
+ query = gr.Textbox(label="Ask something from the PDF", placeholder="E.g. What is the main argument of the paper?")
115
+ query_btn = gr.Button("🧠 Get Answer")
116
+ answer = gr.Textbox(label="Answer", placeholder="AI-generated answer will appear here...", lines=8)
117
+
118
+ upload_btn.click(fn=index_pdf, inputs=[pdf_input], outputs=[upload_status])
119
+ query_btn.click(fn=generate_answer, inputs=[query], outputs=[answer])
120
+
121
+ if __name__ == "__main__":
122
+ demo.launch()