Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,18 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from
|
|
|
|
|
|
|
| 3 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
def respond(
|
| 6 |
message,
|
|
@@ -14,32 +26,35 @@ def respond(
|
|
| 14 |
"""
|
| 15 |
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
|
| 16 |
"""
|
| 17 |
-
client = InferenceClient(token=hf_token.token, model="drumwell/autotrain-2duhi-5mmyz")
|
| 18 |
-
|
| 19 |
messages = [{"role": "system", "content": system_message}]
|
| 20 |
-
|
| 21 |
messages.extend(history)
|
| 22 |
-
|
| 23 |
messages.append({"role": "user", "content": message})
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
temperature=temperature,
|
| 32 |
top_p=top_p,
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 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 |
"""
|
|
@@ -65,6 +80,5 @@ with gr.Blocks() as demo:
|
|
| 65 |
gr.LoginButton()
|
| 66 |
chatbot.render()
|
| 67 |
|
| 68 |
-
|
| 69 |
if __name__ == "__main__":
|
| 70 |
-
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
|
| 3 |
+
from threading import Thread
|
| 4 |
+
import torch
|
| 5 |
|
| 6 |
+
# Load your model once at startup
|
| 7 |
+
print("Loading model...")
|
| 8 |
+
tokenizer = AutoTokenizer.from_pretrained("drumwell/autotrain-2duhi-5mmyz")
|
| 9 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 10 |
+
"drumwell/autotrain-2duhi-5mmyz",
|
| 11 |
+
device_map="auto",
|
| 12 |
+
torch_dtype=torch.float16,
|
| 13 |
+
load_in_8bit=True,
|
| 14 |
+
)
|
| 15 |
+
print("Model loaded!")
|
| 16 |
|
| 17 |
def respond(
|
| 18 |
message,
|
|
|
|
| 26 |
"""
|
| 27 |
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
|
| 28 |
"""
|
|
|
|
|
|
|
| 29 |
messages = [{"role": "system", "content": system_message}]
|
|
|
|
| 30 |
messages.extend(history)
|
|
|
|
| 31 |
messages.append({"role": "user", "content": message})
|
| 32 |
+
|
| 33 |
+
# Apply chat template
|
| 34 |
+
text = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
|
| 35 |
+
inputs = tokenizer(text, return_tensors="pt").to(model.device)
|
| 36 |
+
|
| 37 |
+
# Setup streaming
|
| 38 |
+
streamer = TextIteratorStreamer(tokenizer, skip_prompt=True, skip_special_tokens=True)
|
| 39 |
+
generation_kwargs = dict(
|
| 40 |
+
inputs,
|
| 41 |
+
streamer=streamer,
|
| 42 |
+
max_new_tokens=max_tokens,
|
| 43 |
temperature=temperature,
|
| 44 |
top_p=top_p,
|
| 45 |
+
do_sample=True,
|
| 46 |
+
repetition_penalty=1.1,
|
| 47 |
+
)
|
| 48 |
+
|
| 49 |
+
# Generate in a separate thread for streaming
|
| 50 |
+
thread = Thread(target=model.generate, kwargs=generation_kwargs)
|
| 51 |
+
thread.start()
|
| 52 |
+
|
| 53 |
+
response = ""
|
| 54 |
+
for token in streamer:
|
| 55 |
response += token
|
| 56 |
yield response
|
| 57 |
|
|
|
|
| 58 |
"""
|
| 59 |
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
| 60 |
"""
|
|
|
|
| 80 |
gr.LoginButton()
|
| 81 |
chatbot.render()
|
| 82 |
|
|
|
|
| 83 |
if __name__ == "__main__":
|
| 84 |
+
demo.launch()
|