add streamlit app.py and requirements
Browse files- .env.example +5 -0
- .gitignore +41 -0
- requirements.txt +3 -3
- src/streamlit_app.py +170 -38
.env.example
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# HuggingFace API Token
|
| 2 |
+
HF_TOKEN=your_huggingface_token_here
|
| 3 |
+
|
| 4 |
+
# Inference Endpoint URL
|
| 5 |
+
INFERENCE_ENDPOINT=your_inference_endpoint_url_here
|
.gitignore
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Environment variables
|
| 2 |
+
.env
|
| 3 |
+
|
| 4 |
+
# Python
|
| 5 |
+
__pycache__/
|
| 6 |
+
*.py[cod]
|
| 7 |
+
*$py.class
|
| 8 |
+
*.so
|
| 9 |
+
.Python
|
| 10 |
+
env/
|
| 11 |
+
venv/
|
| 12 |
+
ENV/
|
| 13 |
+
build/
|
| 14 |
+
develop-eggs/
|
| 15 |
+
dist/
|
| 16 |
+
downloads/
|
| 17 |
+
eggs/
|
| 18 |
+
.eggs/
|
| 19 |
+
lib/
|
| 20 |
+
lib64/
|
| 21 |
+
parts/
|
| 22 |
+
sdist/
|
| 23 |
+
var/
|
| 24 |
+
wheels/
|
| 25 |
+
*.egg-info/
|
| 26 |
+
.installed.cfg
|
| 27 |
+
*.egg
|
| 28 |
+
|
| 29 |
+
# IDEs
|
| 30 |
+
.vscode/
|
| 31 |
+
.idea/
|
| 32 |
+
*.swp
|
| 33 |
+
*.swo
|
| 34 |
+
*~
|
| 35 |
+
|
| 36 |
+
# OS
|
| 37 |
+
.DS_Store
|
| 38 |
+
Thumbs.db
|
| 39 |
+
|
| 40 |
+
# Streamlit
|
| 41 |
+
.streamlit/
|
requirements.txt
CHANGED
|
@@ -1,3 +1,3 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
|
|
|
| 1 |
+
streamlit
|
| 2 |
+
huggingface_hub
|
| 3 |
+
requests
|
src/streamlit_app.py
CHANGED
|
@@ -1,40 +1,172 @@
|
|
| 1 |
-
import altair as alt
|
| 2 |
-
import numpy as np
|
| 3 |
-
import pandas as pd
|
| 4 |
import streamlit as st
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
""
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
.
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
import requests
|
| 3 |
+
import os
|
| 4 |
|
| 5 |
+
# Page configuration
|
| 6 |
+
st.set_page_config(
|
| 7 |
+
page_title="Med-Gemma Chat",
|
| 8 |
+
page_icon="🏥",
|
| 9 |
+
layout="centered"
|
| 10 |
+
)
|
| 11 |
+
|
| 12 |
+
# Title
|
| 13 |
+
st.title("🏥 Med-Gemma Medical Assistant")
|
| 14 |
+
st.markdown("Ask medical questions and get informed responses from Med-Gemma")
|
| 15 |
+
|
| 16 |
+
# Sidebar for configuration
|
| 17 |
+
with st.sidebar:
|
| 18 |
+
st.header("Configuration")
|
| 19 |
+
|
| 20 |
+
# Get HuggingFace API token from environment or user input
|
| 21 |
+
hf_token = os.environ.get("HF_TOKEN", "")
|
| 22 |
+
if not hf_token:
|
| 23 |
+
hf_token = st.text_input(
|
| 24 |
+
"HuggingFace API Token",
|
| 25 |
+
type="password",
|
| 26 |
+
help="Enter your HuggingFace API token to access the inference endpoint"
|
| 27 |
+
)
|
| 28 |
+
else:
|
| 29 |
+
st.success("✓ API Token loaded from environment")
|
| 30 |
+
|
| 31 |
+
# Inference endpoint URL
|
| 32 |
+
default_endpoint = os.environ.get("INFERENCE_ENDPOINT", "")
|
| 33 |
+
endpoint_url = st.text_input(
|
| 34 |
+
"Inference Endpoint URL",
|
| 35 |
+
value=default_endpoint,
|
| 36 |
+
help="Your HuggingFace Inference Endpoint URL (e.g., https://xxx.endpoints.huggingface.cloud)"
|
| 37 |
+
)
|
| 38 |
+
|
| 39 |
+
st.info("💡 Make sure your token was created by the same account that owns the endpoint!")
|
| 40 |
+
|
| 41 |
+
# Model parameters
|
| 42 |
+
st.subheader("Model Parameters")
|
| 43 |
+
max_tokens = st.slider("Max Tokens", 50, 2048, 512)
|
| 44 |
+
temperature = st.slider("Temperature", 0.0, 2.0, 0.7, 0.1)
|
| 45 |
+
top_p = st.slider("Top P", 0.0, 1.0, 0.95, 0.05)
|
| 46 |
+
|
| 47 |
+
if st.button("Clear Chat History"):
|
| 48 |
+
st.session_state.messages = []
|
| 49 |
+
st.rerun()
|
| 50 |
+
|
| 51 |
+
# Initialize chat history
|
| 52 |
+
if "messages" not in st.session_state:
|
| 53 |
+
st.session_state.messages = []
|
| 54 |
+
|
| 55 |
+
# Display chat messages
|
| 56 |
+
for message in st.session_state.messages:
|
| 57 |
+
with st.chat_message(message["role"]):
|
| 58 |
+
st.markdown(message["content"])
|
| 59 |
+
|
| 60 |
+
# Function to call the inference endpoint
|
| 61 |
+
def query_model(prompt, endpoint_url, token, max_tokens, temperature, top_p):
|
| 62 |
+
"""Send a request to the HuggingFace Inference Endpoint"""
|
| 63 |
+
headers = {
|
| 64 |
+
"Authorization": f"Bearer {token}",
|
| 65 |
+
"Content-Type": "application/json"
|
| 66 |
+
}
|
| 67 |
+
|
| 68 |
+
# vLLM endpoints use OpenAI-compatible chat format
|
| 69 |
+
base_url = endpoint_url.rstrip('/')
|
| 70 |
+
api_url = f"{base_url}/v1/chat/completions"
|
| 71 |
+
|
| 72 |
+
payload = {
|
| 73 |
+
"model": "google/medgemma-27b-text-it",
|
| 74 |
+
"messages": [
|
| 75 |
+
{
|
| 76 |
+
"role": "user",
|
| 77 |
+
"content": prompt
|
| 78 |
+
}
|
| 79 |
+
],
|
| 80 |
+
"max_tokens": max_tokens,
|
| 81 |
+
"temperature": temperature,
|
| 82 |
+
"top_p": top_p
|
| 83 |
+
}
|
| 84 |
+
|
| 85 |
+
try:
|
| 86 |
+
response = requests.post(api_url, headers=headers, json=payload, timeout=60)
|
| 87 |
+
|
| 88 |
+
# Debug: Show status code if there's an error
|
| 89 |
+
if response.status_code != 200:
|
| 90 |
+
error_detail = response.text
|
| 91 |
+
return f"❌ Error {response.status_code}: {error_detail}\n\nCalled URL: {api_url}\n\n💡 Troubleshooting:\n- Make sure your token was created by the account that owns this endpoint\n- Check that the endpoint is 'Running' (not Paused)\n- Verify the endpoint URL is exactly: https://npbufgk80gff6voc.us-east-1.aws.endpoints.huggingface.cloud"
|
| 92 |
+
|
| 93 |
+
result = response.json()
|
| 94 |
+
|
| 95 |
+
# Handle OpenAI-compatible chat completion format
|
| 96 |
+
if isinstance(result, dict) and "choices" in result:
|
| 97 |
+
if len(result["choices"]) > 0:
|
| 98 |
+
return result["choices"][0]["message"]["content"]
|
| 99 |
+
else:
|
| 100 |
+
return "No response generated"
|
| 101 |
+
else:
|
| 102 |
+
return str(result)
|
| 103 |
+
|
| 104 |
+
except requests.exceptions.RequestException as e:
|
| 105 |
+
return f"❌ Error connecting to the model: {str(e)}\n\nMake sure your endpoint URL is correct and the endpoint is running."
|
| 106 |
+
except Exception as e:
|
| 107 |
+
return f"❌ Error processing response: {str(e)}"
|
| 108 |
+
|
| 109 |
+
# Chat input
|
| 110 |
+
if prompt := st.chat_input("Ask a medical question..."):
|
| 111 |
+
# Validate configuration
|
| 112 |
+
if not hf_token:
|
| 113 |
+
st.error("⚠️ Please provide your HuggingFace API Token in the sidebar")
|
| 114 |
+
st.stop()
|
| 115 |
+
|
| 116 |
+
if not endpoint_url:
|
| 117 |
+
st.error("⚠️ Please provide your Inference Endpoint URL in the sidebar")
|
| 118 |
+
st.stop()
|
| 119 |
+
|
| 120 |
+
# Add user message to chat history
|
| 121 |
+
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 122 |
+
|
| 123 |
+
# Display user message
|
| 124 |
+
with st.chat_message("user"):
|
| 125 |
+
st.markdown(prompt)
|
| 126 |
+
|
| 127 |
+
# Display assistant response
|
| 128 |
+
with st.chat_message("assistant"):
|
| 129 |
+
message_placeholder = st.empty()
|
| 130 |
+
message_placeholder.markdown("Thinking... 🤔")
|
| 131 |
+
|
| 132 |
+
# Query the model
|
| 133 |
+
response = query_model(
|
| 134 |
+
prompt=prompt,
|
| 135 |
+
endpoint_url=endpoint_url,
|
| 136 |
+
token=hf_token,
|
| 137 |
+
max_tokens=max_tokens,
|
| 138 |
+
temperature=temperature,
|
| 139 |
+
top_p=top_p
|
| 140 |
+
)
|
| 141 |
+
|
| 142 |
+
# Display the response
|
| 143 |
+
message_placeholder.markdown(response)
|
| 144 |
+
|
| 145 |
+
# Add assistant response to chat history
|
| 146 |
+
st.session_state.messages.append({"role": "assistant", "content": response})
|
| 147 |
+
|
| 148 |
+
# Information footer
|
| 149 |
+
with st.expander("ℹ️ How to use"):
|
| 150 |
+
st.markdown("""
|
| 151 |
+
### Getting Started:
|
| 152 |
+
|
| 153 |
+
1. **Get your HuggingFace API Token:**
|
| 154 |
+
- Go to [HuggingFace Settings](https://huggingface.co/settings/tokens)
|
| 155 |
+
- Create a new token with 'read' permissions
|
| 156 |
+
- Copy the token
|
| 157 |
+
|
| 158 |
+
2. **Get your Inference Endpoint URL:**
|
| 159 |
+
- Go to your [HuggingFace Inference Endpoints](https://ui.endpoints.huggingface.co/)
|
| 160 |
+
- Copy the endpoint URL (it looks like: `https://xxx.endpoints.huggingface.cloud`)
|
| 161 |
+
|
| 162 |
+
3. **Enter credentials:**
|
| 163 |
+
- Paste both in the sidebar configuration
|
| 164 |
+
- Or set them as environment variables: `HF_TOKEN` and `INFERENCE_ENDPOINT`
|
| 165 |
+
|
| 166 |
+
4. **Start chatting:**
|
| 167 |
+
- Type your medical question in the chat input
|
| 168 |
+
- Wait for Med-Gemma to respond
|
| 169 |
+
|
| 170 |
+
### Note:
|
| 171 |
+
This is an AI assistant for informational purposes only. Always consult healthcare professionals for medical advice.
|
| 172 |
+
""")
|