Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,10 +2,10 @@ import gradio as gr
|
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
import os
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
HF_TOKEN = os.
|
| 7 |
|
| 8 |
-
# Persona system
|
| 9 |
PERSONA_MESSAGE = (
|
| 10 |
"You are Dr. Alex, a highly knowledgeable yet empathetic doctor. "
|
| 11 |
"You always provide clear, safe, and well-structured medical advice in simple language. "
|
|
@@ -13,39 +13,47 @@ PERSONA_MESSAGE = (
|
|
| 13 |
"You behave politely, patiently, and with care, like a trusted family doctor."
|
| 14 |
)
|
| 15 |
|
| 16 |
-
# Create HF InferenceClient
|
| 17 |
client = InferenceClient(
|
| 18 |
model="m42-health/Llama3-Med42-70B",
|
| 19 |
token=HF_TOKEN
|
| 20 |
)
|
| 21 |
|
|
|
|
| 22 |
def respond(message, history, max_tokens, temperature, top_p):
|
| 23 |
-
|
| 24 |
-
|
|
|
|
| 25 |
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
|
|
|
|
|
|
| 30 |
|
| 31 |
-
|
| 32 |
-
|
| 33 |
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
|
| 48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
chatbot = gr.ChatInterface(
|
| 50 |
respond,
|
| 51 |
type="messages",
|
|
@@ -53,13 +61,15 @@ chatbot = gr.ChatInterface(
|
|
| 53 |
gr.Slider(1, 2048, value=512, step=1, label="Max tokens"),
|
| 54 |
gr.Slider(0.1, 2.0, value=0.7, step=0.1, label="Temperature"),
|
| 55 |
gr.Slider(0.1, 1.0, value=0.95, step=0.05, label="Top-p"),
|
| 56 |
-
]
|
| 57 |
)
|
| 58 |
|
|
|
|
| 59 |
with gr.Blocks() as demo:
|
| 60 |
with gr.Row():
|
| 61 |
gr.Markdown("## 🩺 AI Health Mentor — Dr. Alex")
|
| 62 |
chatbot.render()
|
| 63 |
|
|
|
|
| 64 |
if __name__ == "__main__":
|
| 65 |
demo.launch(show_error=True)
|
|
|
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
import os
|
| 4 |
|
| 5 |
+
# 🔹 Hardcoded token for internal use (or use os.environ for safety)
|
| 6 |
+
HF_TOKEN = os.getenv('telemedpro')
|
| 7 |
|
| 8 |
+
# Persona system message
|
| 9 |
PERSONA_MESSAGE = (
|
| 10 |
"You are Dr. Alex, a highly knowledgeable yet empathetic doctor. "
|
| 11 |
"You always provide clear, safe, and well-structured medical advice in simple language. "
|
|
|
|
| 13 |
"You behave politely, patiently, and with care, like a trusted family doctor."
|
| 14 |
)
|
| 15 |
|
| 16 |
+
# Create HF InferenceClient
|
| 17 |
client = InferenceClient(
|
| 18 |
model="m42-health/Llama3-Med42-70B",
|
| 19 |
token=HF_TOKEN
|
| 20 |
)
|
| 21 |
|
| 22 |
+
# Respond function with safe error handling
|
| 23 |
def respond(message, history, max_tokens, temperature, top_p):
|
| 24 |
+
try:
|
| 25 |
+
# Initialize messages with persona
|
| 26 |
+
messages = [{"role": "system", "content": PERSONA_MESSAGE}]
|
| 27 |
|
| 28 |
+
# Add conversation history safely
|
| 29 |
+
for h in history:
|
| 30 |
+
user_msg = h[0] if h[0] is not None else ""
|
| 31 |
+
ai_msg = h[1] if h[1] is not None else ""
|
| 32 |
+
messages.append({"role": "user", "content": user_msg})
|
| 33 |
+
messages.append({"role": "assistant", "content": ai_msg})
|
| 34 |
|
| 35 |
+
# Add current user message
|
| 36 |
+
messages.append({"role": "user", "content": message})
|
| 37 |
|
| 38 |
+
# Stream AI response token by token
|
| 39 |
+
response = ""
|
| 40 |
+
for msg in client.chat_completion(
|
| 41 |
+
messages,
|
| 42 |
+
max_tokens=max_tokens,
|
| 43 |
+
stream=True,
|
| 44 |
+
temperature=temperature,
|
| 45 |
+
top_p=top_p
|
| 46 |
+
):
|
| 47 |
+
if msg.choices and msg.choices[0].delta.content:
|
| 48 |
+
token = msg.choices[0].delta.content
|
| 49 |
+
response += token
|
| 50 |
+
yield response
|
| 51 |
|
| 52 |
+
except Exception as e:
|
| 53 |
+
# Return error as a string so Space never crashes
|
| 54 |
+
yield f"⚠️ Space error: {e}"
|
| 55 |
+
|
| 56 |
+
# 🔹 Gradio Chat Interface
|
| 57 |
chatbot = gr.ChatInterface(
|
| 58 |
respond,
|
| 59 |
type="messages",
|
|
|
|
| 61 |
gr.Slider(1, 2048, value=512, step=1, label="Max tokens"),
|
| 62 |
gr.Slider(0.1, 2.0, value=0.7, step=0.1, label="Temperature"),
|
| 63 |
gr.Slider(0.1, 1.0, value=0.95, step=0.05, label="Top-p"),
|
| 64 |
+
]
|
| 65 |
)
|
| 66 |
|
| 67 |
+
# 🔹 Layout
|
| 68 |
with gr.Blocks() as demo:
|
| 69 |
with gr.Row():
|
| 70 |
gr.Markdown("## 🩺 AI Health Mentor — Dr. Alex")
|
| 71 |
chatbot.render()
|
| 72 |
|
| 73 |
+
# 🔹 Launch the Space safely
|
| 74 |
if __name__ == "__main__":
|
| 75 |
demo.launch(show_error=True)
|