asif-coder commited on
Commit
9675ff7
·
verified ·
1 Parent(s): f34ed1e

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +163 -0
app.py ADDED
@@ -0,0 +1,163 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ os.environ["GROQ_API_KEY"] = "YOUR_GROQ_API_KEY"
3
+ # from google.colab import userdata
4
+ # GROQ_API_KEY=userdata.get('rag-based')
5
+ import gradio as gr
6
+ from groq import Groq
7
+
8
+ from langchain_text_splitters import RecursiveCharacterTextSplitter
9
+ from langchain_community.document_loaders import PyPDFLoader
10
+ from langchain_community.vectorstores import FAISS
11
+ from langchain_community.embeddings import HuggingFaceEmbeddings
12
+
13
+ # -----------------------------
14
+ # Environment Setup
15
+ # -----------------------------
16
+
17
+ # GROQ_API_KEY = os.environ.get("Rag-based")
18
+
19
+
20
+ client = Groq(api_key=os.environ.get("Rag-based"))
21
+
22
+ # -----------------------------
23
+ # Global Variables
24
+ # -----------------------------
25
+
26
+ vector_db = None
27
+
28
+ # -----------------------------
29
+ # Embedding Model
30
+ # -----------------------------
31
+
32
+ embedding_model = HuggingFaceEmbeddings(
33
+ model_name="sentence-transformers/all-MiniLM-L6-v2"
34
+ )
35
+
36
+ # -----------------------------
37
+ # Document Processing Function
38
+ # -----------------------------
39
+
40
+ def process_document(pdf_file):
41
+
42
+ global vector_db
43
+
44
+ if pdf_file is None:
45
+ return "Please upload a PDF Document first."
46
+
47
+ try:
48
+
49
+ # Load PDF
50
+ loader = PyPDFLoader(pdf_file.name)
51
+ documents = loader.load()
52
+
53
+ # Chunking
54
+ text_splitter = RecursiveCharacterTextSplitter(
55
+ chunk_size=1000,
56
+ chunk_overlap=200
57
+ )
58
+
59
+ chunks = text_splitter.split_documents(documents)
60
+
61
+ # Create FAISS vector database
62
+ vector_db = FAISS.from_documents(
63
+ chunks,
64
+ embedding_model
65
+ )
66
+
67
+ return f"Document processed successfully. {len(chunks)} chunks of your document created. Now, proceed to ask your question ahead."
68
+
69
+ except Exception as e:
70
+ return f"Error processing document: {str(e)}"
71
+
72
+
73
+ # -----------------------------
74
+ # Question Answering Function
75
+ # -----------------------------
76
+
77
+ def ask_question(question):
78
+
79
+ global vector_db
80
+
81
+ if vector_db is None:
82
+ return "Please upload and process a PDF document first."
83
+
84
+ try:
85
+
86
+ # Retrieve relevant chunks
87
+ docs = vector_db.similarity_search(question, k=4)
88
+
89
+ context = "\n\n".join([doc.page_content for doc in docs])
90
+
91
+ prompt = f"""
92
+ You are a helpful assistant. Answer the question ONLY using the following context.
93
+ If the answer is not in the context, say "I could not find the answer in the provided context."
94
+
95
+ Context:
96
+ {context}
97
+
98
+ Question:
99
+ {question}
100
+
101
+ Answer clearly and based only on the provided context.
102
+ """
103
+
104
+ # Groq LLM call
105
+ chat_completion = client.chat.completions.create(
106
+ messages=[
107
+ {"role": "user", "content": prompt}
108
+ ],
109
+ model="llama-3.3-70b-versatile",
110
+ )
111
+
112
+ response = chat_completion.choices[0].message.content
113
+
114
+ return response
115
+
116
+ except Exception as e:
117
+ return f"Error generating answer: {str(e)}"
118
+
119
+
120
+ # -----------------------------
121
+ # Gradio Interface
122
+ # -----------------------------
123
+
124
+ with gr.Blocks() as demo:
125
+
126
+ gr.Markdown("# 📄 PDF Document Assistant Developed by Asif Jamal")
127
+
128
+ gr.Markdown(
129
+ "Upload a PDF document and ask questions about its content."
130
+ )
131
+
132
+ pdf_input = gr.File(label="Upload PDF Document")
133
+
134
+ process_button = gr.Button("Click to Process Document")
135
+
136
+ process_output = gr.Textbox(label="Processing Status")
137
+
138
+ process_button.click(
139
+ process_document,
140
+ inputs=pdf_input,
141
+ outputs=process_output
142
+ )
143
+
144
+ gr.Markdown("## Ask Questions")
145
+
146
+ question_input = gr.Textbox(
147
+ label="Enter your question."
148
+ )
149
+
150
+ ask_button = gr.Button("Click to Proceed")
151
+
152
+ answer_output = gr.Textbox(
153
+ label="Answer",
154
+ lines=10
155
+ )
156
+
157
+ ask_button.click(
158
+ ask_question,
159
+ inputs=question_input,
160
+ outputs=answer_output
161
+ )
162
+
163
+ demo.launch()