File size: 5,710 Bytes
d8ac93a e7b7dea 0f7b905 d8ac93a 9d4d039 d8ac93a 2b8f99e 0f7b905 9d4d039 3f90631 0f7b905 3f90631 0f7b905 e7b7dea 0f7b905 13dc13c 0f7b905 13dc13c 604263d 13dc13c 0f7b905 f68e936 0f7b905 604263d 9d4d039 604263d e7b7dea 0f7b905 604263d 9d4d039 f68e936 37fdd8f f68e936 37fdd8f f68e936 fca0f92 f68e936 13dc13c 37fdd8f 13dc13c 37fdd8f 0ba1d06 37fdd8f 604263d 13dc13c 37fdd8f 0ba1d06 37fdd8f f68e936 0ba1d06 13dc13c 4839e5c 37fdd8f f68e936 0ba1d06 37fdd8f 0ba1d06 f68e936 0ba1d06 f68e936 fca0f92 f68e936 0ba1d06 f68e936 0ba1d06 f68e936 0ba1d06 37fdd8f 0ba1d06 37fdd8f 0ba1d06 37fdd8f f68e936 0ba1d06 f68e936 0ba1d06 37fdd8f fca0f92 0ba1d06 37fdd8f 0ba1d06 37fdd8f 0ba1d06 37fdd8f 0ba1d06 37fdd8f 0ba1d06 f68e936 fca0f92 f68e936 37fdd8f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 | 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("""
<div class="header-card">
<h1 style="color:#E96A8D;">πΈ Femory</h1>
<p>Your reproductive health companion</p>
</div>
""")
with gr.Row():
with gr.Column(scale=1):
gr.Markdown("### π Common Topics")
pmos_btn = gr.Button("PMOS")
endo_btn = gr.Button("Endometriosis")
adeno_btn = gr.Button("Adenomyosis")
fibroid_btn = gr.Button("Fibroids")
gr.Markdown("""
### π· Safe Space
- Symptoms
- Treatments
- Diagnoses
- Women's health conditions
""")
with gr.Column(scale=3):
gr.HTML("""
<div class="main-panel">
<h2 style="color:#E96A8D;">Chat with Femory</h2>
<p>Ask questions about symptoms, treatments, and reproductive health.</p>
</div>
""")
chatbot = gr.Chatbot(height=500)
with gr.Row():
painful_btn = gr.Button("Painful Periods")
heavy_btn = gr.Button("Heavy Bleeding")
irregular_btn = gr.Button("Irregular Cycle")
msg = gr.Textbox(
placeholder="Ask a reproductive health question...",
show_label=False
)
send = gr.Button("Send")
with gr.Column(scale=1):
gr.HTML("""
<div class="condition-card">
<h4>π PMOS</h4>
Hormonal condition that can affect periods, skin, hair growth, and fertility.
</div>
<div class="condition-card">
<h4>π Endometriosis</h4>
Tissue similar to the uterine lining grows outside the uterus.
</div>
<div class="condition-card">
<h4>πΈ Adenomyosis</h4>
Tissue grows into the muscle wall of the uterus.
</div>
<div class="condition-card">
<h4>π©· Fibroids</h4>
Noncancerous growths that can develop in or around the uterus.
</div>
""")
send.click(chat, inputs=[msg, chatbot], outputs=[chatbot, msg])
msg.submit(chat, inputs=[msg, chatbot], outputs=[chatbot, msg])
pmos_btn.click(fill_pmos, outputs=msg)
endo_btn.click(fill_endometriosis, outputs=msg)
adeno_btn.click(fill_adenomyosis, outputs=msg)
fibroid_btn.click(fill_fibroids, outputs=msg)
painful_btn.click(fill_painful_periods, outputs=msg)
heavy_btn.click(fill_heavy_bleeding, outputs=msg)
irregular_btn.click(fill_irregular_cycle, outputs=msg)
demo.launch(css=custom_css, theme=gr.themes.Soft()) |