Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,21 +2,41 @@ from groq import Groq
|
|
| 2 |
import streamlit as st
|
| 3 |
import re
|
| 4 |
from datetime import datetime
|
| 5 |
-
|
| 6 |
from typing import Generator, List, Tuple, Optional
|
| 7 |
import logging
|
| 8 |
-
|
| 9 |
|
| 10 |
# --- Load Environment Variables ---
|
| 11 |
-
|
| 12 |
|
| 13 |
# --- Logging Configuration ---
|
| 14 |
logging.basicConfig(level=logging.INFO)
|
| 15 |
logger = logging.getLogger(__name__)
|
| 16 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
# --- Constants ---
|
| 18 |
MODEL_CONFIG = {
|
| 19 |
-
"model": "meta-llama/
|
| 20 |
"temperature": 0.5,
|
| 21 |
"max_completion_tokens": 1024,
|
| 22 |
"stream": True,
|
|
@@ -186,8 +206,17 @@ def main() -> None:
|
|
| 186 |
try:
|
| 187 |
setup_page()
|
| 188 |
initialize_session_state()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 189 |
render_sidebar()
|
| 190 |
-
|
| 191 |
st.title("🧠 DoctorX")
|
| 192 |
st.caption("Preliminary health guidance - Always consult healthcare professionals")
|
| 193 |
|
|
@@ -206,16 +235,6 @@ def main() -> None:
|
|
| 206 |
logger.error(f"Application error: {str(e)}")
|
| 207 |
st.error("An unexpected error occurred. Please refresh the page and try again.")
|
| 208 |
|
| 209 |
-
#
|
| 210 |
-
try:
|
| 211 |
-
# client = Groq(api_key=os.getenv("GROQ_API_KEY"))
|
| 212 |
-
client = Groq(api_key=GROQ_API_KEY)
|
| 213 |
-
if not client:
|
| 214 |
-
raise ValueError("Failed to initialize Groq client. Please check your API key.")
|
| 215 |
-
except Exception as e:
|
| 216 |
-
logger.error(f"Error initializing Groq client: {str(e)}")
|
| 217 |
-
st.error("Failed to initialize AI model. Please check your API key.")
|
| 218 |
-
st.stop()
|
| 219 |
-
|
| 220 |
if __name__ == "__main__":
|
| 221 |
main()
|
|
|
|
| 2 |
import streamlit as st
|
| 3 |
import re
|
| 4 |
from datetime import datetime
|
| 5 |
+
import os
|
| 6 |
from typing import Generator, List, Tuple, Optional
|
| 7 |
import logging
|
| 8 |
+
from dotenv import load_dotenv
|
| 9 |
|
| 10 |
# --- Load Environment Variables ---
|
| 11 |
+
load_dotenv()
|
| 12 |
|
| 13 |
# --- Logging Configuration ---
|
| 14 |
logging.basicConfig(level=logging.INFO)
|
| 15 |
logger = logging.getLogger(__name__)
|
| 16 |
|
| 17 |
+
# --- API Key Management ---
|
| 18 |
+
def get_api_key() -> Optional[str]:
|
| 19 |
+
"""Get API key from environment or user input."""
|
| 20 |
+
api_key = os.getenv("GROQ_API_KEY")
|
| 21 |
+
|
| 22 |
+
if not api_key:
|
| 23 |
+
st.sidebar.markdown("## 🔑 API Configuration")
|
| 24 |
+
api_key = st.sidebar.text_input(
|
| 25 |
+
"Enter your Groq API Key:",
|
| 26 |
+
type="password",
|
| 27 |
+
help="Get your API key from https://console.groq.com",
|
| 28 |
+
key="groq_api_key"
|
| 29 |
+
)
|
| 30 |
+
if api_key:
|
| 31 |
+
st.sidebar.success("API Key set successfully!")
|
| 32 |
+
else:
|
| 33 |
+
st.sidebar.warning("Please enter your Groq API Key to continue")
|
| 34 |
+
|
| 35 |
+
return api_key
|
| 36 |
+
|
| 37 |
# --- Constants ---
|
| 38 |
MODEL_CONFIG = {
|
| 39 |
+
"model": "meta-llama/llama-4-scout-17b-16e-instruct",
|
| 40 |
"temperature": 0.5,
|
| 41 |
"max_completion_tokens": 1024,
|
| 42 |
"stream": True,
|
|
|
|
| 206 |
try:
|
| 207 |
setup_page()
|
| 208 |
initialize_session_state()
|
| 209 |
+
|
| 210 |
+
# Get API key
|
| 211 |
+
api_key = get_api_key()
|
| 212 |
+
if not api_key:
|
| 213 |
+
st.stop()
|
| 214 |
+
|
| 215 |
+
# Initialize Groq client
|
| 216 |
+
global client
|
| 217 |
+
client = Groq(api_key=api_key)
|
| 218 |
+
|
| 219 |
render_sidebar()
|
|
|
|
| 220 |
st.title("🧠 DoctorX")
|
| 221 |
st.caption("Preliminary health guidance - Always consult healthcare professionals")
|
| 222 |
|
|
|
|
| 235 |
logger.error(f"Application error: {str(e)}")
|
| 236 |
st.error("An unexpected error occurred. Please refresh the page and try again.")
|
| 237 |
|
| 238 |
+
# Remove the global client initialization
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 239 |
if __name__ == "__main__":
|
| 240 |
main()
|