Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,64 +1,84 @@
|
|
| 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("GoofyLM/gonzalez-v1")
|
| 8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
def respond(
|
| 11 |
-
message,
|
| 12 |
-
history: list[tuple[str, str]],
|
| 13 |
-
system_message,
|
| 14 |
-
max_tokens,
|
| 15 |
-
temperature,
|
| 16 |
top_p,
|
| 17 |
):
|
|
|
|
| 18 |
messages = [{"role": "system", "content": system_message}]
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
messages.append({"role": "assistant", "content": val[1]})
|
| 25 |
-
|
| 26 |
messages.append({"role": "user", "content": message})
|
| 27 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
response = ""
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
stream=True,
|
| 34 |
-
temperature=temperature,
|
| 35 |
-
top_p=top_p,
|
| 36 |
):
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
"""
|
| 46 |
demo = gr.ChatInterface(
|
| 47 |
respond,
|
| 48 |
additional_inputs=[
|
| 49 |
-
gr.Textbox(value="You are Gonzalez.", label="System message"),
|
| 50 |
-
gr.Slider(minimum=1, maximum=2048, value=
|
| 51 |
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 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 |
-
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 3 |
+
import torch
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
+
# Load model and tokenizer locally
|
| 6 |
+
model_name = "GoofyLM/gonzalez-v1"
|
| 7 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 8 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 9 |
+
model_name,
|
| 10 |
+
torch_dtype=torch.float16, # Use float16 for efficiency
|
| 11 |
+
device_map="auto" # Automatically distribute across available GPUs/devices
|
| 12 |
+
)
|
| 13 |
|
| 14 |
def respond(
|
| 15 |
+
message,
|
| 16 |
+
history: list[tuple[str, str]],
|
| 17 |
+
system_message,
|
| 18 |
+
max_tokens,
|
| 19 |
+
temperature,
|
| 20 |
top_p,
|
| 21 |
):
|
| 22 |
+
# Format messages for the model
|
| 23 |
messages = [{"role": "system", "content": system_message}]
|
| 24 |
+
for user_msg, assistant_msg in history:
|
| 25 |
+
if user_msg:
|
| 26 |
+
messages.append({"role": "user", "content": user_msg})
|
| 27 |
+
if assistant_msg:
|
| 28 |
+
messages.append({"role": "assistant", "content": assistant_msg})
|
|
|
|
|
|
|
| 29 |
messages.append({"role": "user", "content": message})
|
| 30 |
|
| 31 |
+
# Convert messages to model input format
|
| 32 |
+
chat_template = tokenizer.apply_chat_template(
|
| 33 |
+
messages,
|
| 34 |
+
tokenize=False,
|
| 35 |
+
add_generation_prompt=True
|
| 36 |
+
)
|
| 37 |
+
|
| 38 |
+
# Tokenize the input
|
| 39 |
+
inputs = tokenizer(chat_template, return_tensors="pt").to(model.device)
|
| 40 |
+
|
| 41 |
+
# Generate response with streaming
|
| 42 |
+
input_length = inputs.input_ids.shape[1]
|
| 43 |
+
generated_tokens = []
|
| 44 |
+
|
| 45 |
+
# Set up generation parameters
|
| 46 |
+
gen_kwargs = {
|
| 47 |
+
"max_new_tokens": max_tokens,
|
| 48 |
+
"temperature": temperature,
|
| 49 |
+
"top_p": top_p,
|
| 50 |
+
"do_sample": temperature > 0,
|
| 51 |
+
"pad_token_id": tokenizer.eos_token_id,
|
| 52 |
+
}
|
| 53 |
+
|
| 54 |
+
# Stream the generation
|
| 55 |
response = ""
|
| 56 |
+
for output in model.generate(
|
| 57 |
+
**inputs,
|
| 58 |
+
**gen_kwargs,
|
| 59 |
+
streamer=transformers.TextStreamer(tokenizer, skip_prompt=True),
|
|
|
|
|
|
|
|
|
|
| 60 |
):
|
| 61 |
+
# Skip input tokens
|
| 62 |
+
if len(output) <= input_length:
|
| 63 |
+
continue
|
| 64 |
+
|
| 65 |
+
# Get new tokens
|
| 66 |
+
new_tokens = output[input_length:]
|
| 67 |
+
decoded = tokenizer.decode(new_tokens, skip_special_tokens=True)
|
| 68 |
+
response = decoded
|
| 69 |
yield response
|
| 70 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
demo = gr.ChatInterface(
|
| 72 |
respond,
|
| 73 |
additional_inputs=[
|
| 74 |
+
gr.Textbox(value="You are a Gonzalez-v1.", label="System message"),
|
| 75 |
+
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 76 |
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 77 |
gr.Slider(
|
| 78 |
+
minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
),
|
| 80 |
],
|
| 81 |
)
|
| 82 |
|
|
|
|
| 83 |
if __name__ == "__main__":
|
| 84 |
+
demo.launch()
|