logeswari commited on
Commit
ca91be6
·
1 Parent(s): 2d97d7b
Files changed (3) hide show
  1. 25.0.1 +0 -0
  2. See +0 -0
  3. frontend.py +84 -0
25.0.1 ADDED
File without changes
See ADDED
File without changes
frontend.py ADDED
@@ -0,0 +1,84 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import os
3
+ from dotenv import load_dotenv
4
+ from groq import Groq
5
+ import re
6
+
7
+ # Load environment variables from .env file
8
+ load_dotenv()
9
+ API_KEY = ("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")
10
+
11
+ # Check if API key is set
12
+ if not API_KEY:
13
+ st.error("GROQ_API_KEY is missing. Set it as an environment variable or in a .env file.")
14
+ st.stop()
15
+
16
+ # Initialize Groq client
17
+ client = Groq(api_key=API_KEY)
18
+
19
+ # System prompt for the Gynecologist Chatbot
20
+ SYSTEM_PROMPT = """
21
+ You are an AI-powered Gynecologist, designed to provide accurate, science-backed information about women's health,
22
+ pregnancy, menstrual cycles, fertility, and reproductive health.
23
+ You assist users by answering questions about gynecological health, prenatal care, contraception, and hormonal balance.
24
+ You do NOT provide medical diagnoses or prescriptions.
25
+ If a user asks about serious medical conditions, always recommend consulting a healthcare professional.
26
+ Maintain a professional, supportive, and non-judgmental tone.
27
+ """
28
+
29
+ # Streamlit UI
30
+ st.set_page_config(page_title="👩‍⚕️ Gynecologist Chatbot", page_icon="💖")
31
+
32
+ st.title("👩‍⚕️ AI Gynecologist Chatbot")
33
+ st.write("Ask me anything about women's health, pregnancy, periods, or reproductive health!")
34
+
35
+ # Initialize chat history
36
+ if "messages" not in st.session_state:
37
+ st.session_state.messages = [{"role": "system", "content": SYSTEM_PROMPT}]
38
+
39
+ # Function to check if the question is gynecology-related
40
+ def is_gynecology_related(question):
41
+ keywords = [
42
+ "pregnancy", "menstrual", "fertility", "ovulation", "contraception", "hormones", "reproductive",
43
+ "periods", "gynecologist", "women's health", "vaginal", "uterus", "cervix", "PCOS", "fibroids",
44
+ "ovaries", "menopause", "endometriosis", "prenatal", "postpartum", "STI", "sexual health"
45
+ ]
46
+ return any(re.search(rf"\b{kw}\b", question, re.IGNORECASE) for kw in keywords)
47
+
48
+ # Display chat history
49
+ for message in st.session_state.messages[1:]: # Skip system prompt in UI
50
+ with st.chat_message(message["role"]):
51
+ st.write(message["content"])
52
+
53
+ # User input
54
+ user_input = st.chat_input("Type your question here...")
55
+
56
+ if user_input:
57
+ if is_gynecology_related(user_input):
58
+ # Add user message to chat history
59
+ st.session_state.messages.append({"role": "user", "content": user_input})
60
+
61
+ # Send request to Groq API using LLaMA 3.3 70B Versatile model
62
+ try:
63
+ response = client.chat.completions.create(
64
+ model="llama-3.3-70b-versatile",
65
+ messages=st.session_state.messages
66
+ )
67
+
68
+ # Extract AI response
69
+ ai_response = response.choices[0].message.content
70
+
71
+ with st.chat_message("user"):
72
+ st.markdown(f"**You:** {user_input}")
73
+
74
+ # Display AI response
75
+ with st.chat_message("assistant"):
76
+ st.write(ai_response)
77
+
78
+ # Add AI response to chat history
79
+ st.session_state.messages.append({"role": "assistant", "content": ai_response})
80
+
81
+ except Exception as e:
82
+ st.error(f"Error: {e}")
83
+ else:
84
+ st.warning("Please ask a question related to gynecology, women's health, pregnancy, or reproductive health.")