Update app.py
Browse files
app.py
CHANGED
@@ -1,19 +1,13 @@
|
|
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 |
|
@@ -27,18 +21,24 @@ def respond(
|
|
27 |
|
28 |
response = ""
|
29 |
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
|
43 |
# CSS for styling the interface
|
44 |
css = """
|
@@ -46,7 +46,6 @@ 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,30 +53,32 @@ 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 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
|
|
|
|
|
|
74 |
gr.Button("Chatgpt"),
|
75 |
gr.Button("Llama"),
|
76 |
gr.Button("Claude"),
|
|
|
|
|
|
|
|
|
|
|
77 |
],
|
78 |
css=css, # Pass the custom CSS here
|
79 |
)
|
80 |
|
81 |
-
|
82 |
if __name__ == "__main__":
|
83 |
demo.launch(share=True)
|
|
|
1 |
import gradio as gr
|
2 |
from huggingface_hub import InferenceClient
|
3 |
|
|
|
|
|
|
|
4 |
client = InferenceClient("meta-llama/Llama-3.2-3B-Instruct")
|
5 |
|
|
|
6 |
def respond(
|
7 |
+
model_name,
|
8 |
message,
|
9 |
history: list[tuple[str, str]],
|
10 |
system_message,
|
|
|
|
|
|
|
11 |
):
|
12 |
messages = [{"role": "system", "content": system_message}]
|
13 |
|
|
|
21 |
|
22 |
response = ""
|
23 |
|
24 |
+
if model_name == "Llama":
|
25 |
+
for message in client.chat_completion(
|
26 |
+
messages,
|
27 |
+
max_tokens=512,
|
28 |
+
stream=True,
|
29 |
+
temperature=0.7,
|
30 |
+
top_p=0.95,
|
31 |
+
):
|
32 |
+
token = message.choices[0].delta.content
|
33 |
+
response += token
|
34 |
+
elif model_name == "Chatgpt":
|
35 |
+
response = "ChatGPT functionality is not yet implemented."
|
36 |
+
elif model_name == "Claude":
|
37 |
+
response = "Claude functionality is not yet implemented."
|
38 |
+
else:
|
39 |
+
response = "Model not recognized."
|
40 |
+
|
41 |
+
return response
|
42 |
|
43 |
# CSS for styling the interface
|
44 |
css = """
|
|
|
46 |
background-color: #06688E; /* Dark background */
|
47 |
color: white; /* Text color for better visibility */
|
48 |
}
|
|
|
49 |
.gr-button {
|
50 |
background-color: #42B3CE !important; /* White button color */
|
51 |
color: black !important; /* Black text for contrast */
|
|
|
53 |
padding: 8px 16px !important;
|
54 |
border-radius: 5px !important;
|
55 |
}
|
|
|
56 |
.gr-button:hover {
|
57 |
background-color: #e0e0e0 !important; /* Slightly lighter button on hover */
|
58 |
}
|
|
|
|
|
|
|
|
|
59 |
"""
|
60 |
|
61 |
+
# Define the button click handler to hide the dropdown
|
62 |
+
def on_button_click(model_name, message, history, system_message, dropdown_state):
|
63 |
+
return respond(model_name, message, history, system_message), gr.update(visible=False) # Hide dropdown after click
|
64 |
+
|
65 |
+
# Define the Gradio interface with buttons and model selection
|
66 |
+
demo = gr.Interface(
|
67 |
+
fn=on_button_click,
|
68 |
+
inputs=[
|
69 |
+
gr.Textbox(value="Hello!", label="User Message"),
|
70 |
+
gr.Textbox(value="You are a virtual health assistant. Your primary goal is to assist with health-related queries.", label="System Message", visible=False),
|
71 |
gr.Button("Chatgpt"),
|
72 |
gr.Button("Llama"),
|
73 |
gr.Button("Claude"),
|
74 |
+
gr.State() # To track the state of dropdown visibility
|
75 |
+
],
|
76 |
+
outputs=[
|
77 |
+
"text", # For the model's response
|
78 |
+
gr.Textbox(visible=False) # Dropdown (this textbox will be hidden after the button is clicked)
|
79 |
],
|
80 |
css=css, # Pass the custom CSS here
|
81 |
)
|
82 |
|
|
|
83 |
if __name__ == "__main__":
|
84 |
demo.launch(share=True)
|