Views
No views yet
1# Install the required PyTorch version with CUDA support (ensure CUDA 12.1 is installed)
2!pip install torch==2.2.1 torchvision==0.17.1 torchaudio==2.2.1 --index-url https://download.pytorch.org/whl/cu121
3
4# Install AutoGPTQ for quantized model handling
5!pip install auto-gptq --no-build-isolation
6
7# Install Optimum for model optimization
8!pip install optimum1from transformers import AutoTokenizer, pipeline
2from auto_gptq import AutoGPTQForCausalLM
3import torch
4
5# Define the Alpaca-style prompt template
6alpaca_prompt = """Below is an instruction that describes a task, paired with an input that provides further context. Write a response that appropriately completes the request.
7
8### Instruction:
9{}
10
11### Input:
12{}
13
14### Response:
15"""
16
17# Model directory and tokenizer
18quantized_model_dir = "meta-llama-8b-quantized-4bit" # Path where quantized model is saved
19tokenizer = AutoTokenizer.from_pretrained(quantized_model_dir)
20
21# Load the quantized model
22model = AutoGPTQForCausalLM.from_quantized(
23 quantized_model_dir,
24 device_map="auto", # Automatically map the model to the available device (GPU or CPU)
25 torch_dtype=torch.float16, # Ensure FP16 for efficiency
26 use_safetensors=True # If you saved the model using safetensors format, set this to True
27)
28
29# Set up the text generation pipeline without specifying the device
30pipeline = pipeline(
31 "text-generation",
32 model=model,
33 tokenizer=tokenizer
34)
35
36# Function to generate SQL query from input text using the Alpaca prompt
37def generate_sql(input_text):
38 # Format the prompt
39 prompt = alpaca_prompt.format(
40 "Provide the SQL query",
41 input_text
42 )
43
44 # Generate the response using the pipeline
45 generated_text = pipeline(
46 prompt,
47 max_length=200,
48 eos_token_id=tokenizer.eos_token_id
49 )[0]["generated_text"]
50
51 # Clean the output by removing the prompt and any extra newlines
52 cleaned_output = generated_text.replace(prompt, '').strip()
53
54 return cleaned_output
55
56# Example usage
57italian_input = "Seleziona tutte le colonne della tabella table1 dove la colonna anni è uguale a 2020"
58sql_query = generate_sql(italian_input)
59print(sql_query)1italian_input = "Seleziona tutte le colonne della tabella table1 dove la colonna anni è uguale a 2020"
2sql_query = generate_sql(italian_input)
3print(sql_query)SELECT * FROM table1 WHERE anni = 2020;