Chat with Femory
Ask questions about symptoms, treatments, and reproductive health.
import gradio as gr from huggingface_hub import InferenceClient from sentence_transformers import SentenceTransformer from sklearn.metrics.pairwise import cosine_similarity import numpy as np client = InferenceClient(model="Qwen/Qwen2.5-7B-Instruct") with open("knowledge.txt", "r", encoding="utf-8") as file: knowledge_text = file.read() def preprocess_text(text): chunks = text.strip().split("\n") return [chunk.strip() for chunk in chunks if chunk.strip()] chunks = preprocess_text(knowledge_text) embedding_model = SentenceTransformer("all-MiniLM-L6-v2") chunk_embeddings = embedding_model.encode(chunks) def get_relevant_context(user_query, top_k=5): query_embedding = embedding_model.encode([user_query]) similarity_scores = cosine_similarity(query_embedding, chunk_embeddings)[0] top_indices = np.argsort(similarity_scores)[-top_k:][::-1] return "\n".join([chunks[i] for i in top_indices]) def respond(message, history): context = get_relevant_context(message) system_prompt = f""" You are Femory, a friendly reproductive health education chatbot. Use the context below to answer the user's question. Context: {context} Rules: - Explain in simple, supportive language. - Do not diagnose the user. - Do not claim to replace a doctor. - Only answer reproductive health education questions. - Always end with: "This is for educational purposes only. Please talk to a healthcare professional for medical advice." """ messages = [{"role": "system", "content": system_prompt}] if history: messages.extend(history) messages.append({"role": "user", "content": message}) response = client.chat_completion( messages=messages, max_tokens=600 ) return response.choices[0].message.content.strip() def chat(message, history): if history is None: history = [] bot_response = respond(message, history) history.append({"role": "user", "content": message}) history.append({"role": "assistant", "content": bot_response}) return history, "" def fill_pmos(): return "What is PMOS?" def fill_endometriosis(): return "What are symptoms of endometriosis?" def fill_adenomyosis(): return "What is adenomyosis?" def fill_fibroids(): return "What are fibroids?" def fill_painful_periods(): return "What can cause painful periods?" def fill_heavy_bleeding(): return "What can cause heavy bleeding?" def fill_irregular_cycle(): return "What can cause an irregular cycle?" custom_css = """ .gradio-container { background: #FDE7EC !important; } .header-card { background: white; border-radius: 22px; padding: 22px; margin-bottom: 20px; text-align: center; } .main-panel { background: white; border-radius: 20px; padding: 20px; } .condition-card { background: white; border-radius: 20px; padding: 15px; margin-bottom: 12px; } button { border-radius: 20px !important; } footer { display: none !important; } """ with gr.Blocks() as demo: gr.HTML("""
Your reproductive health companion
Ask questions about symptoms, treatments, and reproductive health.