Spaces:
Build error
Build error
Upload 2 files
Browse files- qachat.py +62 -0
- requirements.txt +3 -0
qachat.py
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from dotenv import load_dotenv
|
| 2 |
+
load_dotenv()
|
| 3 |
+
|
| 4 |
+
import streamlit as st
|
| 5 |
+
import os
|
| 6 |
+
import google.generativeai as genai
|
| 7 |
+
|
| 8 |
+
# Configure Google Generative AI
|
| 9 |
+
genai.configure(api_key=os.getenv("GOOGLE_KEY"))
|
| 10 |
+
model = genai.GenerativeModel("gemini-pro")
|
| 11 |
+
chat = model.start_chat(history=[])
|
| 12 |
+
|
| 13 |
+
def get_gemini_response(prompt):
|
| 14 |
+
try:
|
| 15 |
+
response = chat.send_message(prompt, stream=True)
|
| 16 |
+
return response
|
| 17 |
+
except Exception as e:
|
| 18 |
+
return f"An error occurred: {str(e)}"
|
| 19 |
+
|
| 20 |
+
# Streamlit app configuration
|
| 21 |
+
st.set_page_config(page_title = "Med ChatBot")
|
| 22 |
+
st.header = ("ChatBot App")
|
| 23 |
+
|
| 24 |
+
# Initialize session state for chat history
|
| 25 |
+
if "chat_history" not in st.session_state:
|
| 26 |
+
st.session_state["chat_history"] = []
|
| 27 |
+
|
| 28 |
+
# Input and submission button
|
| 29 |
+
input_text = st.text_input("Input: ", key="input")
|
| 30 |
+
submit = st.button("Ask the question")
|
| 31 |
+
|
| 32 |
+
if submit and input_text:
|
| 33 |
+
# Context for the LLM with history included
|
| 34 |
+
chat_history_text = " ".join([f"{role}: {text}" for role, text in st.session_state["chat_history"]])
|
| 35 |
+
|
| 36 |
+
context = (
|
| 37 |
+
"You are a medical chatbot designed to assist users in understanding their symptoms. "
|
| 38 |
+
"Provide clear, concise, and informative responses based on NHS guidelines. "
|
| 39 |
+
"Avoid technical jargon and code snippets. If asked a question unrelated to medical topics, "
|
| 40 |
+
"respond with: 'I am a medical bot and I don't have that knowledge.' "
|
| 41 |
+
f"Previous conversation: {chat_history_text} "
|
| 42 |
+
)
|
| 43 |
+
|
| 44 |
+
prompt = f"{context} User's latest input: {input_text}" # Include the latest user input
|
| 45 |
+
response = get_gemini_response(prompt)
|
| 46 |
+
|
| 47 |
+
# Add user query to session state chat history
|
| 48 |
+
st.session_state['chat_history'].append(("You", input_text))
|
| 49 |
+
|
| 50 |
+
st.subheader("The Response is")
|
| 51 |
+
if isinstance(response, str): # Handle cases where response might be a string
|
| 52 |
+
st.write(response)
|
| 53 |
+
st.session_state['chat_history'].append(("Bot", response))
|
| 54 |
+
else:
|
| 55 |
+
for chunk in response:
|
| 56 |
+
st.write(chunk.text)
|
| 57 |
+
st.session_state['chat_history'].append(("Bot", chunk.text))
|
| 58 |
+
|
| 59 |
+
st.subheader("The Chat History is")
|
| 60 |
+
|
| 61 |
+
for role, text in st.session_state['chat_history']:
|
| 62 |
+
st.write(f"{role}: {text}")
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
streamlit
|
| 2 |
+
google-generativeai
|
| 3 |
+
python-dotenv
|