Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,76 +7,92 @@ import google.generativeai as genai
|
|
| 7 |
import re
|
| 8 |
import os
|
| 9 |
|
| 10 |
-
# Load
|
| 11 |
-
def
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
|
|
|
| 17 |
|
| 18 |
-
docs_df, index =
|
| 19 |
|
| 20 |
-
#
|
| 21 |
-
|
| 22 |
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
|
|
|
| 28 |
|
| 29 |
-
#
|
| 30 |
-
def
|
| 31 |
text = text.lower()
|
| 32 |
-
text =
|
| 33 |
-
|
|
|
|
|
|
|
| 34 |
|
| 35 |
-
# Retrieve
|
| 36 |
-
def
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
|
|
|
|
|
|
| 40 |
|
| 41 |
-
# RAG
|
| 42 |
-
def
|
| 43 |
-
|
| 44 |
-
|
| 45 |
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
f"
|
| 54 |
-
f"
|
| 55 |
-
f"
|
| 56 |
-
f"
|
| 57 |
-
f"- End with a short, clear recommendation (if context permits).\n"
|
| 58 |
-
f"- Avoid medical advice unless the context contains it."
|
| 59 |
)
|
| 60 |
-
|
| 61 |
-
|
|
|
|
| 62 |
prompt,
|
| 63 |
generation_config=genai.types.GenerationConfig(
|
| 64 |
max_output_tokens=max_tokens,
|
| 65 |
-
temperature=
|
| 66 |
)
|
| 67 |
)
|
| 68 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
|
| 70 |
-
# Gradio interface
|
| 71 |
demo = gr.Interface(
|
| 72 |
-
fn=
|
| 73 |
inputs=[
|
| 74 |
-
gr.Textbox(label="
|
| 75 |
],
|
| 76 |
-
outputs=gr.Textbox(label="
|
| 77 |
-
title="
|
| 78 |
-
description="
|
| 79 |
)
|
| 80 |
|
| 81 |
if __name__ == "__main__":
|
| 82 |
-
demo.launch()
|
|
|
|
| 7 |
import re
|
| 8 |
import os
|
| 9 |
|
| 10 |
+
# Load data and FAISS index
|
| 11 |
+
def load_data_and_index():
|
| 12 |
+
docs_df = pd.read_pickle("data.pkl") # Adjust path for HF Spaces
|
| 13 |
+
embeddings = np.array(docs_df['embeddings'].tolist(), dtype=np.float32)
|
| 14 |
+
dimension = embeddings.shape[1]
|
| 15 |
+
index = faiss.IndexFlatL2(dimension)
|
| 16 |
+
index.add(embeddings)
|
| 17 |
+
return docs_df, index
|
| 18 |
|
| 19 |
+
docs_df, index = load_data_and_index()
|
| 20 |
|
| 21 |
+
# Load SentenceTransformer
|
| 22 |
+
minilm = SentenceTransformer('all-MiniLM-L6-v2')
|
| 23 |
|
| 24 |
+
# Configure Gemini API using Hugging Face Secrets
|
| 25 |
+
GEMINI_API_KEY = os.environ.get("GEMINI_API_KEY")
|
| 26 |
+
if not GEMINI_API_KEY:
|
| 27 |
+
raise ValueError("Gemini API key not found. Please set it in Hugging Face Spaces secrets.")
|
| 28 |
+
genai.configure(api_key=GEMINI_API_KEY)
|
| 29 |
+
model = genai.GenerativeModel('gemini-2.0-flash')
|
| 30 |
|
| 31 |
+
# Preprocess text function
|
| 32 |
+
def preprocess_text(text):
|
| 33 |
text = text.lower()
|
| 34 |
+
text = text.replace('\n', ' ').replace('\t', ' ')
|
| 35 |
+
text = re.sub(r'[^\w\s.,;:>-]', ' ', text)
|
| 36 |
+
text = ' '.join(text.split()).strip()
|
| 37 |
+
return text
|
| 38 |
|
| 39 |
+
# Retrieve documents
|
| 40 |
+
def retrieve_docs(query, k=5):
|
| 41 |
+
query_embedding = minilm.encode([query], show_progress_bar=False)[0].astype(np.float32)
|
| 42 |
+
distances, indices = index.search(np.array([query_embedding]), k)
|
| 43 |
+
retrieved_docs = docs_df.iloc[indices[0]][['label', 'text', 'source']]
|
| 44 |
+
retrieved_docs['distance'] = distances[0]
|
| 45 |
+
return retrieved_docs
|
| 46 |
|
| 47 |
+
# RAG pipeline integrated into respond function
|
| 48 |
+
def respond(message, system_message, max_tokens, temperature):
|
| 49 |
+
# Preprocess the user message
|
| 50 |
+
preprocessed_query = preprocess_text(message)
|
| 51 |
|
| 52 |
+
# Retrieve relevant documents
|
| 53 |
+
retrieved_docs = retrieve_docs(preprocessed_query, k=5)
|
| 54 |
+
context = "\n".join(retrieved_docs['text'].tolist())
|
| 55 |
+
|
| 56 |
+
# Construct the prompt with system message and RAG context, asking for structured response
|
| 57 |
+
prompt = f"{system_message}\n\n"
|
| 58 |
+
prompt += (
|
| 59 |
+
f"Query: {message}\n"
|
| 60 |
+
f"Relevant Context: {context}\n"
|
| 61 |
+
f"Generate a short, concise response to the query based only on the provided context. "
|
| 62 |
+
f"Format the response as a structured with headings and information write in the form of points not paragraph"
|
|
|
|
|
|
|
| 63 |
)
|
| 64 |
+
|
| 65 |
+
# Generate response with Gemini
|
| 66 |
+
response = model.generate_content(
|
| 67 |
prompt,
|
| 68 |
generation_config=genai.types.GenerationConfig(
|
| 69 |
max_output_tokens=max_tokens,
|
| 70 |
+
temperature=temperature
|
| 71 |
)
|
| 72 |
)
|
| 73 |
+
answer = response.text.strip()
|
| 74 |
+
if not answer.endswith('.'):
|
| 75 |
+
last_period = answer.rfind('.')
|
| 76 |
+
if last_period != -1:
|
| 77 |
+
answer = answer[:last_period + 1]
|
| 78 |
+
else:
|
| 79 |
+
answer += "."
|
| 80 |
+
|
| 81 |
+
return answer
|
| 82 |
+
|
| 83 |
+
# Simple Gradio Interface
|
| 84 |
+
def chatbot_interface(message, system_message, max_tokens, temperature):
|
| 85 |
+
return respond(message, system_message, max_tokens, temperature)
|
| 86 |
|
|
|
|
| 87 |
demo = gr.Interface(
|
| 88 |
+
fn=chatbot_interface,
|
| 89 |
inputs=[
|
| 90 |
+
gr.Textbox(label="Your Query", placeholder="Enter your medical question here..."),
|
| 91 |
],
|
| 92 |
+
outputs=gr.Textbox(label="Response"),
|
| 93 |
+
title="🏥 Medical Chat Assistant",
|
| 94 |
+
description="A simple medical assistant that diagnoses patient queries using AI and past records, providing structured responses."
|
| 95 |
)
|
| 96 |
|
| 97 |
if __name__ == "__main__":
|
| 98 |
+
demo.launch()
|