Spaces:
Build error
Build error
Commit ·
72ad694
0
Parent(s):
Duplicate from ysharma/RedPajama-Chat-3B
Browse files- .gitattributes +34 -0
- README.md +14 -0
- app.py +117 -0
- requirements.txt +2 -0
.gitattributes
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
*.7z filter=lfs diff=lfs merge=lfs -text
|
| 2 |
+
*.arrow filter=lfs diff=lfs merge=lfs -text
|
| 3 |
+
*.bin filter=lfs diff=lfs merge=lfs -text
|
| 4 |
+
*.bz2 filter=lfs diff=lfs merge=lfs -text
|
| 5 |
+
*.ckpt filter=lfs diff=lfs merge=lfs -text
|
| 6 |
+
*.ftz filter=lfs diff=lfs merge=lfs -text
|
| 7 |
+
*.gz filter=lfs diff=lfs merge=lfs -text
|
| 8 |
+
*.h5 filter=lfs diff=lfs merge=lfs -text
|
| 9 |
+
*.joblib filter=lfs diff=lfs merge=lfs -text
|
| 10 |
+
*.lfs.* filter=lfs diff=lfs merge=lfs -text
|
| 11 |
+
*.mlmodel filter=lfs diff=lfs merge=lfs -text
|
| 12 |
+
*.model filter=lfs diff=lfs merge=lfs -text
|
| 13 |
+
*.msgpack filter=lfs diff=lfs merge=lfs -text
|
| 14 |
+
*.npy filter=lfs diff=lfs merge=lfs -text
|
| 15 |
+
*.npz filter=lfs diff=lfs merge=lfs -text
|
| 16 |
+
*.onnx filter=lfs diff=lfs merge=lfs -text
|
| 17 |
+
*.ot filter=lfs diff=lfs merge=lfs -text
|
| 18 |
+
*.parquet filter=lfs diff=lfs merge=lfs -text
|
| 19 |
+
*.pb filter=lfs diff=lfs merge=lfs -text
|
| 20 |
+
*.pickle filter=lfs diff=lfs merge=lfs -text
|
| 21 |
+
*.pkl filter=lfs diff=lfs merge=lfs -text
|
| 22 |
+
*.pt filter=lfs diff=lfs merge=lfs -text
|
| 23 |
+
*.pth filter=lfs diff=lfs merge=lfs -text
|
| 24 |
+
*.rar filter=lfs diff=lfs merge=lfs -text
|
| 25 |
+
*.safetensors filter=lfs diff=lfs merge=lfs -text
|
| 26 |
+
saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
| 27 |
+
*.tar.* filter=lfs diff=lfs merge=lfs -text
|
| 28 |
+
*.tflite filter=lfs diff=lfs merge=lfs -text
|
| 29 |
+
*.tgz filter=lfs diff=lfs merge=lfs -text
|
| 30 |
+
*.wasm filter=lfs diff=lfs merge=lfs -text
|
| 31 |
+
*.xz filter=lfs diff=lfs merge=lfs -text
|
| 32 |
+
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 33 |
+
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 34 |
+
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
README.md
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
title: RedPajama Chat 3B
|
| 3 |
+
emoji: 👀
|
| 4 |
+
colorFrom: pink
|
| 5 |
+
colorTo: yellow
|
| 6 |
+
sdk: gradio
|
| 7 |
+
sdk_version: 3.28.3
|
| 8 |
+
app_file: app.py
|
| 9 |
+
pinned: false
|
| 10 |
+
license: mit
|
| 11 |
+
duplicated_from: ysharma/RedPajama-Chat-3B
|
| 12 |
+
---
|
| 13 |
+
|
| 14 |
+
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
app.py
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline, StoppingCriteria, StoppingCriteriaList, TextIteratorStreamer
|
| 4 |
+
import time
|
| 5 |
+
import numpy as np
|
| 6 |
+
from torch.nn import functional as F
|
| 7 |
+
import os
|
| 8 |
+
from threading import Thread
|
| 9 |
+
|
| 10 |
+
# init
|
| 11 |
+
tok = AutoTokenizer.from_pretrained("togethercomputer/RedPajama-INCITE-Chat-3B-v1")
|
| 12 |
+
m = AutoModelForCausalLM.from_pretrained("togethercomputer/RedPajama-INCITE-Chat-3B-v1", torch_dtype=torch.float16)
|
| 13 |
+
m = m.to('cuda:0')
|
| 14 |
+
|
| 15 |
+
class StopOnTokens(StoppingCriteria):
|
| 16 |
+
def __call__(self, input_ids: torch.LongTensor, scores: torch.FloatTensor, **kwargs) -> bool:
|
| 17 |
+
#stop_ids = [[29, 13961, 31], [29, 12042, 31], 1, 0]
|
| 18 |
+
stop_ids = [29, 0]
|
| 19 |
+
for stop_id in stop_ids:
|
| 20 |
+
#print(f"^^input ids - {input_ids}")
|
| 21 |
+
if input_ids[0][-1] == stop_id:
|
| 22 |
+
return True
|
| 23 |
+
return False
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
def user(message, history):
|
| 27 |
+
# Append the user's message to the conversation history
|
| 28 |
+
return "", history + [[message, ""]]
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
def chat(history, top_p, top_k, temperature):
|
| 33 |
+
# Initialize a StopOnTokens object
|
| 34 |
+
stop = StopOnTokens()
|
| 35 |
+
|
| 36 |
+
# Construct the input message string for the model by concatenating the current system message and conversation history
|
| 37 |
+
messages = "".join(["".join(["\n<human>:"+item[0], "\n<bot>:"+item[1]]) #curr_system_message +
|
| 38 |
+
for item in history])
|
| 39 |
+
|
| 40 |
+
# Tokenize the messages string
|
| 41 |
+
model_inputs = tok([messages], return_tensors="pt").to("cuda")
|
| 42 |
+
streamer = TextIteratorStreamer(
|
| 43 |
+
tok, timeout=10., skip_prompt=False, skip_special_tokens=True)
|
| 44 |
+
generate_kwargs = dict(
|
| 45 |
+
model_inputs,
|
| 46 |
+
streamer=streamer,
|
| 47 |
+
max_new_tokens=1024,
|
| 48 |
+
do_sample=True,
|
| 49 |
+
top_p=top_p, #0.95,
|
| 50 |
+
top_k=top_k, #1000,
|
| 51 |
+
temperature=temperature, #1.0,
|
| 52 |
+
num_beams=1,
|
| 53 |
+
stopping_criteria=StoppingCriteriaList([stop])
|
| 54 |
+
)
|
| 55 |
+
t = Thread(target=m.generate, kwargs=generate_kwargs)
|
| 56 |
+
t.start()
|
| 57 |
+
|
| 58 |
+
# Initialize an empty string to store the generated text
|
| 59 |
+
partial_text = ""
|
| 60 |
+
for new_text in streamer:
|
| 61 |
+
#print(new_text)
|
| 62 |
+
if new_text != '<':
|
| 63 |
+
partial_text += new_text
|
| 64 |
+
history[-1][1] = partial_text.split('<bot>:')[-1]
|
| 65 |
+
# Yield an empty string to clean up the message textbox and the updated conversation history
|
| 66 |
+
yield history
|
| 67 |
+
return partial_text
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
title = """<h1 align="center">🔥RedPajama-INCITE-Chat-3B-v1</h1><br><h2 align="center">🏃♂️💨Streaming with Transformers & Gradio💪</h2>"""
|
| 71 |
+
description = """<br><br><h3 align="center">This is a RedPajama Chat model fine-tuned using data from Dolly 2.0 and Open Assistant over the RedPajama-INCITE-Base-3B-v1 base model.</h3>"""
|
| 72 |
+
theme = gr.themes.Soft(
|
| 73 |
+
primary_hue=gr.themes.Color("#ededed", "#fee2e2", "#fecaca", "#fca5a5", "#f87171", "#ef4444", "#dc2626", "#b91c1c", "#991b1b", "#7f1d1d", "#6c1e1e"),
|
| 74 |
+
neutral_hue="red",
|
| 75 |
+
)
|
| 76 |
+
|
| 77 |
+
|
| 78 |
+
with gr.Blocks(theme=theme) as demo:
|
| 79 |
+
gr.HTML(title)
|
| 80 |
+
gr.HTML('''<center><a href="https://huggingface.co/spaces/ysharma/RedPajama-Chat-3B?duplicate=true"><img src="https://bit.ly/3gLdBN6" alt="Duplicate Space"></a>Duplicate the Space to skip the queue and run in a private space</center>''')
|
| 81 |
+
chatbot = gr.Chatbot().style(height=500)
|
| 82 |
+
with gr.Row():
|
| 83 |
+
with gr.Column():
|
| 84 |
+
msg = gr.Textbox(label="Chat Message Box", placeholder="Chat Message Box",
|
| 85 |
+
show_label=False).style(container=False)
|
| 86 |
+
with gr.Column():
|
| 87 |
+
with gr.Row():
|
| 88 |
+
submit = gr.Button("Submit")
|
| 89 |
+
stop = gr.Button("Stop")
|
| 90 |
+
clear = gr.Button("Clear")
|
| 91 |
+
|
| 92 |
+
#Advanced options - top_p, temperature, top_k
|
| 93 |
+
with gr.Accordion("Advanced Options:", open=False):
|
| 94 |
+
top_p = gr.Slider( minimum=-0, maximum=1.0, value=0.95, step=0.05, interactive=True, label="Top-p",)
|
| 95 |
+
top_k = gr.Slider(minimum=0.0, maximum=1000, value=1000, step=1, interactive=True, label="Top-k", )
|
| 96 |
+
temperature = gr.Slider( minimum=-0, maximum=5.0, value=1.0, step=0.1, interactive=True, label="Temperature",)
|
| 97 |
+
|
| 98 |
+
submit_event = msg.submit(fn=user, inputs=[msg, chatbot], outputs=[msg, chatbot], queue=False).then(
|
| 99 |
+
fn=chat, inputs=[chatbot, top_p, top_k, temperature], outputs=[chatbot], queue=True) #inputs=[system_msg, chatbot]
|
| 100 |
+
submit_click_event = submit.click(fn=user, inputs=[msg, chatbot], outputs=[msg, chatbot], queue=False).then(
|
| 101 |
+
fn=chat, inputs=[chatbot, top_p, top_k, temperature], outputs=[chatbot], queue=True) #inputs=[system_msg, chatbot]
|
| 102 |
+
stop.click(fn=None, inputs=None, outputs=None, cancels=[
|
| 103 |
+
submit_event, submit_click_event], queue=False)
|
| 104 |
+
clear.click(lambda: None, None, [chatbot], queue=False)
|
| 105 |
+
|
| 106 |
+
gr.Examples([
|
| 107 |
+
["Hello there! How are you doing?"],
|
| 108 |
+
["Can you explain to me briefly what is Python programming language?"],
|
| 109 |
+
["Explain the plot of Cinderella in a sentence."],
|
| 110 |
+
["What are some common mistakes to avoid when writing code?"],
|
| 111 |
+
["Write a 500-word blog post on “Benefits of Artificial Intelligence"]
|
| 112 |
+
], inputs=msg, label= "Click on any example and press the 'Submit' button"
|
| 113 |
+
)
|
| 114 |
+
gr.HTML(description)
|
| 115 |
+
|
| 116 |
+
demo.queue(max_size=32, concurrency_count=2)
|
| 117 |
+
demo.launch(debug=True)
|
requirements.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
torch
|
| 2 |
+
transformers
|