FraRy commited on
Commit
dbe3591
·
verified ·
1 Parent(s): 0d46e81

Changed dataset to be pulled from hugging face

Browse files
Files changed (1) hide show
  1. main.py +60 -58
main.py CHANGED
@@ -1,58 +1,60 @@
1
- import transformers
2
- import torch
3
- import gradio as gr
4
- from datasets import load_dataset
5
-
6
- # Load the model once when the script starts
7
- model_id = "meta-llama/Meta-Llama-3.1-8B-Instruct"
8
-
9
- # Load the model into memory (on GPU if available)
10
- pipeline = transformers.pipeline(
11
- "text-generation",
12
- model=model_id,
13
- model_kwargs={"torch_dtype": torch.bfloat16},
14
- device_map="auto", # Auto-detect GPU
15
- )
16
-
17
- # Load the dataset from Hugging Face
18
- dataset = load_dataset("quantumminds/cisco_cli_commands")
19
-
20
- # Function to search the dataset for a matching command
21
- def search_dataset(user_input):
22
- # Check if any command in the dataset matches the user input
23
- for entry in dataset['train']: # assuming the dataset is in the 'train' split
24
- if entry["command"].lower() in user_input.lower(): # Match the command with user input (case-insensitive)
25
- 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'}"
26
- return None # No match found
27
-
28
- # Function to generate response using the dataset or fallback to the pipeline
29
- def generate_response(user_input):
30
- # First, try to find a match in the dataset
31
- dataset_response = search_dataset(user_input)
32
-
33
- if dataset_response:
34
- return dataset_response
35
-
36
- # If no match, fallback to the LLM
37
- messages = [
38
- {"role": "system", "content": "You are a pirate chatbot who specializes in Cisco switch and router configurations"},
39
- {"role": "user", "content": user_input},
40
- ]
41
-
42
- # Generate the response from the LLM
43
- outputs = pipeline(messages, max_new_tokens=256)
44
-
45
- # Return the generated text
46
- return outputs[0]["generated_text"]
47
-
48
- # Create Gradio interface
49
- iface = gr.Interface(
50
- fn=generate_response, # Function to call
51
- inputs=gr.Textbox(lines=2, placeholder="Enter your Cisco switch/router question here..."), # Updated Textbox input
52
- outputs="text", # Text output
53
- title="Cisco Configuration Assistant", # Title for the UI
54
- description="Ask the chatbot questions about Cisco switch/router configurations", # Description
55
- )
56
-
57
- # Launch the Gradio app
58
- iface.launch()
 
 
 
1
+ import transformers
2
+ import torch
3
+ import gradio as gr
4
+ from datasets import load_dataset
5
+
6
+ # Remember to add access token to huggingface-cli login
7
+
8
+ # Load the model once when the script starts
9
+ model_id = "meta-llama/Meta-Llama-3.1-8B-Instruct"
10
+
11
+ # Load the model into memory (on GPU if available)
12
+ pipeline = transformers.pipeline(
13
+ "text-generation",
14
+ model=model_id,
15
+ model_kwargs={"torch_dtype": torch.bfloat16},
16
+ device_map="auto", # Auto-detect GPU
17
+ )
18
+
19
+ # Load the dataset from Hugging Face
20
+ dataset = load_dataset("quantumminds/cisco_cli_commands")
21
+
22
+ # Function to search the dataset for a matching command
23
+ def search_dataset(user_input):
24
+ # Check if any command in the dataset matches the user input
25
+ for entry in dataset['train']: # assuming the dataset is in the 'train' split
26
+ if entry["command"].lower() in user_input.lower(): # Match the command with user input (case-insensitive)
27
+ 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'}"
28
+ return None # If no match found
29
+
30
+ # Function to generate response using the dataset or fallback to the pipeline
31
+ def generate_response(user_input):
32
+ # First, try to find a match in the dataset
33
+ dataset_response = search_dataset(user_input)
34
+
35
+ if dataset_response:
36
+ return dataset_response
37
+
38
+ # If no match, fallback to the LLM
39
+ messages = [
40
+ {"role": "system", "content": "You are a pirate chatbot who specializes in Cisco switch and router configurations"},
41
+ {"role": "user", "content": user_input},
42
+ ]
43
+
44
+ # Generate the response from the LLM
45
+ outputs = pipeline(messages, max_new_tokens=256)
46
+
47
+ # Return the generated text
48
+ return outputs[0]["generated_text"]
49
+
50
+ # Create Gradio interface
51
+ iface = gr.Interface(
52
+ fn=generate_response, # Function to call
53
+ inputs=gr.Textbox(lines=2, placeholder="Enter your Cisco switch/router question here..."),
54
+ outputs="text",
55
+ title="Cisco Configuration Assistant", # Title for the UI
56
+ description="Ask the chatbot questions about Cisco switch/router configurations",
57
+ )
58
+
59
+ # Launch the Gradio app
60
+ iface.launch()