Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,14 +1,17 @@
|
|
1 |
import os
|
2 |
import gradio as gr
|
|
|
3 |
from llama_index.core import (
|
4 |
SimpleDirectoryReader,
|
5 |
VectorStoreIndex,
|
6 |
StorageContext,
|
7 |
Settings
|
8 |
)
|
9 |
-
from llama_index.embeddings.
|
10 |
-
from llama_index.core.node_parser import SentenceSplitter
|
11 |
from llama_index.vector_stores.faiss import FaissVectorStore
|
|
|
|
|
|
|
12 |
import faiss
|
13 |
|
14 |
# ====== Configuration ======
|
@@ -16,11 +19,23 @@ PDF_DIR = "./data"
|
|
16 |
INDEX_SAVE_PATH = "./saved_index"
|
17 |
CHUNK_SIZE = 512
|
18 |
CHUNK_OVERLAP = 50
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
|
20 |
-
#
|
21 |
-
|
22 |
-
|
23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
)
|
25 |
|
26 |
# ====== Node Parser ======
|
@@ -37,32 +52,46 @@ if os.path.exists(INDEX_SAVE_PATH):
|
|
37 |
index = VectorStoreIndex.load(storage_context=storage_context)
|
38 |
else:
|
39 |
# Create new index
|
|
|
|
|
|
|
40 |
documents = SimpleDirectoryReader(PDF_DIR).load_data()
|
41 |
nodes = parser.get_nodes_from_documents(documents)
|
42 |
|
43 |
# Create FAISS index
|
44 |
-
dimension =
|
45 |
faiss_index = faiss.IndexFlatL2(dimension)
|
46 |
vector_store = FaissVectorStore(faiss_index=faiss_index)
|
47 |
|
48 |
storage_context = StorageContext.from_defaults(vector_store=vector_store)
|
49 |
index = VectorStoreIndex(nodes, storage_context=storage_context)
|
50 |
|
51 |
-
# Save for
|
52 |
index.storage_context.persist(persist_dir=INDEX_SAVE_PATH)
|
53 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
54 |
# ====== Query Engine ======
|
55 |
query_engine = index.as_query_engine()
|
56 |
|
57 |
# ====== Gradio Interface ======
|
58 |
def ask_question(query):
|
59 |
-
response = query_engine.query(query)
|
60 |
-
return
|
61 |
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
|
|
|
1 |
import os
|
2 |
import gradio as gr
|
3 |
+
import torch
|
4 |
from llama_index.core import (
|
5 |
SimpleDirectoryReader,
|
6 |
VectorStoreIndex,
|
7 |
StorageContext,
|
8 |
Settings
|
9 |
)
|
10 |
+
from llama_index.embeddings.huggingface import HuggingFaceEmbedding
|
|
|
11 |
from llama_index.vector_stores.faiss import FaissVectorStore
|
12 |
+
from llama_index.llms.huggingface import HuggingFaceLLM
|
13 |
+
from llama_index.core.node_parser import SentenceSplitter
|
14 |
+
from transformers import AutoTokenizer
|
15 |
import faiss
|
16 |
|
17 |
# ====== Configuration ======
|
|
|
19 |
INDEX_SAVE_PATH = "./saved_index"
|
20 |
CHUNK_SIZE = 512
|
21 |
CHUNK_OVERLAP = 50
|
22 |
+
EMBED_MODEL = "sentence-transformers/paraphrase-multilingual-MiniLM-L12-v2"
|
23 |
+
LLM_MODEL = "microsoft/Phi-3-mini-4k-instruct" # 3.8B parameter model
|
24 |
+
|
25 |
+
# ====== Initialize Local Models ======
|
26 |
+
# Embedding model (runs offline)
|
27 |
+
Settings.embed_model = HuggingFaceEmbedding(model_name=EMBED_MODEL)
|
28 |
|
29 |
+
# Local LLM with 4-bit quantization
|
30 |
+
tokenizer = AutoTokenizer.from_pretrained(LLM_MODEL)
|
31 |
+
Settings.llm = HuggingFaceLLM(
|
32 |
+
model_name=LLM_MODEL,
|
33 |
+
tokenizer_name=LLM_MODEL,
|
34 |
+
device_map="auto",
|
35 |
+
model_kwargs={
|
36 |
+
"torch_dtype": torch.float16,
|
37 |
+
"trust_remote_code": True
|
38 |
+
}
|
39 |
)
|
40 |
|
41 |
# ====== Node Parser ======
|
|
|
52 |
index = VectorStoreIndex.load(storage_context=storage_context)
|
53 |
else:
|
54 |
# Create new index
|
55 |
+
if not os.path.exists(PDF_DIR):
|
56 |
+
raise FileNotFoundError(f"Add medical PDFs to {PDF_DIR} directory first")
|
57 |
+
|
58 |
documents = SimpleDirectoryReader(PDF_DIR).load_data()
|
59 |
nodes = parser.get_nodes_from_documents(documents)
|
60 |
|
61 |
# Create FAISS index
|
62 |
+
dimension = 384 # Match MiniLM embedding size
|
63 |
faiss_index = faiss.IndexFlatL2(dimension)
|
64 |
vector_store = FaissVectorStore(faiss_index=faiss_index)
|
65 |
|
66 |
storage_context = StorageContext.from_defaults(vector_store=vector_store)
|
67 |
index = VectorStoreIndex(nodes, storage_context=storage_context)
|
68 |
|
69 |
+
# Save for offline use
|
70 |
index.storage_context.persist(persist_dir=INDEX_SAVE_PATH)
|
71 |
|
72 |
+
# ====== Safety Layers ======
|
73 |
+
def validate_response(response: str) -> str:
|
74 |
+
"""Implements WHO protocol constraints"""
|
75 |
+
if len(response.split('\n')) > 6:
|
76 |
+
return "⚠️ Protocol too complex - must be <6 steps\n\n" + response
|
77 |
+
uncertainty_phrases = ["I think", "maybe", "not sure", "غير متأكد"]
|
78 |
+
if any(phrase in response for phrase in uncertainty_phrases):
|
79 |
+
return "⚠️ Consult supervisor - uncertain response\n\n" + response
|
80 |
+
return response
|
81 |
+
|
82 |
# ====== Query Engine ======
|
83 |
query_engine = index.as_query_engine()
|
84 |
|
85 |
# ====== Gradio Interface ======
|
86 |
def ask_question(query):
|
87 |
+
response = str(query_engine.query(query))
|
88 |
+
return validate_response(response)
|
89 |
|
90 |
+
if __name__ == "__main__":
|
91 |
+
gr.Interface(
|
92 |
+
fn=ask_question,
|
93 |
+
inputs=gr.Textbox(lines=2, placeholder="Ask a medical question..."),
|
94 |
+
outputs="text",
|
95 |
+
title="🩺 Gaza Field Medic Assistant (Offline)",
|
96 |
+
description="WHO protocols • No internet required • Arabic/English"
|
97 |
+
).launch(server_name="0.0.0.0")
|