Update app.py
Browse files
app.py
CHANGED
@@ -1,32 +1,33 @@
|
|
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("HuggingFaceH4/zephyr-7b-beta")
|
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
|
21 |
-
if
|
22 |
-
messages.append({"role": "user", "content":
|
23 |
-
if
|
24 |
-
messages.append({"role": "assistant", "content":
|
25 |
|
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,29 +36,27 @@ 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 |
-
|
44 |
-
"""
|
45 |
demo = gr.ChatInterface(
|
46 |
respond,
|
47 |
additional_inputs=[
|
48 |
-
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
49 |
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
50 |
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
51 |
-
gr.Slider(
|
52 |
-
minimum=0.1,
|
53 |
-
maximum=1.0,
|
54 |
-
value=0.95,
|
55 |
-
step=0.05,
|
56 |
-
label="Top-p (nucleus sampling)",
|
57 |
-
),
|
58 |
],
|
|
|
59 |
)
|
60 |
|
61 |
-
|
62 |
if __name__ == "__main__":
|
63 |
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
from huggingface_hub import InferenceClient
|
3 |
|
|
|
|
|
|
|
|
|
4 |
|
5 |
+
client = InferenceClient(model="HuggingFaceH4/zephyr-7b-beta")
|
6 |
|
7 |
+
|
8 |
+
system_message = ("This is MUSK-1, developed by a 14 year old AI engineer Arjun Singh at Elieon.")
|
9 |
+
# Function to generate responses using the LLM
|
10 |
def respond(
|
11 |
message,
|
12 |
history: list[tuple[str, str]],
|
|
|
13 |
max_tokens,
|
14 |
temperature,
|
15 |
top_p,
|
16 |
):
|
17 |
+
# Construct the message history for the API request
|
18 |
messages = [{"role": "system", "content": system_message}]
|
19 |
|
20 |
+
for user_msg, assistant_msg in history:
|
21 |
+
if user_msg:
|
22 |
+
messages.append({"role": "user", "content": user_msg})
|
23 |
+
if assistant_msg:
|
24 |
+
messages.append({"role": "assistant", "content": assistant_msg})
|
25 |
|
26 |
messages.append({"role": "user", "content": message})
|
27 |
|
28 |
response = ""
|
29 |
|
30 |
+
# Stream the response tokens from the model
|
31 |
for message in client.chat_completion(
|
32 |
messages,
|
33 |
max_tokens=max_tokens,
|
|
|
36 |
top_p=top_p,
|
37 |
):
|
38 |
token = message.choices[0].delta.content
|
|
|
39 |
response += token
|
40 |
yield response
|
41 |
|
42 |
+
# Create a Gradio ChatInterface with custom styling
|
43 |
+
custom_css = """
|
44 |
+
.css-1rw10a3 {
|
45 |
+
height: 500px; /* Adjust height as needed */
|
46 |
+
overflow-y: scroll; /* Add scrollbar if content exceeds height */
|
47 |
+
}
|
48 |
"""
|
49 |
+
|
|
|
50 |
demo = gr.ChatInterface(
|
51 |
respond,
|
52 |
additional_inputs=[
|
|
|
53 |
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
54 |
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
55 |
+
gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"),
|
|
|
|
|
|
|
|
|
|
|
|
|
56 |
],
|
57 |
+
css=custom_css, # Apply custom CSS styling
|
58 |
)
|
59 |
|
60 |
+
# Launch the Gradio app
|
61 |
if __name__ == "__main__":
|
62 |
demo.launch()
|