File size: 6,154 Bytes
6e9d128
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import pandas as pd
import re
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer

# Constants
MODEL_ID = "TinyLlama/TinyLlama-1.1B-Chat-v1.0"  # Open-access model
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
MAX_LENGTH = 2048
MAX_NEW_TOKENS = 512

# Global variables to store model and tokenizer
model = None
tokenizer = None

def initialize_model():
    """Initialize the model and tokenizer"""
    global model, tokenizer
    
    if model is None or tokenizer is None:
        try:
            # Load tokenizer
            tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
            
            # Load model without quantization to avoid compatibility issues
            model = AutoModelForCausalLM.from_pretrained(
                MODEL_ID,
                torch_dtype=torch.float32,  # Use regular float instead of bfloat16
                device_map=DEVICE
                # Removed quantization parameters
            )
            
            return True
        except Exception as e:
            print(f"Error initializing model: {str(e)}")
            return False
    
    return True

def get_dataframe_info(df):
    """Get information about the dataframe to include in the prompt"""
    # Basic dataframe info
    num_rows, num_cols = df.shape
    
    # Column names and sample values
    columns_info = []
    for col in df.columns:
        # Get column type
        col_type = df[col].dtype
        
        # Get sample values (non-null)
        non_null_values = df[col].dropna()
        if len(non_null_values) > 0:
            sample_values = non_null_values.iloc[:3].tolist()
            sample_str = ", ".join([str(val) for val in sample_values])
        else:
            sample_str = "No non-null values"
        
        columns_info.append(f"- {col} ({col_type}): Example values: {sample_str}")
    
    # Combine all info
    info_text = f"Dataset has {num_rows} rows and {num_cols} columns.\n\n"
    info_text += "Column information:\n" + "\n".join(columns_info)
    
    return info_text

def create_prompt(question, df, column_mapping):
    """Create a formatted prompt for the LLM"""
    # Get dataframe info
    df_info = get_dataframe_info(df)
    
    # Determine if this is a chart request
    is_chart_request = any(chart_type in question.lower() for chart_type in [
        "chart", "plot", "graph", "visualize", "visualization", "histogram", "bar", "pie", "scatter", "line"
    ])
    
    # Create the complete prompt - adjusted for TinyLlama's format
    prompt = f"""<human>
You are a helpful data analysis assistant. Your task is to answer questions about a dataset.
    
Dataset Information:
{df_info}

The original column names have been cleaned to remove spaces and special characters. 
Here's the mapping from original to cleaned names:
{', '.join([f"'{k}' -> '{v}'" for k, v in column_mapping.items()])}

User Question: {question}

Instructions:
1. Provide a clear, direct answer to the question based on the dataset information.
2. If the question asks for a chart or visualization:
   - ONLY specify which chart type would be best (bar chart, histogram, etc.)
   - ONLY mention which columns should be used for the visualization
   - DO NOT provide any code snippets
   - DO NOT explain how to create the chart
   - Be very brief and direct
3. If asked for data that requires filtering or specific conditions, specify the condition clearly.
4. If the question cannot be answered from the given dataset, explain why.
5. Keep your response concise and focused.
</human>

<assistant>
"""
    
    return prompt

def extract_chart_suggestion(response):
    """
    Extract chart suggestion from LLM response
    Returns a dict with chart type and relevant columns if found
    """
    # Look for common chart type mentions
    chart_patterns = {
        "bar chart": r"bar\s*chart",
        "histogram": r"histogram",
        "line chart": r"line\s*chart",
        "scatter plot": r"scatter\s*plot",
        "pie chart": r"pie\s*chart",
        "box plot": r"box\s*plot"
    }
    
    chart_info = {"type": None, "columns": []}
    
    # Check for chart type mentions
    for chart_type, pattern in chart_patterns.items():
        if re.search(pattern, response, re.IGNORECASE):
            chart_info["type"] = chart_type
            break
    
    # If chart type found, try to extract columns
    if chart_info["type"]:
        # This is a simplified column extraction and would need to be more robust in production
        # Look for column names mentioned after the chart type
        full_text_after_chart = response[response.lower().find(chart_info["type"]):]
        
        # Simple heuristic: look for words that might be column names
        words = re.findall(r'\b[a-z][a-z0-9_]*\b', full_text_after_chart.lower())
        chart_info["columns"] = words[:3]  # Take up to 3 potential column names
    
    return chart_info

def generate_response(question, df, column_mapping):
    """Generate a response from the LLM for the given question"""
    # Initialize model if needed
    if not initialize_model():
        return "Failed to initialize the language model. Please try again later."
    
    # Create the prompt
    prompt = create_prompt(question, df, column_mapping)
    
    try:
        # Encode the prompt
        inputs = tokenizer(prompt, return_tensors="pt").to(DEVICE)
        
        # Generate response
        with torch.no_grad():
            output = model.generate(
                **inputs,
                max_length=MAX_LENGTH,
                max_new_tokens=MAX_NEW_TOKENS,
                temperature=0.7,
                top_p=0.9,
                do_sample=True
            )
        
        # Decode the response
        response = tokenizer.decode(output[0], skip_special_tokens=True)
        
        # Extract just the assistant's reply (after the prompt)
        # Adjusted for TinyLlama format
        if "<assistant>" in response:
            response = response.split("<assistant>")[-1].strip()
        
        return response
    
    except Exception as e:
        return f"Error generating response: {str(e)}"