Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +26 -75
src/streamlit_app.py
CHANGED
|
@@ -3,106 +3,57 @@ import os
|
|
| 3 |
from google import genai
|
| 4 |
from google.genai import types
|
| 5 |
|
| 6 |
-
# --- 1.
|
| 7 |
-
st.set_page_config(
|
| 8 |
-
page_title="Abyssinia Intelligence | Healthcare AI",
|
| 9 |
-
page_icon="π₯",
|
| 10 |
-
layout="wide"
|
| 11 |
-
)
|
| 12 |
|
| 13 |
-
# Custom Medical CSS
|
| 14 |
-
st.markdown("""
|
| 15 |
-
<style>
|
| 16 |
-
.stChatMessage { border-radius: 10px; margin-bottom: 10px; }
|
| 17 |
-
.stAlert { border-radius: 10px; }
|
| 18 |
-
</style>
|
| 19 |
-
""", unsafe_allow_html=True)
|
| 20 |
-
|
| 21 |
-
# --- 2. INITIALIZATION & SECURITY ---
|
| 22 |
api_key = os.environ.get("GOOGLE_API_KEY")
|
| 23 |
-
|
| 24 |
-
if not api_key:
|
| 25 |
-
st.error("π API Key Missing: Please add 'GOOGLE_API_KEY' to your Hugging Face Secrets.")
|
| 26 |
-
st.stop()
|
| 27 |
-
|
| 28 |
-
# Initialize the 2026 unified Client
|
| 29 |
client = genai.Client(api_key=api_key)
|
| 30 |
|
| 31 |
-
# Initialize Chat History in Session State if it doesn't exist
|
| 32 |
if "messages" not in st.session_state:
|
| 33 |
st.session_state.messages = []
|
| 34 |
|
| 35 |
-
#
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
Your goal is to assist users with medical inquiries by providing evidence-based,
|
| 39 |
-
empathetic, and structured health information.
|
| 40 |
-
|
| 41 |
-
Rules:
|
| 42 |
-
1. ALWAYS start the first response with a medical disclaimer.
|
| 43 |
-
2. If symptoms sound life-threatening (chest pain, stroke signs), urge immediate emergency care.
|
| 44 |
-
3. Ask follow-up questions to understand the context (e.g., duration, severity).
|
| 45 |
-
4. Do not provide definitive diagnoses; provide 'possible considerations'.
|
| 46 |
-
"""
|
| 47 |
|
| 48 |
-
|
| 49 |
-
with st.
|
| 50 |
-
|
| 51 |
-
st.info("Your secure, back-and-forth healthcare consultation partner.")
|
| 52 |
-
if st.button("Clear Consultation History"):
|
| 53 |
-
st.session_state.messages = []
|
| 54 |
-
st.rerun()
|
| 55 |
-
|
| 56 |
-
st.markdown("---")
|
| 57 |
-
st.markdown("**Status:** π’ Connected to Gemini 2.0")
|
| 58 |
-
st.markdown("**Region:** Ethiopia/Global")
|
| 59 |
|
| 60 |
-
#
|
| 61 |
-
|
| 62 |
-
with st.chat_message(message["role"]):
|
| 63 |
-
st.markdown(message["content"])
|
| 64 |
-
|
| 65 |
-
# --- 4. CHAT LOGIC ---
|
| 66 |
-
if prompt := st.chat_input("Describe your symptoms or ask a health question..."):
|
| 67 |
-
|
| 68 |
-
# 1. Add user message to UI and State
|
| 69 |
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 70 |
with st.chat_message("user"):
|
| 71 |
st.markdown(prompt)
|
| 72 |
|
| 73 |
-
# 2. Generate AI Response
|
| 74 |
with st.chat_message("assistant"):
|
| 75 |
-
message_placeholder = st.empty()
|
| 76 |
-
|
| 77 |
try:
|
| 78 |
-
# Map history
|
| 79 |
history_bundle = [
|
| 80 |
types.Content(role=m["role"], parts=[types.Part(text=m["content"])])
|
| 81 |
-
for m in st.session_state.messages[:-1]
|
| 82 |
]
|
| 83 |
|
| 84 |
-
# Use the new chat interface
|
| 85 |
chat = client.chats.create(
|
| 86 |
-
model="gemini-
|
| 87 |
config=types.GenerateContentConfig(
|
| 88 |
-
system_instruction=
|
| 89 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
),
|
| 91 |
history=history_bundle
|
| 92 |
)
|
| 93 |
|
| 94 |
-
# Request generation
|
| 95 |
response = chat.send_message(prompt)
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
message_placeholder.markdown(full_response)
|
| 99 |
-
|
| 100 |
-
# 3. Add assistant response to State
|
| 101 |
-
st.session_state.messages.append({"role": "assistant", "content": full_response})
|
| 102 |
|
| 103 |
except Exception as e:
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
st.
|
| 108 |
-
st.caption("Disclaimer: Abyssinia Intelligence is an educational AI tool. It does not provide medical diagnoses or prescriptions.")
|
|
|
|
| 3 |
from google import genai
|
| 4 |
from google.genai import types
|
| 5 |
|
| 6 |
+
# --- 1. CONFIG ---
|
| 7 |
+
st.set_page_config(page_title="Abyssinia Intelligence 3.0", page_icon="π₯")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
api_key = os.environ.get("GOOGLE_API_KEY")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
client = genai.Client(api_key=api_key)
|
| 11 |
|
|
|
|
| 12 |
if "messages" not in st.session_state:
|
| 13 |
st.session_state.messages = []
|
| 14 |
|
| 15 |
+
# --- 2. UI ---
|
| 16 |
+
st.title("π₯ Abyssinia Intelligence 3.0")
|
| 17 |
+
st.caption("Now powered by Gemini 3.0 Flash with Agentic Reasoning")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
+
for msg in st.session_state.messages:
|
| 20 |
+
with st.chat_message(msg["role"]):
|
| 21 |
+
st.markdown(msg["content"])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
+
# --- 3. LOGIC ---
|
| 24 |
+
if prompt := st.chat_input("How can I help you today?"):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 26 |
with st.chat_message("user"):
|
| 27 |
st.markdown(prompt)
|
| 28 |
|
|
|
|
| 29 |
with st.chat_message("assistant"):
|
|
|
|
|
|
|
| 30 |
try:
|
| 31 |
+
# Map history for the 2026 SDK
|
| 32 |
history_bundle = [
|
| 33 |
types.Content(role=m["role"], parts=[types.Part(text=m["content"])])
|
| 34 |
+
for m in st.session_state.messages[:-1]
|
| 35 |
]
|
| 36 |
|
|
|
|
| 37 |
chat = client.chats.create(
|
| 38 |
+
model="gemini-3-flash-preview", # <-- UPGRADED MODEL
|
| 39 |
config=types.GenerateContentConfig(
|
| 40 |
+
system_instruction="You are Abyssinia Intelligence. Use advanced medical reasoning.",
|
| 41 |
+
# Setting thinking to 'low' helps avoid the 429 quota errors
|
| 42 |
+
# by reducing the computational load for simple chat turns.
|
| 43 |
+
thinking_config=types.ThinkingConfig(
|
| 44 |
+
thinking_level=types.ThinkingLevel.LOW
|
| 45 |
+
),
|
| 46 |
+
temperature=0.3
|
| 47 |
),
|
| 48 |
history=history_bundle
|
| 49 |
)
|
| 50 |
|
|
|
|
| 51 |
response = chat.send_message(prompt)
|
| 52 |
+
st.markdown(response.text)
|
| 53 |
+
st.session_state.messages.append({"role": "assistant", "content": response.text})
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
|
| 55 |
except Exception as e:
|
| 56 |
+
if "429" in str(e):
|
| 57 |
+
st.error("π¨ **Quota Exceeded.** Gemini 3.0 Flash is in high demand. Please wait 10 seconds.")
|
| 58 |
+
else:
|
| 59 |
+
st.error(f"Error: {e}")
|
|
|