-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature/to gradio #63
Open
clausqr
wants to merge
14
commits into
Stability-AI:main
Choose a base branch
from
clausqr:feature/to_gradio
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
35ffade
Adds script version and gradio base
clausqr 8c600e6
Remove jupyter rests and color printing
clausqr d854ca3
Move completion to function and pass it to gradio callback
clausqr cb0cf25
Move completion to function and pass it to gradio callback
clausqr 7316082
Move parameters to gradio sliders.
clausqr 4aca708
Move parameters to gradio sliders.
clausqr c06ac7c
Add requirements.txt
clausqr cc37cc3
Add Gradio Web UI instructions to README.md
clausqr 3fee73e
Add Gradio Web UI instructions to README.md
clausqr e2d7f6e
Trim requirements.txt
clausqr edd02a1
Fix typo in README.md
clausqr 5161818
Fix code formatting
clausqr 1aadaf1
Add screenshot and reference to URL for Web UI
clausqr 017eb45
Merge branch 'Stability-AI:main' into feature/to_gradio
clausqr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
gradio==3.28.0 | ||
torch==2.0.0 | ||
transformers==4.28.1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
import gradio as gr | ||
import torch | ||
from transformers import ( | ||
AutoModelForCausalLM, | ||
AutoTokenizer, | ||
StoppingCriteria, | ||
StoppingCriteriaList, | ||
) | ||
|
||
|
||
model_name = "stabilityai/stablelm-tuned-alpha-7b" # @param ["stabilityai/stablelm-tuned-alpha-7b", "stabilityai/stablelm-base-alpha-7b", "stabilityai/stablelm-tuned-alpha-3b", "stabilityai/stablelm-base-alpha-3b"] | ||
|
||
print(f"Using `{model_name}`") | ||
|
||
# Select "big model inference" parameters | ||
torch_dtype = "float16" # @param ["float16", "bfloat16", "float"] | ||
load_in_8bit = False # @param {type:"boolean"} | ||
device_map = "auto" | ||
|
||
print(f"Loading with: `{torch_dtype=}, {load_in_8bit=}, {device_map=}`") | ||
|
||
tokenizer = AutoTokenizer.from_pretrained(model_name) | ||
model = AutoModelForCausalLM.from_pretrained( | ||
model_name, | ||
torch_dtype=getattr(torch, torch_dtype), | ||
load_in_8bit=load_in_8bit, | ||
device_map=device_map, | ||
offload_folder="./offload", | ||
) | ||
|
||
# @title Generate Text | ||
# @markdown <b>Note: The model response is colored in green</b> | ||
|
||
|
||
class StopOnTokens(StoppingCriteria): | ||
def __call__( | ||
self, input_ids: torch.LongTensor, scores: torch.FloatTensor, **kwargs | ||
) -> bool: | ||
stop_ids = [50278, 50279, 50277, 1, 0] | ||
for stop_id in stop_ids: | ||
if input_ids[0][-1] == stop_id: | ||
return True | ||
return False | ||
|
||
|
||
# Process the user prompt | ||
user_prompt = "Can you write a song about a pirate at sea?" # @param {type:"string"} | ||
if "tuned" in model_name: | ||
# Add system prompt for chat tuned models | ||
system_prompt = """<|SYSTEM|># StableLM Tuned (Alpha version) | ||
- StableLM is a helpful and harmless open-source AI language model developed by StabilityAI. | ||
- StableLM is excited to be able to help the user, but will refuse to do anything that could be considered harmful to the user. | ||
- StableLM is more than just an information source, StableLM is also able to write poetry, short stories, and make jokes. | ||
- StableLM will refuse to participate in anything that could harm a human. | ||
""" | ||
prompt = f"{system_prompt}<|USER|>{user_prompt}<|ASSISTANT|>" | ||
else: | ||
prompt = user_prompt | ||
|
||
|
||
def complete( | ||
prompt, max_new_tokens=128, temperature=0.7, top_k=0, top_p=0.9, do_sample=True | ||
): | ||
print( | ||
f"Sampling with: `{max_new_tokens=}, {temperature=}, {top_k=}, {top_p=}, {do_sample=}`" | ||
) | ||
|
||
# Create `generate` inputs | ||
inputs = tokenizer(prompt, return_tensors="pt") | ||
inputs.to(model.device) | ||
|
||
# Generate | ||
tokens = model.generate( | ||
**inputs, | ||
max_new_tokens=max_new_tokens, | ||
temperature=temperature, | ||
top_k=top_k, | ||
top_p=top_p, | ||
do_sample=do_sample, | ||
pad_token_id=tokenizer.eos_token_id, | ||
stopping_criteria=StoppingCriteriaList([StopOnTokens()]), | ||
) | ||
|
||
# Extract out only the completion tokens | ||
completion_tokens = tokens[0][inputs["input_ids"].size(1) :] | ||
completion = tokenizer.decode(completion_tokens, skip_special_tokens=True) | ||
|
||
return prompt + " " + completion | ||
|
||
|
||
iface = gr.Interface( | ||
fn=complete, | ||
inputs=[ | ||
"text", | ||
gr.Slider(32, 3072, 32, default=128), | ||
gr.Slider(0.0, 1.25, 0.05, default=0.7), | ||
gr.Slider(0.0, 1.0, 0.05, default=0.0), | ||
gr.Slider(0.0, 1.0, 0.05, default=0.9), | ||
"checkbox", | ||
], | ||
outputs="text", | ||
share=True, | ||
title="StableLM", | ||
description="StableLM web ui", | ||
) | ||
|
||
iface.launch(server_port=8888) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Try to use isort and black modules to reformatting Python code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed in 5161818