|
| 1 | +from huggingface_hub import login |
| 2 | +import gradio as gr |
| 3 | +import torch |
| 4 | +from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline |
| 5 | + |
| 6 | +# Log in to Hugging Face Hub |
| 7 | +login() |
| 8 | + |
| 9 | +# Determine the device to use (GPU if available, otherwise CPU) |
| 10 | +device = 0 if torch.cuda.is_available() else -1 |
| 11 | + |
| 12 | +# Dictionary mapping model names to their Hugging Face Hub identifiers |
| 13 | +llama_models = { |
| 14 | + "Llama 3 70B Instruct": "meta-llama/Meta-Llama-3-70B-Instruct", |
| 15 | + "Llama 3 8B Instruct": "meta-llama/Meta-Llama-3-8B-Instruct", |
| 16 | + "Llama 3.1 70B Instruct": "meta-llama/Llama-3.1-70B-Instruct", |
| 17 | + "Llama 3.1 8B Instruct": "meta-llama/Llama-3.1-8B-Instruct", |
| 18 | + "Llama 3.2 3B Instruct": "meta-llama/Llama-3.2-3B-Instruct", |
| 19 | + "Llama 3.2 1B Instruct": "meta-llama/Llama-3.2-1B-Instruct", |
| 20 | +} |
| 21 | + |
| 22 | +# Function to load the model and tokenizer |
| 23 | +def load_model(model_name): |
| 24 | + tokenizer = AutoTokenizer.from_pretrained(model_name) |
| 25 | + model = AutoModelForCausalLM.from_pretrained(model_name) |
| 26 | + generator = pipeline('text-generation', model=model, tokenizer=tokenizer, device=device) |
| 27 | + return generator |
| 28 | + |
| 29 | +# Cache to store loaded models |
| 30 | +model_cache = {} |
| 31 | + |
| 32 | +# Function to generate chat responses |
| 33 | +def generate_chat(user_input, history, model_choice): |
| 34 | + # Load the model if not already cached |
| 35 | + if model_choice not in model_cache: |
| 36 | + model_cache[model_choice] = load_model(llama_models[model_choice]) |
| 37 | + generator = model_cache[model_choice] |
| 38 | + |
| 39 | + # Initial system prompt |
| 40 | + system_prompt = {"role": "system", "content": "You are a helpful assistant"} |
| 41 | + |
| 42 | + # Initialize history if it's None |
| 43 | + if history is None: |
| 44 | + history = [system_prompt] |
| 45 | + |
| 46 | + # Append user input to history |
| 47 | + history.append({"role": "user", "content": user_input}) |
| 48 | + |
| 49 | + # Generate response using the model |
| 50 | + response = generator( |
| 51 | + history, |
| 52 | + max_length=512, |
| 53 | + pad_token_id=generator.tokenizer.eos_token_id, |
| 54 | + do_sample=True, |
| 55 | + temperature=0.7, |
| 56 | + top_p=0.9 |
| 57 | + )[-1]["generated_text"][-1]["content"] |
| 58 | + |
| 59 | + # Append model response to history |
| 60 | + history.append({"role": "assistant", "content": response}) |
| 61 | + |
| 62 | + return history |
| 63 | + |
| 64 | +# Create Gradio interface |
| 65 | +with gr.Blocks() as demo: |
| 66 | + gr.Markdown("<h1><center>Chat with Llama Models</center></h1>") |
| 67 | + |
| 68 | + # Dropdown to select model |
| 69 | + model_choice = gr.Dropdown(list(llama_models.keys()), label="Select Llama Model") |
| 70 | + # Chatbot interface |
| 71 | + chatbot = gr.Chatbot(label="Chatbot Interface", type="messages") |
| 72 | + # Textbox for user input |
| 73 | + txt_input = gr.Textbox(show_label=False, placeholder="Type your message here...") |
| 74 | + |
| 75 | + # Function to handle user input and generate response |
| 76 | + def respond(user_input, chat_history, model_choice): |
| 77 | + if model_choice is None: |
| 78 | + model_choice = list(llama_models.keys())[0] |
| 79 | + updated_history = generate_chat(user_input, chat_history, model_choice) |
| 80 | + return "", updated_history |
| 81 | + |
| 82 | + # Submit user input on pressing Enter |
| 83 | + txt_input.submit(respond, [txt_input, chatbot, model_choice], [txt_input, chatbot]) |
| 84 | + # Button to submit user input |
| 85 | + submit_btn = gr.Button("Submit") |
| 86 | + submit_btn.click(respond, [txt_input, chatbot, model_choice], [txt_input, chatbot]) |
| 87 | + |
| 88 | +# Launch the Gradio demo |
| 89 | +demo.launch() |
0 commit comments