|
|
import streamlit as st |
|
|
import os |
|
|
from dotenv import load_dotenv |
|
|
from groq import Groq |
|
|
import re |
|
|
|
|
|
|
|
|
load_dotenv() |
|
|
API_KEY = ("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") |
|
|
|
|
|
|
|
|
if not API_KEY: |
|
|
st.error("GROQ_API_KEY is missing. Set it as an environment variable or in a .env file.") |
|
|
st.stop() |
|
|
|
|
|
|
|
|
client = Groq(api_key=API_KEY) |
|
|
|
|
|
|
|
|
SYSTEM_PROMPT = """ |
|
|
You are an AI-powered Gynecologist, designed to provide accurate, science-backed information about women's health, |
|
|
pregnancy, menstrual cycles, fertility, and reproductive health. |
|
|
You assist users by answering questions about gynecological health, prenatal care, contraception, and hormonal balance. |
|
|
You do NOT provide medical diagnoses or prescriptions. |
|
|
If a user asks about serious medical conditions, always recommend consulting a healthcare professional. |
|
|
Maintain a professional, supportive, and non-judgmental tone. |
|
|
""" |
|
|
|
|
|
|
|
|
st.set_page_config(page_title="👩⚕️ Gynecologist Chatbot", page_icon="💖") |
|
|
|
|
|
st.title("👩⚕️ AI Gynecologist Chatbot") |
|
|
st.write("Ask me anything about women's health, pregnancy, periods, or reproductive health!") |
|
|
|
|
|
|
|
|
if "messages" not in st.session_state: |
|
|
st.session_state.messages = [{"role": "system", "content": SYSTEM_PROMPT}] |
|
|
|
|
|
|
|
|
def is_gynecology_related(question): |
|
|
keywords = [ |
|
|
"pregnancy", "menstrual", "fertility", "ovulation", "contraception", "hormones", "reproductive", |
|
|
"periods", "gynecologist", "women's health", "vaginal", "uterus", "cervix", "PCOS", "fibroids", |
|
|
"ovaries", "menopause", "endometriosis", "prenatal", "postpartum", "STI", "sexual health" |
|
|
] |
|
|
return any(re.search(rf"\b{kw}\b", question, re.IGNORECASE) for kw in keywords) |
|
|
|
|
|
|
|
|
for message in st.session_state.messages[1:]: |
|
|
with st.chat_message(message["role"]): |
|
|
st.write(message["content"]) |
|
|
|
|
|
|
|
|
user_input = st.chat_input("Type your question here...") |
|
|
|
|
|
if user_input: |
|
|
if is_gynecology_related(user_input): |
|
|
|
|
|
st.session_state.messages.append({"role": "user", "content": user_input}) |
|
|
|
|
|
|
|
|
try: |
|
|
response = client.chat.completions.create( |
|
|
model="llama-3.3-70b-versatile", |
|
|
messages=st.session_state.messages |
|
|
) |
|
|
|
|
|
|
|
|
ai_response = response.choices[0].message.content |
|
|
|
|
|
with st.chat_message("user"): |
|
|
st.markdown(f"**You:** {user_input}") |
|
|
|
|
|
|
|
|
with st.chat_message("assistant"): |
|
|
st.write(ai_response) |
|
|
|
|
|
|
|
|
st.session_state.messages.append({"role": "assistant", "content": ai_response}) |
|
|
|
|
|
except Exception as e: |
|
|
st.error(f"Error: {e}") |
|
|
else: |
|
|
st.warning("Please ask a question related to gynecology, women's health, pregnancy, or reproductive health.") |
|
|
|