Update app.py
Browse files
app.py
CHANGED
|
@@ -13,7 +13,6 @@ from sklearn.metrics.pairwise import cosine_similarity
|
|
| 13 |
MODEL_URL = "https://huggingface.co/melsonop/mistral-chat-pdf/resolve/main/mistral-7b-instruct-v0.1.Q4_K_M.gguf"
|
| 14 |
MODEL_PATH = "models/mistral.gguf"
|
| 15 |
|
| 16 |
-
# Download model if not exists
|
| 17 |
if not os.path.exists(MODEL_PATH):
|
| 18 |
os.makedirs("models", exist_ok=True)
|
| 19 |
print("Downloading model...")
|
|
@@ -24,7 +23,6 @@ if not os.path.exists(MODEL_PATH):
|
|
| 24 |
f.write(chunk)
|
| 25 |
print("Model download completed!")
|
| 26 |
|
| 27 |
-
# Load model
|
| 28 |
llm = Llama(
|
| 29 |
model_path=MODEL_PATH,
|
| 30 |
n_ctx=2048,
|
|
@@ -33,11 +31,10 @@ llm = Llama(
|
|
| 33 |
verbose=False
|
| 34 |
)
|
| 35 |
|
| 36 |
-
# Load embedding model
|
| 37 |
embed_model = SentenceTransformer("all-MiniLM-L6-v2")
|
| 38 |
|
| 39 |
# -----------------------------------
|
| 40 |
-
# PDF
|
| 41 |
# -----------------------------------
|
| 42 |
|
| 43 |
docs = []
|
|
@@ -52,39 +49,36 @@ def load_pdf(file):
|
|
| 52 |
|
| 53 |
docs = text.split(". ")
|
| 54 |
doc_embeddings = embed_model.encode(docs)
|
| 55 |
-
return "PDF loaded. You can now ask questions."
|
| 56 |
|
| 57 |
-
|
| 58 |
-
# Question Answering
|
| 59 |
-
# -----------------------------------
|
| 60 |
-
|
| 61 |
-
def answer_question(question):
|
| 62 |
if not docs:
|
| 63 |
-
return "Please upload a PDF first."
|
| 64 |
-
|
| 65 |
-
question_embedding = embed_model.encode([
|
| 66 |
similarities = cosine_similarity(question_embedding, doc_embeddings)[0]
|
| 67 |
top_indices = similarities.argsort()[-3:][::-1]
|
| 68 |
context = "\n".join([docs[i] for i in top_indices])
|
| 69 |
|
| 70 |
-
prompt = f"""You are a helpful assistant. Use the context below to answer the question.\n\nContext:\n{context}\n\nQuestion: {
|
| 71 |
-
|
| 72 |
output = llm(prompt=prompt, max_tokens=512, temperature=0.7)
|
| 73 |
return output['choices'][0]['text'].strip()
|
| 74 |
|
| 75 |
# -----------------------------------
|
| 76 |
-
# Gradio
|
| 77 |
# -----------------------------------
|
| 78 |
|
| 79 |
with gr.Blocks() as demo:
|
| 80 |
-
gr.Markdown("#
|
| 81 |
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
|
|
|
| 89 |
|
|
|
|
| 90 |
demo.launch()
|
|
|
|
| 13 |
MODEL_URL = "https://huggingface.co/melsonop/mistral-chat-pdf/resolve/main/mistral-7b-instruct-v0.1.Q4_K_M.gguf"
|
| 14 |
MODEL_PATH = "models/mistral.gguf"
|
| 15 |
|
|
|
|
| 16 |
if not os.path.exists(MODEL_PATH):
|
| 17 |
os.makedirs("models", exist_ok=True)
|
| 18 |
print("Downloading model...")
|
|
|
|
| 23 |
f.write(chunk)
|
| 24 |
print("Model download completed!")
|
| 25 |
|
|
|
|
| 26 |
llm = Llama(
|
| 27 |
model_path=MODEL_PATH,
|
| 28 |
n_ctx=2048,
|
|
|
|
| 31 |
verbose=False
|
| 32 |
)
|
| 33 |
|
|
|
|
| 34 |
embed_model = SentenceTransformer("all-MiniLM-L6-v2")
|
| 35 |
|
| 36 |
# -----------------------------------
|
| 37 |
+
# PDF + QA
|
| 38 |
# -----------------------------------
|
| 39 |
|
| 40 |
docs = []
|
|
|
|
| 49 |
|
| 50 |
docs = text.split(". ")
|
| 51 |
doc_embeddings = embed_model.encode(docs)
|
| 52 |
+
return "✅ PDF loaded. You can now ask questions."
|
| 53 |
|
| 54 |
+
def answer_question(message, history):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
if not docs:
|
| 56 |
+
return "⚠️ Please upload and load a PDF first."
|
| 57 |
+
|
| 58 |
+
question_embedding = embed_model.encode([message])
|
| 59 |
similarities = cosine_similarity(question_embedding, doc_embeddings)[0]
|
| 60 |
top_indices = similarities.argsort()[-3:][::-1]
|
| 61 |
context = "\n".join([docs[i] for i in top_indices])
|
| 62 |
|
| 63 |
+
prompt = f"""You are a helpful assistant. Use the context below to answer the question.\n\nContext:\n{context}\n\nQuestion: {message}\nAnswer:"""
|
|
|
|
| 64 |
output = llm(prompt=prompt, max_tokens=512, temperature=0.7)
|
| 65 |
return output['choices'][0]['text'].strip()
|
| 66 |
|
| 67 |
# -----------------------------------
|
| 68 |
+
# Gradio Interface
|
| 69 |
# -----------------------------------
|
| 70 |
|
| 71 |
with gr.Blocks() as demo:
|
| 72 |
+
gr.Markdown("## 🤖 Chat with your PDF using Mistral GGUF")
|
| 73 |
|
| 74 |
+
pdf_file = gr.File(label="📄 Upload your PDF", file_types=[".pdf"])
|
| 75 |
+
load_button = gr.Button("🔄 Load PDF")
|
| 76 |
+
|
| 77 |
+
load_status = gr.Textbox(label="", interactive=False)
|
| 78 |
+
|
| 79 |
+
chat = gr.ChatInterface(fn=answer_question)
|
| 80 |
+
|
| 81 |
+
load_button.click(fn=load_pdf, inputs=pdf_file, outputs=load_status)
|
| 82 |
|
| 83 |
+
# 💡 This must be at the top level for Spaces to detect:
|
| 84 |
demo.launch()
|