ModularityAI/LLama3Chat
8
1import gradio as gr2import spaces3import torch4 5import transformers6import torch7from transformers import AutoModelForCausalLM, AutoTokenizer8 9model_name = "meta-llama/Meta-Llama-3-8B-Instruct"10 11pipeline = transformers.pipeline(12 "text-generation",13 model=model_name,14 model_kwargs={"torch_dtype": torch.bfloat16},15 device="cuda",16)17 18@spaces.GPU19def chat_function(message, history, system_prompt,max_new_tokens,temperature):20 messages = [21 {"role": "system", "content": system_prompt},22 {"role": "user", "content": message},23 ]24 prompt = pipeline.tokenizer.apply_chat_template(25 messages,26 tokenize=False,27 add_generation_prompt=True28 )29 terminators = [30 pipeline.tokenizer.eos_token_id,31 pipeline.tokenizer.convert_tokens_to_ids("<|eot_id|>")32 ]33 temp = temperature + 0.134 outputs = pipeline(35 prompt,36 max_new_tokens=max_new_tokens,37 eos_token_id=terminators,38 do_sample=True,39 temperature=temp,40 top_p=0.9,41 )42 return outputs[0]["generated_text"][len(prompt):]43 44gr.ChatInterface(45 chat_function,46 chatbot=gr.Chatbot(height=400),47 textbox=gr.Textbox(placeholder="Enter message here", container=False, scale=7),48 title="LLAMA 3 8B Chat",49 description="""50 This space is dedicated for chatting with Meta's Latest LLM - Llama 8b Instruct. Find this model here: https://huggingface.co/meta-llama/Meta-Llama-3-8B-Instruct 51 Feel free to play with customization in the "Additional Inputs".52 """,53 theme="soft",54 additional_inputs=[55 gr.Textbox("You are helpful AI.", label="System Prompt"),56 gr.Slider(512, 4096, label="Max New Tokens"),57 gr.Slider(0, 1, label="Temperature")58 ]59).launch()