Update app.py
Browse files
app.py
CHANGED
@@ -1,11 +1,52 @@
|
|
1 |
import gradio as gr
|
2 |
from huggingface_hub import InferenceClient
|
3 |
|
4 |
-
|
5 |
-
For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
|
6 |
-
"""
|
7 |
client = InferenceClient("meta-llama/Llama-3.2-3B-Instruct")
|
8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
def respond(
|
11 |
message,
|
@@ -15,11 +56,14 @@ def respond(
|
|
15 |
temperature,
|
16 |
top_p,
|
17 |
):
|
|
|
|
|
|
|
18 |
messages = [{"role": "system", "content": system_message}]
|
19 |
|
20 |
for val in history:
|
21 |
-
if val
|
22 |
-
messages.append({"role": "user", "content": val
|
23 |
if val[1]:
|
24 |
messages.append({"role": "assistant", "content": val[1]})
|
25 |
|
@@ -34,19 +78,17 @@ def respond(
|
|
34 |
temperature=temperature,
|
35 |
top_p=top_p,
|
36 |
):
|
37 |
-
token = message.choices
|
38 |
|
39 |
response += token
|
40 |
yield response
|
41 |
|
42 |
-
|
43 |
# CSS for styling the interface
|
44 |
css = """
|
45 |
body {
|
46 |
background-color: #06688E; /* Dark background */
|
47 |
color: white; /* Text color for better visibility */
|
48 |
}
|
49 |
-
|
50 |
.gr-button {
|
51 |
background-color: #42B3CE !important; /* White button color */
|
52 |
color: black !important; /* Black text for contrast */
|
@@ -54,19 +96,14 @@ body {
|
|
54 |
padding: 8px 16px !important;
|
55 |
border-radius: 5px !important;
|
56 |
}
|
57 |
-
|
58 |
.gr-button:hover {
|
59 |
background-color: #e0e0e0 !important; /* Slightly lighter button on hover */
|
60 |
}
|
61 |
-
|
62 |
.gr-slider-container {
|
63 |
color: white !important; /* Slider labels in white */
|
64 |
}
|
65 |
"""
|
66 |
|
67 |
-
"""
|
68 |
-
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
69 |
-
"""
|
70 |
demo = gr.ChatInterface(
|
71 |
respond,
|
72 |
additional_inputs=[
|
@@ -78,12 +115,12 @@ demo = gr.ChatInterface(
|
|
78 |
maximum=1.0,
|
79 |
value=0.95,
|
80 |
step=0.05,
|
81 |
-
label="Top-p (nucleus sampling)",
|
|
|
82 |
),
|
83 |
],
|
84 |
css=css, # Pass the custom CSS here
|
85 |
)
|
86 |
|
87 |
-
|
88 |
if __name__ == "__main__":
|
89 |
-
demo.launch(share=True)
|
|
|
1 |
import gradio as gr
|
2 |
from huggingface_hub import InferenceClient
|
3 |
|
4 |
+
# Initialize the InferenceClient
|
|
|
|
|
5 |
client = InferenceClient("meta-llama/Llama-3.2-3B-Instruct")
|
6 |
|
7 |
+
def is_health_related(message):
|
8 |
+
# Simple heuristic to check if the message is health-related
|
9 |
+
health_keywords = [
|
10 |
+
"health", "cancer", "diabetes", "hypertension", "asthma",
|
11 |
+
"arthritis", "cardiology", "neurology", "depression",
|
12 |
+
"anxiety", "cholesterol", "obesity", "allergies", "autism",
|
13 |
+
"osteoporosis", "stroke", "HIV", "AIDS", "hepatitis",
|
14 |
+
"tuberculosis", "malaria", "COVID-19", "Parkinson's",
|
15 |
+
"Alzheimer's", "dementia", "eczema", "psoriasis", "migraine",
|
16 |
+
"epilepsy", "insomnia", "fibromyalgia", "sclerosis",
|
17 |
+
"pneumonia", "influenza", "endometriosis", "infertility",
|
18 |
+
"glaucoma", "cataracts", "anemia", "leukemia", "lymphoma",
|
19 |
+
"melanoma", "dermatitis", "IBS", "Crohn's", "ulcers",
|
20 |
+
"GERD", "kidney disease", "liver disease", "cirrhosis",
|
21 |
+
"thyroid", "menopause", "prostate", "cholera",
|
22 |
+
"dysentery", "polio", "tetanus", "diphtheria",
|
23 |
+
"Zika", "Ebola", "mental health", "physical health",
|
24 |
+
"immunology", "orthopedics", "pediatrics", "geriatrics",
|
25 |
+
"surgery", "rehabilitation", "nutrition", "fitness",
|
26 |
+
"wellness", "vaccination", "therapy", "radiology",
|
27 |
+
"oncology", "pathology", "psychology", "psychiatry",
|
28 |
+
"urology", "gastroenterology", "dermatology", "hematology",
|
29 |
+
"toxicology", "pharmacology", "trauma", "injury",
|
30 |
+
"burns", "sepsis", "addiction", "substance abuse",
|
31 |
+
"symptoms", "diagnosis", "treatment", "medication",
|
32 |
+
"medicine", "patient", "healthcare", "hospital",
|
33 |
+
"clinic", "recovery", "remedy", "pain management",
|
34 |
+
"first aid", "disease prevention", "immunization",
|
35 |
+
"primary care", "specialist", "prescription", "chronic illness",
|
36 |
+
"acute care", "emergency care", "surgical procedures",
|
37 |
+
"therapy sessions", "mental therapy", "physical therapy",
|
38 |
+
"health monitoring", "health insurance", "medical advice",
|
39 |
+
"health tips", "diagnostic tests", "lab results",
|
40 |
+
"consultation", "second opinion", "side effects", "health checkup",
|
41 |
+
"disease management", "caregiver support", "home care",
|
42 |
+
"alternative medicine", "herbal treatment", "integrative medicine",
|
43 |
+
"personalized care", "health records", "online consultation"
|
44 |
+
]
|
45 |
+
message = message.lower()
|
46 |
+
for keyword in health_keywords:
|
47 |
+
if keyword in message:
|
48 |
+
return True
|
49 |
+
return False
|
50 |
|
51 |
def respond(
|
52 |
message,
|
|
|
56 |
temperature,
|
57 |
top_p,
|
58 |
):
|
59 |
+
if not is_health_related(message):
|
60 |
+
return "Sorry, I can't help you with that because I am just a bot who can help with health-related queries."
|
61 |
+
|
62 |
messages = [{"role": "system", "content": system_message}]
|
63 |
|
64 |
for val in history:
|
65 |
+
if val:
|
66 |
+
messages.append({"role": "user", "content": val})
|
67 |
if val[1]:
|
68 |
messages.append({"role": "assistant", "content": val[1]})
|
69 |
|
|
|
78 |
temperature=temperature,
|
79 |
top_p=top_p,
|
80 |
):
|
81 |
+
token = message.choices.delta.content
|
82 |
|
83 |
response += token
|
84 |
yield response
|
85 |
|
|
|
86 |
# CSS for styling the interface
|
87 |
css = """
|
88 |
body {
|
89 |
background-color: #06688E; /* Dark background */
|
90 |
color: white; /* Text color for better visibility */
|
91 |
}
|
|
|
92 |
.gr-button {
|
93 |
background-color: #42B3CE !important; /* White button color */
|
94 |
color: black !important; /* Black text for contrast */
|
|
|
96 |
padding: 8px 16px !important;
|
97 |
border-radius: 5px !important;
|
98 |
}
|
|
|
99 |
.gr-button:hover {
|
100 |
background-color: #e0e0e0 !important; /* Slightly lighter button on hover */
|
101 |
}
|
|
|
102 |
.gr-slider-container {
|
103 |
color: white !important; /* Slider labels in white */
|
104 |
}
|
105 |
"""
|
106 |
|
|
|
|
|
|
|
107 |
demo = gr.ChatInterface(
|
108 |
respond,
|
109 |
additional_inputs=[
|
|
|
115 |
maximum=1.0,
|
116 |
value=0.95,
|
117 |
step=0.05,
|
118 |
+
label="Top-p (nucleus sampling)",
|
119 |
+
visible=False
|
120 |
),
|
121 |
],
|
122 |
css=css, # Pass the custom CSS here
|
123 |
)
|
124 |
|
|
|
125 |
if __name__ == "__main__":
|
126 |
+
demo.launch(share=True)
|