| import gradio as gr |
| from sentence_transformers import SentenceTransformer, util |
| from transformers import pipeline |
| |
| |
| with open("info.txt", "r") as f: |
| docs = [line.strip() for line in f.readlines() if line.strip()] |
| |
| |
| |
| embedder = SentenceTransformer("all-MiniLM-L6-v2") |
| doc_embeddings = embedder.encode(docs, convert_to_tensor=True) |
| |
| |
| qa_pipeline = pipeline("text-generation", model="gpt2") |
| |
| |
| def answer_question(user_question): |
| if not user_question.strip(): |
| return "Please type a question." |
| |
| |
| question_embedding = embedder.encode(user_question, convert_to_tensor=True) |
| scores = util.cos_sim(question_embedding, doc_embeddings)[0] |
| best_index = scores.argmax().item() |
| retrieved_doc = docs[best_index] |
| |
| |
| prompt = ( |
| f"Information: {retrieved_doc}\n\n" |
| f"Question: {user_question}\n\n" |
| f"Answer:" |
| ) |
| |
| output = qa_pipeline(prompt, max_new_tokens=150, do_sample=False)[0]["generated_text"] |
| |
| |
| answer = output[len(prompt):].strip() |
| |
| return f"Answer:\n{answer}\n\n---\nSource used:\n{retrieved_doc}" |
| |
| |
| demo = gr.Interface( |
| fn=answer_question, |
| inputs=gr.Textbox( |
| lines=2, |
| placeholder="e.g. How does racial bias affect Alzheimer's research?", |
| label="Your question" |
| ), |
| outputs=gr.Textbox(label="Response", lines=10), |
| title="AI Bias in Neurodegeneration Research — RAG Chatbot", |
| description=( |
| "Ask questions about racial bias in neurodegenerative disease research. " |
| "This chatbot retrieves relevant information from a curated document set " |
| "and generates a grounded answer." |
| ), |
| examples=[ |
| ["How does racial bias affect Alzheimer's research?"], |
| ["Are Black patients underrepresented in Parkinson's clinical trials?"], |
| ["What can AI do to reduce bias in neurodegeneration studies?"], |
| ] |
| ) |
| |
| demo.launch() |
|
|