Delete app-old.py
Browse files- app-old.py +0 -95
app-old.py
DELETED
|
@@ -1,95 +0,0 @@
|
|
| 1 |
-
import streamlit as st # for web UI creation
|
| 2 |
-
from sentence_transformers import SentenceTransformer # this is for embedding queries into dense vectors
|
| 3 |
-
from pinecone import Pinecone, ServerlessSpec # for accessing pinecone vector DB
|
| 4 |
-
import os # for readhing environment variable
|
| 5 |
-
from langchain_huggingface import HuggingFaceEndpoint # for accessing HuggingFace inference endpoint
|
| 6 |
-
from langchain.prompts import PromptTemplate
|
| 7 |
-
import firebase_admin # for access to firebase
|
| 8 |
-
from firebase_admin import credentials, firestore
|
| 9 |
-
|
| 10 |
-
# === Load .env.local config file ===
|
| 11 |
-
from dotenv import load_dotenv
|
| 12 |
-
load_dotenv(".env.local")
|
| 13 |
-
|
| 14 |
-
# === CONFIG ===
|
| 15 |
-
PINECONE_API_KEY = os.getenv("PINECONE_API_KEY")
|
| 16 |
-
PINECONE_ENV = os.getenv("PINECONE_ENV")
|
| 17 |
-
HF_TOKEN = os.getenv("HUGGINGFACEHUB_API_TOKEN")
|
| 18 |
-
USER_ID = "demo_user" # static user for testing
|
| 19 |
-
|
| 20 |
-
# === Firebase Setup ===
|
| 21 |
-
if not firebase_admin._apps:
|
| 22 |
-
cred = credentials.Certificate("service-account-key.json")
|
| 23 |
-
firebase_admin.initialize_app(cred)
|
| 24 |
-
db = firestore.client()
|
| 25 |
-
|
| 26 |
-
# === Setup Pinecone & Embedding Model ===
|
| 27 |
-
pc = Pinecone(api_key=PINECONE_API_KEY)
|
| 28 |
-
INDEX_NAME = "clinical-trials-rag-vector"
|
| 29 |
-
index = pc.Index(INDEX_NAME)
|
| 30 |
-
|
| 31 |
-
embed_model = SentenceTransformer("pritamdeka/BioBERT-mnli-snli-scinli-scitail-mednli-stsb") # BioBERT sentence transformer model
|
| 32 |
-
|
| 33 |
-
# === LLM Part ===
|
| 34 |
-
llm = HuggingFaceEndpoint(
|
| 35 |
-
endpoint_url="https://bjgtatzbe3uoqewn.us-east-2.aws.endpoints.huggingface.cloud", # Inference Endpoint Built from Hugging Face. Pay per hour.
|
| 36 |
-
huggingfacehub_api_token=os.getenv("HUGGINGFACEHUB_API_TOKEN"),
|
| 37 |
-
temperature=0.7,
|
| 38 |
-
max_new_tokens=256
|
| 39 |
-
)
|
| 40 |
-
# prompt template example
|
| 41 |
-
prompt_template = PromptTemplate.from_template(
|
| 42 |
-
"Context:\n{context}\n\nQuestion: {question}\nAnswer:"
|
| 43 |
-
)
|
| 44 |
-
|
| 45 |
-
# === Tabs ===
|
| 46 |
-
tab1, tab2 = st.tabs(["🔍 Ask question related to clinical trial", "⭐ Bookmarked Trials"])
|
| 47 |
-
|
| 48 |
-
# === TAB 1: Question Part ===
|
| 49 |
-
with tab1:
|
| 50 |
-
st.title("Clinical Trial Discovery - Ask a Question")
|
| 51 |
-
user_query = st.text_input("Enter your medical question or condition", "")
|
| 52 |
-
|
| 53 |
-
if user_query:
|
| 54 |
-
with st.spinner("Retrieving relevant trials..."):
|
| 55 |
-
vec = embed_model.encode(user_query).tolist()
|
| 56 |
-
results = index.query(vector=vec, top_k=5, include_metadata=True)
|
| 57 |
-
contexts = [r["metadata"]["text"] for r in results["matches"]]
|
| 58 |
-
nct_ids = [r["metadata"].get("nct_id", "") for r in results["matches"]]
|
| 59 |
-
|
| 60 |
-
joined_context = "\n".join(contexts)
|
| 61 |
-
prompt = prompt_template.format(context=joined_context, question=user_query)
|
| 62 |
-
|
| 63 |
-
with st.spinner("Generating answer..."):
|
| 64 |
-
answer = llm(prompt)
|
| 65 |
-
st.subheader("🧠 Answer:")
|
| 66 |
-
st.write(answer)
|
| 67 |
-
|
| 68 |
-
st.markdown("---")
|
| 69 |
-
st.subheader("📋 Related Clinical Trials")
|
| 70 |
-
|
| 71 |
-
for i, match in enumerate(results["matches"]):
|
| 72 |
-
meta = match["metadata"]
|
| 73 |
-
nct_id = meta.get("nct_id", f"chunk_{i}")
|
| 74 |
-
chunk_text = meta.get("text", "")[:400]
|
| 75 |
-
with st.expander(f"Trial: {nct_id}"):
|
| 76 |
-
st.write(chunk_text + "...")
|
| 77 |
-
if st.button(f"⭐ Bookmark {nct_id}", key=f"bookmark_{i}"):
|
| 78 |
-
db.collection("Users").document(USER_ID) .collection("Bookmarks").document(nct_id).set({
|
| 79 |
-
"nct_id": nct_id,
|
| 80 |
-
"text": chunk_text
|
| 81 |
-
})
|
| 82 |
-
st.success(f"Bookmarked {nct_id} to Firestore.")
|
| 83 |
-
|
| 84 |
-
# === TAB 2: Bookmarked Trials ===
|
| 85 |
-
with tab2:
|
| 86 |
-
st.title("⭐ Your Bookmarked Trials")
|
| 87 |
-
docs = db.collection("Users").document(USER_ID).collection("Bookmarks").stream()
|
| 88 |
-
bookmarks = [doc.to_dict() for doc in docs]
|
| 89 |
-
|
| 90 |
-
if not bookmarks:
|
| 91 |
-
st.info("You haven't bookmarked any trials yet.")
|
| 92 |
-
else:
|
| 93 |
-
for b in bookmarks:
|
| 94 |
-
with st.expander(f"{b['nct_id']}"):
|
| 95 |
-
st.write(b["text"])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|