Update app.py
Browse files
app.py
CHANGED
|
@@ -1,64 +1,64 @@
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from
|
|
|
|
| 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 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
messages =
|
| 19 |
|
| 20 |
-
for
|
| 21 |
-
|
| 22 |
-
messages.append({"role": "user", "content": val[0]})
|
| 23 |
-
if val[1]:
|
| 24 |
-
messages.append({"role": "assistant", "content": val[1]})
|
| 25 |
|
| 26 |
-
messages
|
| 27 |
|
| 28 |
-
|
| 29 |
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
temperature=temperature,
|
| 35 |
top_p=top_p,
|
| 36 |
-
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
| 41 |
|
| 42 |
|
| 43 |
-
|
| 44 |
-
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
| 45 |
-
"""
|
| 46 |
-
demo = gr.ChatInterface(
|
| 47 |
respond,
|
|
|
|
| 48 |
additional_inputs=[
|
| 49 |
-
gr.Textbox(value="You are a
|
| 50 |
-
gr.Slider(
|
| 51 |
-
gr.Slider(
|
| 52 |
-
gr.Slider(
|
| 53 |
-
minimum=0.1,
|
| 54 |
-
maximum=1.0,
|
| 55 |
-
value=0.95,
|
| 56 |
-
step=0.05,
|
| 57 |
-
label="Top-p (nucleus sampling)",
|
| 58 |
-
),
|
| 59 |
],
|
| 60 |
)
|
| 61 |
|
| 62 |
-
|
| 63 |
if __name__ == "__main__":
|
| 64 |
-
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import torch
|
| 3 |
import gradio as gr
|
| 4 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, TextIteratorStreamer
|
| 5 |
+
from threading import Thread
|
| 6 |
|
| 7 |
+
MODEL_NAME = "S1mp1eXXX/Nimi-1b-thinking"
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
+
# Load once at startup (important)
|
| 10 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
| 11 |
|
| 12 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 13 |
+
MODEL_NAME,
|
| 14 |
+
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
|
| 15 |
+
device_map="auto"
|
| 16 |
+
)
|
| 17 |
+
|
| 18 |
+
def respond(message, history, system_message, max_tokens, temperature, top_p):
|
| 19 |
+
|
| 20 |
+
messages = system_message + "\n"
|
| 21 |
|
| 22 |
+
for h in history:
|
| 23 |
+
messages += f"{h['role']}: {h['content']}\n"
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
+
messages += f"user: {message}\nassistant:"
|
| 26 |
|
| 27 |
+
inputs = tokenizer(messages, return_tensors="pt").to(model.device)
|
| 28 |
|
| 29 |
+
streamer = TextIteratorStreamer(
|
| 30 |
+
tokenizer,
|
| 31 |
+
skip_prompt=True,
|
| 32 |
+
skip_special_tokens=True
|
| 33 |
+
)
|
| 34 |
+
|
| 35 |
+
generation_kwargs = dict(
|
| 36 |
+
**inputs,
|
| 37 |
+
max_new_tokens=max_tokens,
|
| 38 |
temperature=temperature,
|
| 39 |
top_p=top_p,
|
| 40 |
+
streamer=streamer
|
| 41 |
+
)
|
| 42 |
+
|
| 43 |
+
thread = Thread(target=model.generate, kwargs=generation_kwargs)
|
| 44 |
+
thread.start()
|
| 45 |
|
| 46 |
+
partial_output = ""
|
| 47 |
+
for new_token in streamer:
|
| 48 |
+
partial_output += new_token
|
| 49 |
+
yield partial_output
|
| 50 |
|
| 51 |
|
| 52 |
+
chatbot = gr.ChatInterface(
|
|
|
|
|
|
|
|
|
|
| 53 |
respond,
|
| 54 |
+
type="messages",
|
| 55 |
additional_inputs=[
|
| 56 |
+
gr.Textbox(value="You are a helpful assistant.", label="System message"),
|
| 57 |
+
gr.Slider(1, 2048, value=512, step=1, label="Max new tokens"),
|
| 58 |
+
gr.Slider(0.1, 4.0, value=0.7, step=0.1, label="Temperature"),
|
| 59 |
+
gr.Slider(0.1, 1.0, value=0.95, step=0.05, label="Top-p"),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
],
|
| 61 |
)
|
| 62 |
|
|
|
|
| 63 |
if __name__ == "__main__":
|
| 64 |
+
chatbot.launch()
|