logeswari's picture
Update app.py
281da49 verified
import streamlit as st
from dotenv import load_dotenv
from groq import Client
# Load environment variables from .env file
load_dotenv()
GROQ_API_KEY = "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"
def main():
st.set_page_config(page_title="Gynecology Consultation Chatbot", layout="wide")
st.title("🩺 Gynecology Consultation Assistant")
# Load chatbot model
@st.cache_resource
def load_chatbot():
return Client(api_key=GROQ_API_KEY)
assistant = load_chatbot()
# Initialize chat history
if "messages" not in st.session_state:
st.session_state["messages"] = [{"role": "system", "content": "You are a professional gynecologist assistant. Your role is to provide accurate and up-to-date medical advice related to women's health, pregnancy, menstrual cycles, reproductive health, and gynecological concerns. Always ensure that responses are factual, empathetic, and professional. If a query is **unrelated to gynecology or medical concerns**, politely redirect the user by saying: 'I specialize in gynecology-related assistance. Let me know how I can help with your health concerns.'"}]
# Sidebar for chat history
with st.sidebar:
st.header("Chat History")
for message in st.session_state["messages"]:
if message["role"] != "system":
st.text_area(label=message["role"].capitalize(), value=message["content"], height=100, disabled=True)
# User input
user_input = st.chat_input("How can I assist you with your gynecological concerns?")
if user_input:
# Add user message to session state
st.session_state["messages"].append({"role": "user", "content": user_input})
with st.chat_message("user"):
st.markdown(user_input)
# Generate response with full conversation history
response = assistant.chat.completions.create(
model="llama-3.3-70b-versatile",
messages=st.session_state["messages"] # Pass entire chat history
)
assistant_reply = response.choices[0].message.content
# Add assistant's response to session state
st.session_state["messages"].append({"role": "assistant", "content": assistant_reply})
with st.chat_message("assistant"):
st.markdown(assistant_reply)
if __name__ == "__main__":
main()