Spaces:
Running
Running
Commit
Β·
0ba063f
1
Parent(s):
633e590
Update app.py
Browse files
app.py
CHANGED
|
@@ -12,6 +12,9 @@ import traceback
|
|
| 12 |
# Initialize embedding model
|
| 13 |
sbert_model = SentenceTransformer('all-MiniLM-L6-v2')
|
| 14 |
|
|
|
|
|
|
|
|
|
|
| 15 |
# Data storage
|
| 16 |
chunks = []
|
| 17 |
faiss_index = None
|
|
@@ -61,12 +64,12 @@ def create_faiss_index(embeddings):
|
|
| 61 |
|
| 62 |
return index
|
| 63 |
|
| 64 |
-
def process_pdf(pdf_file_obj
|
| 65 |
"""Process PDF and create FAISS index."""
|
| 66 |
global chunks, faiss_index
|
| 67 |
|
| 68 |
-
if not
|
| 69 |
-
return None, [["System", "β οΈ
|
| 70 |
|
| 71 |
if pdf_file_obj is None:
|
| 72 |
return None, [["System", "π Please upload a PDF file."]]
|
|
@@ -146,7 +149,7 @@ def retrieve_relevant_chunks(query, top_k=3):
|
|
| 146 |
print(f"Error in FAISS search: {str(e)}")
|
| 147 |
return []
|
| 148 |
|
| 149 |
-
def chat_fn(message, history
|
| 150 |
"""Handle chat interaction."""
|
| 151 |
if not message.strip():
|
| 152 |
return history, ""
|
|
@@ -154,8 +157,8 @@ def chat_fn(message, history, api_key):
|
|
| 154 |
# Add user message to history
|
| 155 |
history = history + [[message, None]]
|
| 156 |
|
| 157 |
-
if not
|
| 158 |
-
history[-1][1] = "β οΈ
|
| 159 |
return history, ""
|
| 160 |
|
| 161 |
if not chunks or faiss_index is None:
|
|
@@ -164,7 +167,7 @@ def chat_fn(message, history, api_key):
|
|
| 164 |
|
| 165 |
try:
|
| 166 |
# Configure Gemini
|
| 167 |
-
genai.configure(api_key=
|
| 168 |
|
| 169 |
# Get relevant context using FAISS
|
| 170 |
context_chunks = retrieve_relevant_chunks(message, top_k=5)
|
|
@@ -207,25 +210,18 @@ css = """
|
|
| 207 |
"""
|
| 208 |
|
| 209 |
with gr.Blocks(css=css, title="π Chat with Your PDF") as demo:
|
| 210 |
-
api_key_state = gr.State("")
|
| 211 |
|
| 212 |
-
gr.Markdown("""
|
| 213 |
# π Chat with Your PDF (FAISS-Powered)
|
| 214 |
Upload a PDF document and chat with it naturally. Now with FAISS for faster vector search!
|
|
|
|
|
|
|
| 215 |
""")
|
| 216 |
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
type="password",
|
| 222 |
-
placeholder="Enter your API key here..."
|
| 223 |
-
)
|
| 224 |
-
with gr.Column(scale=1):
|
| 225 |
-
pdf_input = gr.File(
|
| 226 |
-
label="π Upload PDF",
|
| 227 |
-
file_types=['.pdf']
|
| 228 |
-
)
|
| 229 |
|
| 230 |
# Chat interface
|
| 231 |
chatbot = gr.Chatbot(
|
|
@@ -247,30 +243,21 @@ with gr.Blocks(css=css, title="π Chat with Your PDF") as demo:
|
|
| 247 |
clear_btn = gr.Button("Clear Chat ποΈ")
|
| 248 |
|
| 249 |
# Event handlers
|
| 250 |
-
def update_api_key(key):
|
| 251 |
-
return key
|
| 252 |
-
|
| 253 |
-
api_key_input.change(
|
| 254 |
-
fn=update_api_key,
|
| 255 |
-
inputs=api_key_input,
|
| 256 |
-
outputs=api_key_state
|
| 257 |
-
)
|
| 258 |
-
|
| 259 |
pdf_input.upload(
|
| 260 |
fn=process_pdf,
|
| 261 |
-
inputs=[pdf_input
|
| 262 |
outputs=[msg_input, chatbot]
|
| 263 |
)
|
| 264 |
|
| 265 |
submit_btn.click(
|
| 266 |
fn=chat_fn,
|
| 267 |
-
inputs=[msg_input, chatbot
|
| 268 |
outputs=[chatbot, msg_input]
|
| 269 |
)
|
| 270 |
|
| 271 |
msg_input.submit(
|
| 272 |
fn=chat_fn,
|
| 273 |
-
inputs=[msg_input, chatbot
|
| 274 |
outputs=[chatbot, msg_input]
|
| 275 |
)
|
| 276 |
|
|
|
|
| 12 |
# Initialize embedding model
|
| 13 |
sbert_model = SentenceTransformer('all-MiniLM-L6-v2')
|
| 14 |
|
| 15 |
+
# Get API key from environment
|
| 16 |
+
GEMINI_API_KEY = os.getenv('GAPI')
|
| 17 |
+
|
| 18 |
# Data storage
|
| 19 |
chunks = []
|
| 20 |
faiss_index = None
|
|
|
|
| 64 |
|
| 65 |
return index
|
| 66 |
|
| 67 |
+
def process_pdf(pdf_file_obj):
|
| 68 |
"""Process PDF and create FAISS index."""
|
| 69 |
global chunks, faiss_index
|
| 70 |
|
| 71 |
+
if not GEMINI_API_KEY:
|
| 72 |
+
return None, [["System", "β οΈ GAPI environment variable not set. Please configure your Gemini API key."]]
|
| 73 |
|
| 74 |
if pdf_file_obj is None:
|
| 75 |
return None, [["System", "π Please upload a PDF file."]]
|
|
|
|
| 149 |
print(f"Error in FAISS search: {str(e)}")
|
| 150 |
return []
|
| 151 |
|
| 152 |
+
def chat_fn(message, history):
|
| 153 |
"""Handle chat interaction."""
|
| 154 |
if not message.strip():
|
| 155 |
return history, ""
|
|
|
|
| 157 |
# Add user message to history
|
| 158 |
history = history + [[message, None]]
|
| 159 |
|
| 160 |
+
if not GEMINI_API_KEY:
|
| 161 |
+
history[-1][1] = "β οΈ GAPI environment variable not set. Please configure your Gemini API key."
|
| 162 |
return history, ""
|
| 163 |
|
| 164 |
if not chunks or faiss_index is None:
|
|
|
|
| 167 |
|
| 168 |
try:
|
| 169 |
# Configure Gemini
|
| 170 |
+
genai.configure(api_key=GEMINI_API_KEY)
|
| 171 |
|
| 172 |
# Get relevant context using FAISS
|
| 173 |
context_chunks = retrieve_relevant_chunks(message, top_k=5)
|
|
|
|
| 210 |
"""
|
| 211 |
|
| 212 |
with gr.Blocks(css=css, title="π Chat with Your PDF") as demo:
|
|
|
|
| 213 |
|
| 214 |
+
gr.Markdown(f"""
|
| 215 |
# π Chat with Your PDF (FAISS-Powered)
|
| 216 |
Upload a PDF document and chat with it naturally. Now with FAISS for faster vector search!
|
| 217 |
+
|
| 218 |
+
{"β
API Key: Configured from environment" if GEMINI_API_KEY else "β API Key: Not found in GAPI environment variable"}
|
| 219 |
""")
|
| 220 |
|
| 221 |
+
pdf_input = gr.File(
|
| 222 |
+
label="π Upload PDF",
|
| 223 |
+
file_types=['.pdf']
|
| 224 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 225 |
|
| 226 |
# Chat interface
|
| 227 |
chatbot = gr.Chatbot(
|
|
|
|
| 243 |
clear_btn = gr.Button("Clear Chat ποΈ")
|
| 244 |
|
| 245 |
# Event handlers
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 246 |
pdf_input.upload(
|
| 247 |
fn=process_pdf,
|
| 248 |
+
inputs=[pdf_input],
|
| 249 |
outputs=[msg_input, chatbot]
|
| 250 |
)
|
| 251 |
|
| 252 |
submit_btn.click(
|
| 253 |
fn=chat_fn,
|
| 254 |
+
inputs=[msg_input, chatbot],
|
| 255 |
outputs=[chatbot, msg_input]
|
| 256 |
)
|
| 257 |
|
| 258 |
msg_input.submit(
|
| 259 |
fn=chat_fn,
|
| 260 |
+
inputs=[msg_input, chatbot],
|
| 261 |
outputs=[chatbot, msg_input]
|
| 262 |
)
|
| 263 |
|