new
Browse files- README.md +6 -8
- app.py +58 -0
- requirements.txt +2 -0
README.md
CHANGED
@@ -1,12 +1,10 @@
|
|
1 |
---
|
2 |
-
title:
|
3 |
-
emoji:
|
4 |
-
colorFrom:
|
5 |
-
colorTo:
|
6 |
sdk: streamlit
|
7 |
-
sdk_version: 1.42.
|
8 |
app_file: app.py
|
9 |
pinned: false
|
10 |
-
---
|
11 |
-
|
12 |
-
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
1 |
---
|
2 |
+
title: Neurology Chatbot
|
3 |
+
emoji: 🏃
|
4 |
+
colorFrom: purple
|
5 |
+
colorTo: green
|
6 |
sdk: streamlit
|
7 |
+
sdk_version: 1.42.1
|
8 |
app_file: app.py
|
9 |
pinned: false
|
10 |
+
---
|
|
|
|
app.py
ADDED
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from groq import Client
|
3 |
+
|
4 |
+
# API Key Configuration (Use environment variables in production)
|
5 |
+
GROQ_API_KEY = "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"
|
6 |
+
|
7 |
+
def main():
|
8 |
+
st.set_page_config(page_title="🧠 Neurology Chatbot", layout="wide")
|
9 |
+
st.title("🧠 Neurology AI Chatbot")
|
10 |
+
|
11 |
+
# Load chatbot model
|
12 |
+
@st.cache_resource
|
13 |
+
def load_model():
|
14 |
+
return Client(api_key=GROQ_API_KEY)
|
15 |
+
|
16 |
+
chatbot = load_model()
|
17 |
+
|
18 |
+
# Strict system prompt
|
19 |
+
system_prompt = {
|
20 |
+
"role": "system",
|
21 |
+
"content": ("You are a professional neurology assistant. Your role is to provide accurate and up-to-date medical insights related to neurological disorders, brain health, symptoms, treatments, and neuroscience research. Always ensure that responses are factual, empathetic, and professional. If a query is **unrelated to neurology** or medical concerns, politely redirect the user by saying: 'I specialize in neurology-related assistance. Let me know how I can help with your neurological health concerns.'"
|
22 |
+
)
|
23 |
+
}
|
24 |
+
|
25 |
+
# Initialize chat history
|
26 |
+
if "messages" not in st.session_state:
|
27 |
+
st.session_state["messages"] = [system_prompt]
|
28 |
+
|
29 |
+
# Display chat history (excluding system message)
|
30 |
+
for message in st.session_state["messages"]:
|
31 |
+
if message["role"] != "system":
|
32 |
+
with st.chat_message(message["role"]):
|
33 |
+
st.markdown(message["content"])
|
34 |
+
|
35 |
+
# User input
|
36 |
+
user_input = st.chat_input("Ask me anything about neurology...")
|
37 |
+
|
38 |
+
if user_input:
|
39 |
+
# Add user message to session state
|
40 |
+
st.session_state["messages"].append({"role": "user", "content": user_input})
|
41 |
+
with st.chat_message("user"):
|
42 |
+
st.markdown(user_input)
|
43 |
+
|
44 |
+
# Generate response with strict system prompt
|
45 |
+
response = chatbot.chat.completions.create(
|
46 |
+
model="llama-3.3-70b-versatile",
|
47 |
+
messages=[system_prompt] + st.session_state["messages"] # Always include system prompt
|
48 |
+
)
|
49 |
+
|
50 |
+
bot_response = response.choices[0].message.content
|
51 |
+
|
52 |
+
# Add assistant's response to session state
|
53 |
+
st.session_state["messages"].append({"role": "assistant", "content": bot_response})
|
54 |
+
with st.chat_message("assistant"):
|
55 |
+
st.markdown(bot_response)
|
56 |
+
|
57 |
+
if __name__ == "__main__":
|
58 |
+
main()
|
requirements.txt
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
streamlit
|
2 |
+
groq
|