Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,104 +1,97 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
|
|
|
| 5 |
import faiss
|
| 6 |
import numpy as np
|
| 7 |
-
import torch
|
| 8 |
from groq import Groq
|
| 9 |
-
from sentence_transformers import SentenceTransformer
|
| 10 |
|
| 11 |
-
#
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
return "
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
with st.spinner("Thinking..."):
|
| 100 |
-
context = "\n\n".join(retrieve_similar_chunks(query))
|
| 101 |
-
answer = get_llm_answer(query, context)
|
| 102 |
-
st.markdown(f'<div class="card"><b>Answer:</b><br>{answer}</div>', unsafe_allow_html=True)
|
| 103 |
-
|
| 104 |
-
st.markdown("<br><center style='color: grey;'>Built by Muqadas with β€οΈ using Streamlit + Groq + FAISS</center>", unsafe_allow_html=True)
|
|
|
|
| 1 |
+
!pip install -q gradio sentence-transformers faiss-cpu pdfplumber groq
|
| 2 |
+
|
| 3 |
+
import gradio as gr
|
| 4 |
+
import pdfplumber
|
| 5 |
+
from sentence_transformers import SentenceTransformer
|
| 6 |
import faiss
|
| 7 |
import numpy as np
|
|
|
|
| 8 |
from groq import Groq
|
|
|
|
| 9 |
|
| 10 |
+
# Global vars
|
| 11 |
+
model = SentenceTransformer("all-MiniLM-L6-v2")
|
| 12 |
+
documents, embeddings, index, text_chunks, client = [], None, None, [], None
|
| 13 |
+
|
| 14 |
+
def ask_llama3(system_prompt, user_prompt):
|
| 15 |
+
global client
|
| 16 |
+
try:
|
| 17 |
+
chat_completion = client.chat.completions.create(
|
| 18 |
+
model="llama-3.1-8b-instant",
|
| 19 |
+
messages=[
|
| 20 |
+
{"role": "system", "content": system_prompt},
|
| 21 |
+
{"role": "user", "content": user_prompt}
|
| 22 |
+
]
|
| 23 |
+
)
|
| 24 |
+
return chat_completion.choices[0].message.content
|
| 25 |
+
except Exception as e:
|
| 26 |
+
return f"β LLaMA3 error: {e}"
|
| 27 |
+
|
| 28 |
+
def pdf_to_chunks(pdf_file, user_key):
|
| 29 |
+
global text_chunks, embeddings, index, client
|
| 30 |
+
try:
|
| 31 |
+
client = Groq(api_key=user_key)
|
| 32 |
+
except Exception as e:
|
| 33 |
+
return f"β API key error: {e}"
|
| 34 |
+
|
| 35 |
+
text_chunks = []
|
| 36 |
+
try:
|
| 37 |
+
with pdfplumber.open(pdf_file.name) as pdf:
|
| 38 |
+
for page in pdf.pages:
|
| 39 |
+
text = page.extract_text()
|
| 40 |
+
if text:
|
| 41 |
+
sentences = text.split(". ")
|
| 42 |
+
text_chunks.extend(sentences)
|
| 43 |
+
|
| 44 |
+
if not text_chunks:
|
| 45 |
+
return "β No text found in PDF."
|
| 46 |
+
|
| 47 |
+
embeddings = model.encode(text_chunks, convert_to_tensor=False)
|
| 48 |
+
embeddings = np.array(embeddings).astype("float32")
|
| 49 |
+
dimension = embeddings.shape[1]
|
| 50 |
+
index = faiss.IndexFlatL2(dimension)
|
| 51 |
+
index.add(embeddings)
|
| 52 |
+
|
| 53 |
+
return "β
PDF processed and indexed successfully."
|
| 54 |
+
except Exception as e:
|
| 55 |
+
return f"β PDF processing error: {e}"
|
| 56 |
+
|
| 57 |
+
def query_document(question):
|
| 58 |
+
global index, text_chunks, model
|
| 59 |
+
if index is None or not text_chunks:
|
| 60 |
+
return "β Please upload and process a PDF first."
|
| 61 |
+
|
| 62 |
+
try:
|
| 63 |
+
q_embedding = model.encode([question])[0].astype("float32")
|
| 64 |
+
D, I = index.search(np.array([q_embedding]), 5)
|
| 65 |
+
retrieved_chunks = [text_chunks[i] for i in I[0]]
|
| 66 |
+
context = "\n".join(retrieved_chunks)
|
| 67 |
+
|
| 68 |
+
system_prompt = "You are a helpful study supervisor. Use the provided context to answer clearly."
|
| 69 |
+
user_prompt = f"Context:\n{context}\n\nQuestion:\n{question}"
|
| 70 |
+
|
| 71 |
+
return ask_llama3(system_prompt, user_prompt)
|
| 72 |
+
except Exception as e:
|
| 73 |
+
return f"β Query error: {e}"
|
| 74 |
+
|
| 75 |
+
# UI
|
| 76 |
+
with gr.Blocks() as demo:
|
| 77 |
+
gr.Markdown("""
|
| 78 |
+
<div style="text-align:center; background:#f97316; color:white; padding: 12px; border-radius: 10px;">
|
| 79 |
+
<h2>π PDF Study Assistant</h2>
|
| 80 |
+
<p>Ask questions from your uploaded PDF using Groq + LLaMA3</p>
|
| 81 |
+
</div>
|
| 82 |
+
""")
|
| 83 |
+
|
| 84 |
+
with gr.Column():
|
| 85 |
+
api_input = gr.Textbox(label="π Groq API Key", type="password")
|
| 86 |
+
pdf_input = gr.File(label="π Upload PDF", file_types=[".pdf"])
|
| 87 |
+
upload_btn = gr.Button("π₯ Extract & Index PDF", variant="primary")
|
| 88 |
+
status_output = gr.Textbox(label="π οΈ Status", interactive=False)
|
| 89 |
+
|
| 90 |
+
question = gr.Textbox(label="β Ask a Question", lines=2)
|
| 91 |
+
get_answer_btn = gr.Button("π¬ Get Answer")
|
| 92 |
+
answer_output = gr.Textbox(label="π’ Answer", lines=10, interactive=False)
|
| 93 |
+
|
| 94 |
+
upload_btn.click(fn=pdf_to_chunks, inputs=[pdf_input, api_input], outputs=[status_output])
|
| 95 |
+
get_answer_btn.click(fn=query_document, inputs=[question], outputs=[answer_output])
|
| 96 |
+
|
| 97 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|