Spaces:
Sleeping
Sleeping
| import json | |
| from threading import Thread | |
| import gradio as gr | |
| import spaces | |
| from transformers import ( | |
| AutoModelForCausalLM, | |
| AutoTokenizer, | |
| TextIteratorStreamer, | |
| ) | |
| MAX_NEW_TOKENS = 8192 | |
| MODEL_NAME = "Azure99/Blossom-V6.2-14B" | |
| model = AutoModelForCausalLM.from_pretrained( | |
| MODEL_NAME, | |
| torch_dtype="auto", | |
| device_map="auto", | |
| ) | |
| tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME) | |
| def get_messages(user, history): | |
| try: | |
| parsed_body = json.loads(user) | |
| if parsed_body.get("by_json_str"): | |
| return parsed_body["messages"] | |
| except: | |
| pass | |
| messages = [] | |
| messages.extend(history or []) | |
| messages.append({"role": "user", "content": user}) | |
| return messages | |
| def chat(user, history, temperature, top_p, repetition_penalty): | |
| streamer = TextIteratorStreamer( | |
| tokenizer, skip_prompt=True, skip_special_tokens=True | |
| ) | |
| messages = get_messages(user, history) | |
| print(f"Messages: {messages}") | |
| input_ids = tokenizer.apply_chat_template(messages, return_tensors="pt").to( | |
| model.device | |
| ) | |
| generation_kwargs = dict( | |
| input_ids=input_ids, | |
| streamer=streamer, | |
| do_sample=True, | |
| max_new_tokens=MAX_NEW_TOKENS, | |
| temperature=temperature, | |
| top_p=top_p, | |
| repetition_penalty=repetition_penalty, | |
| ) | |
| Thread(target=model.generate, kwargs=generation_kwargs).start() | |
| outputs = "" | |
| for new_text in streamer: | |
| outputs += new_text | |
| yield outputs | |
| additional_inputs = [ | |
| gr.Slider( | |
| label="Temperature", | |
| value=0.5, | |
| minimum=0.0, | |
| maximum=1.0, | |
| step=0.05, | |
| interactive=True, | |
| info="Controls randomness in choosing words.", | |
| ), | |
| gr.Slider( | |
| label="Top-P", | |
| value=0.85, | |
| minimum=0.0, | |
| maximum=1.0, | |
| step=0.05, | |
| interactive=True, | |
| info="Picks words until their combined probability is at least top_p.", | |
| ), | |
| gr.Slider( | |
| label="Repetition penalty", | |
| value=1.05, | |
| minimum=1.0, | |
| maximum=1.2, | |
| step=0.01, | |
| interactive=True, | |
| info="Repetition Penalty: Controls how much repetition is penalized.", | |
| ), | |
| ] | |
| gr.ChatInterface( | |
| chat, | |
| type="messages", | |
| chatbot=gr.Chatbot( | |
| show_label=False, | |
| height=500, | |
| show_copy_button=True, | |
| render_markdown=True, | |
| type="messages", | |
| latex_delimiters=[{"left": "\\[", "right": "\\]", "display": True}], | |
| ), | |
| textbox=gr.Textbox(placeholder="", container=False, scale=7), | |
| title=f"{MODEL_NAME} Demo", | |
| description="Hello, I am Blossom, an open source conversational large language model.🌠" | |
| '<a href="https://github.com/Azure99/BlossomLM">GitHub</a>', | |
| theme="soft", | |
| examples=[ | |
| ["Hello"], | |
| ["What is MBTI"], | |
| ["用Python实现二分查找"], | |
| ["为switch写一篇小红书种草文案,带上emoji"], | |
| ], | |
| cache_examples=False, | |
| additional_inputs=additional_inputs, | |
| additional_inputs_accordion=gr.Accordion(label="Config", open=True), | |
| ).queue().launch() | |