Spaces:
Sleeping
Sleeping
import streamlit as st | |
import os | |
import google.generativeai as genai | |
# Set your Google API Key directly or use Hugging Face Spaces' Secrets | |
genai.configure(api_key=os.getenv("GOOGLE_KEY")) # Make sure to set this in the Spaces secrets | |
model = genai.GenerativeModel("gemini-pro") | |
chat = model.start_chat(history=[]) | |
def get_gemini_response(prompt): | |
try: | |
chunks = [chunk.text for chunk in chat.send_message(prompt, stream=True)] # Collect response chunks | |
return " ".join(chunks) # Combine all chunks into a single string | |
except Exception as e: | |
return f"An error occurred: {str(e)}" | |
# Streamlit app configuration | |
st.set_page_config(page_title="Med ChatBot") | |
st.title("Medical ChatBot") | |
# Initialize session state for chat history | |
if "chat_history" not in st.session_state: | |
st.session_state["chat_history"] = [] | |
# Function to handle submission (without the button) | |
def submit_input(): | |
input_text = st.session_state["input"] | |
if input_text: | |
# Context for the LLM with history included | |
chat_history_text = " ".join([f"{role}: {text}" for role, text in st.session_state["chat_history"]]) | |
context = ( | |
"You are a friendly and knowledgeable medical chatbot here to help users understand their symptoms. " | |
"Provide clear, concise answers based on NHS guidelines, avoiding technical jargon. " | |
"If the user asks something unrelated to medical topics, politely respond with: " | |
"'I'm just a medical bot, so I can only assist with health-related questions.'" | |
f"Previous conversation: {chat_history_text} " | |
) | |
prompt = f"{context} User's latest input: {input_text}" # Include the latest user input | |
response = get_gemini_response(prompt) | |
# Add user query to session state chat history | |
st.session_state['chat_history'].append(("You", input_text)) | |
# Add bot response to session state chat history | |
if isinstance(response, list): | |
full_response = " ".join([chunk.text for chunk in response]) | |
st.session_state['chat_history'].append(("Bot", full_response)) | |
else: | |
st.session_state['chat_history'].append(("Bot", response)) | |
# Clear input field after submission | |
st.session_state["input"] = "" | |
# Chat input (without button, submit on "Enter") | |
st.text_input("Enter your message:", key="input", on_change=submit_input) | |
# Display chat history in a chatbox style | |
st.subheader("Chat History") | |
for role, text in st.session_state['chat_history']: | |
if role == "You": | |
st.markdown(f"<div style='text-align: right;'>{role}: {text}</div>", unsafe_allow_html=True) | |
else: | |
st.markdown(f"<div style='text-align: left;'>{role}: {text}</div>", unsafe_allow_html=True) | |