File size: 1,926 Bytes
c4868a7 b4e88f2 5eb6970 07afc51 5eb6970 07afc51 5eb6970 07afc51 5eb6970 07afc51 5eb6970 07afc51 5eb6970 07afc51 5eb6970 07afc51 5eb6970 d91393a 07afc51 5eb6970 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
import gradio as gr
from transformers import AutoTokenizer, AutoModelForCausalLM
# Load model and tokenizer
model_name = "meta-llama/Llama-2-7b-hf"
# Use Hugging Face authentication token
token = 'HUGGINGFACE_TOKEN' # Replace this with your Hugging Face token
# Load model and tokenizer from Hugging Face
tokenizer = AutoTokenizer.from_pretrained(model_name, use_auth_token=token)
model = AutoModelForCausalLM.from_pretrained(model_name, use_auth_token=token)
# Function to generate response from the model
def generate_response(user_input, chat_history=None):
if chat_history is None:
chat_history = []
# Format the input for the model
input_text = user_input + ' ' # Add a space for separation between user input and the response
# Encode the input
inputs = tokenizer.encode(input_text, return_tensors="pt")
# Generate a response from the model
outputs = model.generate(inputs, max_length=150, num_return_sequences=1, no_repeat_ngram_size=2, pad_token_id=tokenizer.eos_token_id)
# Decode the response
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
# Append the response to the chat history
chat_history.append((user_input, response))
# Return the response and updated chat history
return response, chat_history
# Gradio Interface
def respond(user_input, chat_history=None):
response, chat_history = generate_response(user_input, chat_history)
return response, chat_history
# Set up Gradio interface
iface = gr.Interface(
fn=respond,
inputs=[
gr.Textbox(label="Your Message", placeholder="Ask me anything!", lines=2),
gr.State()
],
outputs=[
gr.Textbox(label="Response", lines=3),
gr.State()
],
title="Llama-2 Chatbot",
description="Ask me anything, and I'll respond using Llama-2 model.",
live=True
)
# Launch the Gradio interface
iface.launch()
|