Changed dataset to be pulled from hugging face
Browse files12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758import transformersimport torchimport gradio as grfrom datasets import load_dataset# Load the model once when the script startsmodel_id = "meta-llama/Meta-Llama-3.1-8B-Instruct"# Load the model into memory (on GPU if available)pipeline = transformers.pipeline("text-generation",model=model_id,model_kwargs={"torch_dtype": torch.bfloat16},device_map="auto", # Auto-detect GPU)# Load the dataset from Hugging Facedataset = load_dataset("quantumminds/cisco_cli_commands")# Function to search the dataset for a matching commanddef search_dataset(user_input):# Check if any command in the dataset matches the user inputfor entry in dataset['train']: # assuming the dataset is in the 'train' splitif entry["command"].lower() in user_input.lower(): # Match the command with user input (case-insensitive)return f"**Command:** {entry['command']}\n\n**Description:** {entry['description']}\n\n**Example:** {entry['examples'][0]['example_command'] if 'examples' in entry else 'No example available'}"return None # No match found# Function to generate response using the dataset or fallback to the pipelinedef generate_response(user_input):# First, try to find a match in the datasetdataset_response = search_dataset(user_input)if dataset_response:return dataset_response# If no match, fallback to the LLMmessages = [{"role": "system", "content": "You are a pirate chatbot who specializes in Cisco switch and router configurations"},{"role": "user", "content": user_input},]# Generate the response from the LLMoutputs = pipeline(messages, max_new_tokens=256)# Return the generated textreturn outputs[0]["generated_text"]# Create Gradio interfaceiface = gr.Interface(fn=generate_response, # Function to callinputs=gr.Textbox(lines=2, placeholder="Enter your Cisco switch/router question here..."), # Updated Textbox inputoutputs="text", # Text outputtitle="Cisco Configuration Assistant", # Title for the UIdescription="Ask the chatbot questions about Cisco switch/router configurations", # Description)# Launch the Gradio appiface.launch()123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960import transformersimport torchimport gradio as grfrom datasets import load_dataset# Remember to add access token to huggingface-cli login# Load the model once when the script startsmodel_id = "meta-llama/Meta-Llama-3.1-8B-Instruct"# Load the model into memory (on GPU if available)pipeline = transformers.pipeline("text-generation",model=model_id,model_kwargs={"torch_dtype": torch.bfloat16},device_map="auto", # Auto-detect GPU)# Load the dataset from Hugging Facedataset = load_dataset("quantumminds/cisco_cli_commands")# Function to search the dataset for a matching commanddef search_dataset(user_input):# Check if any command in the dataset matches the user inputfor entry in dataset['train']: # assuming the dataset is in the 'train' splitif entry["command"].lower() in user_input.lower(): # Match the command with user input (case-insensitive)return f"**Command:** {entry['command']}\n\n**Description:** {entry['description']}\n\n**Example:** {entry['examples'][0]['example_command'] if 'examples' in entry else 'No example available'}"return None # If no match found# Function to generate response using the dataset or fallback to the pipelinedef generate_response(user_input):# First, try to find a match in the datasetdataset_response = search_dataset(user_input)if dataset_response:return dataset_response# If no match, fallback to the LLMmessages = [{"role": "system", "content": "You are a pirate chatbot who specializes in Cisco switch and router configurations"},{"role": "user", "content": user_input},]# Generate the response from the LLMoutputs = pipeline(messages, max_new_tokens=256)# Return the generated textreturn outputs[0]["generated_text"]# Create Gradio interfaceiface = gr.Interface(fn=generate_response, # Function to callinputs=gr.Textbox(lines=2, placeholder="Enter your Cisco switch/router question here..."),outputs="text",title="Cisco Configuration Assistant", # Title for the UIdescription="Ask the chatbot questions about Cisco switch/router configurations",)# Launch the Gradio appiface.launch()
main.py+60-58