Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,23 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
def respond(
|
| 6 |
message,
|
|
@@ -11,15 +28,18 @@ def respond(
|
|
| 11 |
top_p,
|
| 12 |
hf_token: gr.OAuthToken,
|
| 13 |
):
|
| 14 |
-
|
| 15 |
-
For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
|
| 16 |
-
"""
|
| 17 |
client = InferenceClient(token=hf_token.token, model="openai/gpt-oss-20b")
|
| 18 |
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
|
|
|
|
|
|
| 21 |
messages.extend(history)
|
| 22 |
-
|
| 23 |
messages.append({"role": "user", "content": message})
|
| 24 |
|
| 25 |
response = ""
|
|
@@ -47,16 +67,10 @@ chatbot = gr.ChatInterface(
|
|
| 47 |
respond,
|
| 48 |
type="messages",
|
| 49 |
additional_inputs=[
|
| 50 |
-
gr.Textbox(value="You are a
|
| 51 |
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 52 |
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 53 |
-
gr.Slider(
|
| 54 |
-
minimum=0.1,
|
| 55 |
-
maximum=1.0,
|
| 56 |
-
value=0.95,
|
| 57 |
-
step=0.05,
|
| 58 |
-
label="Top-p (nucleus sampling)",
|
| 59 |
-
),
|
| 60 |
],
|
| 61 |
)
|
| 62 |
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import faiss
|
| 3 |
+
import pickle
|
| 4 |
+
from sentence_transformers import SentenceTransformer
|
| 5 |
+
import numpy as np
|
| 6 |
from huggingface_hub import InferenceClient
|
| 7 |
|
| 8 |
+
index = faiss.read_index("alzheimers_index.faiss")
|
| 9 |
+
|
| 10 |
+
with open("chunks.pkl", "rb") as f:
|
| 11 |
+
chunks = pickle.load(f)
|
| 12 |
+
|
| 13 |
+
model = SentenceTransformer("all-MiniLM-L6-v2")
|
| 14 |
+
|
| 15 |
+
def retrieve_rag_context(query, k=3):
|
| 16 |
+
"""Return top-k relevant chunks for a query."""
|
| 17 |
+
query_embedding = model.encode([query])
|
| 18 |
+
distances, indices = index.search(np.array(query_embedding), k)
|
| 19 |
+
results = "\n\n---\n\n".join([chunks[i]["text"] for i in indices[0]])
|
| 20 |
+
return results
|
| 21 |
|
| 22 |
def respond(
|
| 23 |
message,
|
|
|
|
| 28 |
top_p,
|
| 29 |
hf_token: gr.OAuthToken,
|
| 30 |
):
|
| 31 |
+
"""Respond using GPT-OSS-20B with RAG context"""
|
|
|
|
|
|
|
| 32 |
client = InferenceClient(token=hf_token.token, model="openai/gpt-oss-20b")
|
| 33 |
|
| 34 |
+
# Retrieve RAG context
|
| 35 |
+
rag_context = retrieve_rag_context(message)
|
| 36 |
+
|
| 37 |
+
# Combine system message with RAG context
|
| 38 |
+
full_system_message = f"{system_message}\n\nRelevant info from knowledge base:\n{rag_context}"
|
| 39 |
|
| 40 |
+
# Prepare messages
|
| 41 |
+
messages = [{"role": "system", "content": full_system_message}]
|
| 42 |
messages.extend(history)
|
|
|
|
| 43 |
messages.append({"role": "user", "content": message})
|
| 44 |
|
| 45 |
response = ""
|
|
|
|
| 67 |
respond,
|
| 68 |
type="messages",
|
| 69 |
additional_inputs=[
|
| 70 |
+
gr.Textbox(value="You are a helpful AI assistant for Alzheimer's patients and caregivers.", label="System message"),
|
| 71 |
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 72 |
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 73 |
+
gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
],
|
| 75 |
)
|
| 76 |
|