Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import chromadb
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import gradio as gr
|
| 4 |
+
from llama_cpp import Llama
|
| 5 |
+
|
| 6 |
+
# Load ChromaDB for symptom retrieval
|
| 7 |
+
chroma_client = chromadb.PersistentClient(path="./chroma_db")
|
| 8 |
+
collection = chroma_client.get_or_create_collection(name="symptom_tree")
|
| 9 |
+
|
| 10 |
+
# Load and insert CSV data into ChromaDB
|
| 11 |
+
csv_path = "/mnt/data/enhanced_symptom_tree_with_measures.csv"
|
| 12 |
+
df = pd.read_csv(csv_path)
|
| 13 |
+
for _, row in df.iterrows():
|
| 14 |
+
collection.add(
|
| 15 |
+
documents=[row["Primary Symptom"]],
|
| 16 |
+
metadatas={
|
| 17 |
+
"follow_up_questions": row["Follow-up Questions"],
|
| 18 |
+
"possible_diseases": row["Possible Diseases"],
|
| 19 |
+
"recommended_measures": row["Recommended Measures"]
|
| 20 |
+
},
|
| 21 |
+
ids=[row["Primary Symptom"]]
|
| 22 |
+
)
|
| 23 |
+
|
| 24 |
+
# Load Mistral 7B model (GGUF format for low-resource deployment)
|
| 25 |
+
llm = Llama(model_path="mistral-7b.Q4_K_M.gguf", n_ctx=2048)
|
| 26 |
+
|
| 27 |
+
def chatbot(symptom: str):
|
| 28 |
+
# Retrieve symptom details from ChromaDB
|
| 29 |
+
results = collection.query(query_texts=[symptom], n_results=1)
|
| 30 |
+
if not results["documents"]:
|
| 31 |
+
return "I'm sorry, I couldn't find relevant information."
|
| 32 |
+
|
| 33 |
+
metadata = results["metadatas"][0]
|
| 34 |
+
follow_up_qs = metadata["follow_up_questions"].split(";")
|
| 35 |
+
possible_diseases = metadata["possible_diseases"].split(";")
|
| 36 |
+
measures = metadata["recommended_measures"].split(";")
|
| 37 |
+
|
| 38 |
+
# Generate chatbot response using Mistral 7B
|
| 39 |
+
prompt = f"You are a doctor. A patient reports {symptom}. Ask 2 follow-up questions: {follow_up_qs}"
|
| 40 |
+
response = llm(prompt)["choices"][0]["text"]
|
| 41 |
+
|
| 42 |
+
return f"\n**Follow-up Questions:**\n {' '.join(follow_up_qs)}\n\n**Possible Diseases:**\n {' '.join(possible_diseases)}\n\n**Recommended Measures:**\n {' '.join(measures)}\n\n**Chatbot Response:**\n {response}"
|
| 43 |
+
|
| 44 |
+
# Create Gradio UI
|
| 45 |
+
demo = gr.Interface(fn=chatbot, inputs="text", outputs="markdown", title="Chest X-Ray Chatbot")
|
| 46 |
+
|
| 47 |
+
demo.launch(share=True)
|