Spaces:
Paused
Paused
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from huggingface_hub import InferenceClient
|
| 2 |
+
import gradio as gr
|
| 3 |
+
|
| 4 |
+
client = InferenceClient(
|
| 5 |
+
"mistralai/Mixtral-8x7B-Instruct-v0.1"
|
| 6 |
+
)
|
| 7 |
+
|
| 8 |
+
def format_prompt(message, history):
|
| 9 |
+
prompt = "<s>"
|
| 10 |
+
for user_prompt, bot_response in history:
|
| 11 |
+
prompt += f"[INST] {user_prompt} [/INST]"
|
| 12 |
+
prompt += f" {bot_response}</s> "
|
| 13 |
+
prompt += f"[INST] {message} [/INST]"
|
| 14 |
+
return prompt
|
| 15 |
+
|
| 16 |
+
def generate(
|
| 17 |
+
prompt, history, system_prompt, temperature=0.9, max_new_tokens=256, top_p=0.95, repetition_penalty=1.0,
|
| 18 |
+
):
|
| 19 |
+
temperature = float(temperature)
|
| 20 |
+
if temperature < 1e-2:
|
| 21 |
+
temperature = 1e-2
|
| 22 |
+
top_p = float(top_p)
|
| 23 |
+
|
| 24 |
+
generate_kwargs = dict(
|
| 25 |
+
temperature=temperature,
|
| 26 |
+
max_new_tokens=max_new_tokens,
|
| 27 |
+
top_p=top_p,
|
| 28 |
+
repetition_penalty=repetition_penalty,
|
| 29 |
+
do_sample=True,
|
| 30 |
+
seed=42,
|
| 31 |
+
)
|
| 32 |
+
|
| 33 |
+
formatted_prompt = format_prompt(f"{system_prompt}, {prompt}", history)
|
| 34 |
+
stream = client.text_generation(formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=False)
|
| 35 |
+
output = ""
|
| 36 |
+
|
| 37 |
+
for response in stream:
|
| 38 |
+
output += response.token.text
|
| 39 |
+
yield output
|
| 40 |
+
return output
|
| 41 |
+
|
| 42 |
+
additional_inputs=[
|
| 43 |
+
gr.Textbox(
|
| 44 |
+
label="System-Prompt",
|
| 45 |
+
max_lines=1,
|
| 46 |
+
interactive=True,
|
| 47 |
+
),
|
| 48 |
+
gr.Slider(
|
| 49 |
+
label="Temperatur",
|
| 50 |
+
value=0.5,
|
| 51 |
+
minimum=0.0,
|
| 52 |
+
maximum=1.0,
|
| 53 |
+
step=0.05,
|
| 54 |
+
interactive=True,
|
| 55 |
+
info="Höhere Werte erzeugen vielfältigere Ergebnisse.",
|
| 56 |
+
),
|
| 57 |
+
gr.Slider(
|
| 58 |
+
label="Maximale Anzahl neuer Tokens",
|
| 59 |
+
value=20480,
|
| 60 |
+
minimum=0,
|
| 61 |
+
maximum=32768,
|
| 62 |
+
step=64,
|
| 63 |
+
interactive=True,
|
| 64 |
+
info="Maximale Anzahl an Tokens",
|
| 65 |
+
),
|
| 66 |
+
gr.Slider(
|
| 67 |
+
label="Top-p (Nucleus Sampling)",
|
| 68 |
+
value=0.75,
|
| 69 |
+
minimum=0.0,
|
| 70 |
+
maximum=1,
|
| 71 |
+
step=0.05,
|
| 72 |
+
interactive=True,
|
| 73 |
+
info="Höhere Werte ermöglichen eine größere Vielfalt",
|
| 74 |
+
),
|
| 75 |
+
gr.Slider(
|
| 76 |
+
label="Wiederholungsstrafe",
|
| 77 |
+
value=1.2,
|
| 78 |
+
minimum=1.0,
|
| 79 |
+
maximum=2.0,
|
| 80 |
+
step=0.05,
|
| 81 |
+
interactive=True,
|
| 82 |
+
info="Strafe für wiederholte Tokens",
|
| 83 |
+
)
|
| 84 |
+
]
|
| 85 |
+
|
| 86 |
+
examples=[
|
| 87 |
+
["", "Antworten Sie immer vollständig auf Englisch", 0.5, 20480, 0.75, 1.2],
|
| 88 |
+
["", "Répondez toujours complètement en Français", 0.5, 20480, 0.75, 1.2],
|
| 89 |
+
["", "Отвечай всегда полностью на русском языке", 0.5, 20480, 0.75, 1.2],
|
| 90 |
+
]
|
| 91 |
+
|
| 92 |
+
description = r"""
|
| 93 |
+
"""
|
| 94 |
+
|
| 95 |
+
gr.ChatInterface(
|
| 96 |
+
fn=generate,
|
| 97 |
+
chatbot=gr.Chatbot(show_label=False, show_share_button=False, show_copy_button=True, likeable=True, layout="panel"),
|
| 98 |
+
additional_inputs=additional_inputs,
|
| 99 |
+
title="Mixtral-8x7B-Chat",
|
| 100 |
+
examples=examples,
|
| 101 |
+
description=description,
|
| 102 |
+
concurrency_limit=20,
|
| 103 |
+
).launch(show_api=False)
|