Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,6 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
|
| 4 |
-
|
| 5 |
def respond(
|
| 6 |
message,
|
| 7 |
history: list[dict[str, str]],
|
|
@@ -12,34 +11,36 @@ def respond(
|
|
| 12 |
hf_token: gr.OAuthToken,
|
| 13 |
):
|
| 14 |
"""
|
| 15 |
-
|
| 16 |
"""
|
| 17 |
-
client = InferenceClient(token=hf_token.token, model="
|
| 18 |
-
|
| 19 |
-
messages
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
response = ""
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
|
|
|
| 30 |
stream=True,
|
| 31 |
temperature=temperature,
|
| 32 |
top_p=top_p,
|
| 33 |
):
|
| 34 |
-
choices = message.choices
|
| 35 |
-
token = ""
|
| 36 |
-
if len(choices) and choices[0].delta.content:
|
| 37 |
-
token = choices[0].delta.content
|
| 38 |
-
|
| 39 |
response += token
|
| 40 |
yield response
|
| 41 |
|
| 42 |
-
|
| 43 |
"""
|
| 44 |
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
| 45 |
"""
|
|
@@ -47,7 +48,7 @@ chatbot = gr.ChatInterface(
|
|
| 47 |
respond,
|
| 48 |
type="messages",
|
| 49 |
additional_inputs=[
|
| 50 |
-
gr.Textbox(value="You are a
|
| 51 |
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 52 |
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 53 |
gr.Slider(
|
|
@@ -59,12 +60,15 @@ chatbot = gr.ChatInterface(
|
|
| 59 |
),
|
| 60 |
],
|
| 61 |
)
|
| 62 |
-
|
| 63 |
with gr.Blocks() as demo:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
with gr.Sidebar():
|
| 65 |
gr.LoginButton()
|
| 66 |
chatbot.render()
|
| 67 |
|
| 68 |
-
|
| 69 |
if __name__ == "__main__":
|
| 70 |
-
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
|
|
|
|
| 4 |
def respond(
|
| 5 |
message,
|
| 6 |
history: list[dict[str, str]],
|
|
|
|
| 11 |
hf_token: gr.OAuthToken,
|
| 12 |
):
|
| 13 |
"""
|
| 14 |
+
Generate a response using the Dolphin 2.9.1 Llama 3 70B model
|
| 15 |
"""
|
| 16 |
+
client = InferenceClient(token=hf_token.token, model="dphn/dolphin-2.9.1-llama-3-70b")
|
| 17 |
+
|
| 18 |
+
# Format the messages according to the ChatML template that Dolphin expects
|
| 19 |
+
formatted_prompt = f"<|im_start|>system\n{system_message}<|im_end|>\n"
|
| 20 |
+
|
| 21 |
+
# Add history messages
|
| 22 |
+
for entry in history:
|
| 23 |
+
if entry["role"] == "user":
|
| 24 |
+
formatted_prompt += f"<|im_start|>user\n{entry['content']}<|im_end|>\n"
|
| 25 |
+
elif entry["role"] == "assistant":
|
| 26 |
+
formatted_prompt += f"<|im_start|>assistant\n{entry['content']}<|im_end|>\n"
|
| 27 |
+
|
| 28 |
+
# Add the current user message
|
| 29 |
+
formatted_prompt += f"<|im_start|>user\n{message}<|im_end|>\n<|im_start|>assistant\n"
|
| 30 |
+
|
| 31 |
response = ""
|
| 32 |
+
|
| 33 |
+
# Send the formatted prompt to the model
|
| 34 |
+
for token in client.text_generation(
|
| 35 |
+
formatted_prompt,
|
| 36 |
+
max_new_tokens=max_tokens,
|
| 37 |
stream=True,
|
| 38 |
temperature=temperature,
|
| 39 |
top_p=top_p,
|
| 40 |
):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
response += token
|
| 42 |
yield response
|
| 43 |
|
|
|
|
| 44 |
"""
|
| 45 |
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
| 46 |
"""
|
|
|
|
| 48 |
respond,
|
| 49 |
type="messages",
|
| 50 |
additional_inputs=[
|
| 51 |
+
gr.Textbox(value="You are Dolphin, a helpful AI assistant.", label="System message"),
|
| 52 |
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 53 |
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 54 |
gr.Slider(
|
|
|
|
| 60 |
),
|
| 61 |
],
|
| 62 |
)
|
|
|
|
| 63 |
with gr.Blocks() as demo:
|
| 64 |
+
gr.Markdown("# Dolphin 2.9.1 Llama 3 70B Demo")
|
| 65 |
+
gr.Markdown("This is a demo of the Dolphin 2.9.1 Llama 3 70B model. Note that this model is uncensored.")
|
| 66 |
+
gr.Markdown("### Warning:")
|
| 67 |
+
gr.Markdown("This model is uncensored and may comply with any requests, including unethical ones. Use responsibly.")
|
| 68 |
+
|
| 69 |
with gr.Sidebar():
|
| 70 |
gr.LoginButton()
|
| 71 |
chatbot.render()
|
| 72 |
|
|
|
|
| 73 |
if __name__ == "__main__":
|
| 74 |
+
demo.launch()
|