Spaces:
Sleeping
Sleeping
Jakaria commited on
Commit ·
f2ec379
1
Parent(s): bf8bf90
Add Bangla model API
Browse files
app.py
CHANGED
|
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from config import DATA_DIR, VECTOR_DIR, UPLOADED_PDF_PATH, GROQ_API_KEY
|
| 4 |
+
from ingestion import ingest_pdf
|
| 5 |
+
from rag import answer, reset_memory
|
| 6 |
+
|
| 7 |
+
# Prepare runtime folders
|
| 8 |
+
os.makedirs(DATA_DIR, exist_ok=True)
|
| 9 |
+
os.makedirs(VECTOR_DIR, exist_ok=True)
|
| 10 |
+
|
| 11 |
+
def do_ingest(pdf_file):
|
| 12 |
+
if GROQ_API_KEY.strip() == "":
|
| 13 |
+
return "⚠️ GROQ_API_KEY is missing. Add it in your environment / HF Space secrets."
|
| 14 |
+
|
| 15 |
+
if pdf_file is None:
|
| 16 |
+
return "Please upload a PDF."
|
| 17 |
+
|
| 18 |
+
# Save uploaded file
|
| 19 |
+
with open(UPLOADED_PDF_PATH, "wb") as f:
|
| 20 |
+
f.write(pdf_file.read())
|
| 21 |
+
|
| 22 |
+
# Ingest and reset memory
|
| 23 |
+
msg = ingest_pdf(UPLOADED_PDF_PATH)
|
| 24 |
+
reset_memory()
|
| 25 |
+
return f"✅ {msg}"
|
| 26 |
+
|
| 27 |
+
def chat_fn(user_message, history):
|
| 28 |
+
# Ensure vector index exists
|
| 29 |
+
if not os.path.exists(VECTOR_DIR) or not os.listdir(VECTOR_DIR):
|
| 30 |
+
return "Please upload a PDF and click 'Ingest PDF' first.", history
|
| 31 |
+
|
| 32 |
+
ans = answer(user_message)
|
| 33 |
+
history.append((user_message, ans))
|
| 34 |
+
return "", history
|
| 35 |
+
|
| 36 |
+
with gr.Blocks() as demo:
|
| 37 |
+
gr.Markdown("## 📚 Chat with Your PDF — FAISS + Groq + Memory (LangChain)")
|
| 38 |
+
|
| 39 |
+
with gr.Row():
|
| 40 |
+
pdf = gr.File(label="Upload PDF", file_types=[".pdf"], type="file")
|
| 41 |
+
ingest_btn = gr.Button("Ingest PDF")
|
| 42 |
+
|
| 43 |
+
status = gr.Textbox(label="Status", interactive=False)
|
| 44 |
+
|
| 45 |
+
chatbot = gr.Chatbot(label="Chat")
|
| 46 |
+
msg = gr.Textbox(label="Ask a question about the PDF and press Enter")
|
| 47 |
+
clear = gr.Button("Clear Chat")
|
| 48 |
+
|
| 49 |
+
ingest_btn.click(do_ingest, inputs=pdf, outputs=status)
|
| 50 |
+
msg.submit(chat_fn, [msg, chatbot], [msg, chatbot])
|
| 51 |
+
clear.click(lambda: None, None, chatbot, queue=False)
|
| 52 |
+
|
| 53 |
+
if __name__ == "__main__":
|
| 54 |
+
demo.launch()
|