Update app.py
Browse files
app.py
CHANGED
|
@@ -1,20 +1,11 @@
|
|
| 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,
|
| 12 |
-
history: list[tuple[str, str]],
|
| 13 |
-
system_message,
|
| 14 |
-
max_tokens,
|
| 15 |
-
temperature,
|
| 16 |
-
top_p,
|
| 17 |
-
):
|
| 18 |
messages = [{"role": "system", "content": system_message}]
|
| 19 |
|
| 20 |
for val in history:
|
|
@@ -26,7 +17,6 @@ def respond(
|
|
| 26 |
messages.append({"role": "user", "content": message})
|
| 27 |
|
| 28 |
response = ""
|
| 29 |
-
|
| 30 |
for message in client.chat_completion(
|
| 31 |
messages,
|
| 32 |
max_tokens=max_tokens,
|
|
@@ -35,55 +25,73 @@ def respond(
|
|
| 35 |
top_p=top_p,
|
| 36 |
):
|
| 37 |
token = message.choices[0].delta.content
|
| 38 |
-
|
| 39 |
response += token
|
| 40 |
yield response
|
| 41 |
|
| 42 |
-
|
| 43 |
-
# CSS for styling the interface
|
| 44 |
css = """
|
| 45 |
body {
|
| 46 |
-
|
| 47 |
-
color:
|
|
|
|
| 48 |
}
|
| 49 |
-
|
| 50 |
.gr-button {
|
| 51 |
-
background-color: #
|
| 52 |
-
color:
|
| 53 |
border: none !important;
|
| 54 |
-
|
| 55 |
-
|
|
|
|
|
|
|
|
|
|
| 56 |
}
|
| 57 |
-
|
| 58 |
.gr-button:hover {
|
| 59 |
-
background-color: #
|
| 60 |
}
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
}
|
| 65 |
"""
|
| 66 |
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
""
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
gr.
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
),
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
)
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
|
| 4 |
+
# Create an InferenceClient to interact with the model
|
|
|
|
|
|
|
| 5 |
client = InferenceClient("meta-llama/Llama-3.2-3B-Instruct")
|
| 6 |
|
| 7 |
+
# Define the function to generate a response
|
| 8 |
+
def respond(message, history: list[tuple[str, str]], system_message, max_tokens, temperature, top_p):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
messages = [{"role": "system", "content": system_message}]
|
| 10 |
|
| 11 |
for val in history:
|
|
|
|
| 17 |
messages.append({"role": "user", "content": message})
|
| 18 |
|
| 19 |
response = ""
|
|
|
|
| 20 |
for message in client.chat_completion(
|
| 21 |
messages,
|
| 22 |
max_tokens=max_tokens,
|
|
|
|
| 25 |
top_p=top_p,
|
| 26 |
):
|
| 27 |
token = message.choices[0].delta.content
|
|
|
|
| 28 |
response += token
|
| 29 |
yield response
|
| 30 |
|
| 31 |
+
# Custom CSS for styling
|
|
|
|
| 32 |
css = """
|
| 33 |
body {
|
| 34 |
+
font-family: 'Arial', sans-serif;
|
| 35 |
+
background-color: #f8f9fa; /* Light background */
|
| 36 |
+
color: #333;
|
| 37 |
}
|
|
|
|
| 38 |
.gr-button {
|
| 39 |
+
background-color: #0b2545 !important;
|
| 40 |
+
color: white !important;
|
| 41 |
border: none !important;
|
| 42 |
+
border-radius: 25px !important;
|
| 43 |
+
padding: 8px 20px !important;
|
| 44 |
+
font-size: 14px;
|
| 45 |
+
font-weight: bold;
|
| 46 |
+
cursor: pointer;
|
| 47 |
}
|
|
|
|
| 48 |
.gr-button:hover {
|
| 49 |
+
background-color: #0a1b35 !important;
|
| 50 |
}
|
| 51 |
+
.search-box {
|
| 52 |
+
border-radius: 20px;
|
| 53 |
+
border: 1px solid #ccc;
|
| 54 |
+
padding: 10px;
|
| 55 |
+
width: 100%;
|
| 56 |
+
font-size: 16px;
|
| 57 |
+
background-color: #ffffff;
|
| 58 |
}
|
| 59 |
"""
|
| 60 |
|
| 61 |
+
# Main function to create the interface
|
| 62 |
+
with gr.Blocks(css=css) as demo:
|
| 63 |
+
gr.Markdown("<h1 style='text-align: center;'>Health Assistant GPT</h1>")
|
| 64 |
+
gr.Markdown("<h3 style='text-align: center;'>What do you want to know about health and wellness?</h3>")
|
| 65 |
+
|
| 66 |
+
# Sidebar
|
| 67 |
+
with gr.Sidebar():
|
| 68 |
+
gr.Markdown("### Settings")
|
| 69 |
+
system_message = gr.Textbox(
|
| 70 |
+
value="You are a virtual health assistant designed to provide accurate and reliable information related to health, wellness, and medical topics. Your primary goal is to assist users with their health-related queries, offer general guidance, and suggest when to consult a licensed medical professional. If a user asks a question that is unrelated to health, wellness, or medical topics, respond politely but firmly.",
|
| 71 |
+
label="System message",
|
| 72 |
+
visible=False
|
| 73 |
+
)
|
| 74 |
+
max_tokens = gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens", visible=False)
|
| 75 |
+
temperature = gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature", visible=False)
|
| 76 |
+
top_p = gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)", visible=False)
|
| 77 |
+
|
| 78 |
+
# Main content
|
| 79 |
+
with gr.Row():
|
| 80 |
+
with gr.Column(scale=7):
|
| 81 |
+
gr.Markdown("### Ask a health-related question:")
|
| 82 |
+
search_input = gr.Textbox(label="Search Input", placeholder="Type your health-related question here...", lines=1)
|
| 83 |
+
submit_button = gr.Button("Generate Response")
|
| 84 |
+
output = gr.Markdown()
|
| 85 |
+
|
| 86 |
+
with gr.Column(scale=3):
|
| 87 |
+
gr.Markdown("### Upload a relevant file (Optional):")
|
| 88 |
+
uploaded_file = gr.File(label="Upload PDF")
|
| 89 |
+
|
| 90 |
+
# Button click action to trigger response generation
|
| 91 |
+
submit_button.click(
|
| 92 |
+
fn=respond,
|
| 93 |
+
inputs=[search_input, [], system_message, max_tokens, temperature, top_p], # Empty history for fresh chat
|
| 94 |
+
outputs=output
|
| 95 |
+
)
|
| 96 |
+
|
| 97 |
+
demo.launch()
|