File size: 3,314 Bytes
638a596
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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

import re
from dotenv import load_dotenv 
import requests
import json
import os
import logging
import io
import sys
from api_client import predict_deepseek,predict_gpt

load_dotenv()

if os.environ.get("method") =="local":
    from model import generate_response



from prompt_templates import prompt_template_textual,prompt_template_visual,description_template,suggestion_template,libraries


def describe_file(df,filename):
    first_row = df.iloc[0].to_dict()
    prompt = description_template.format(filename=filename,example_row=first_row)
    response = predict_gpt(prompt)
    return response

def suggest_questions(df,filename):
    example_row = dict(df.iloc[0])
    prompt = suggestion_template.format(filename=filename,example_row=example_row)
    response = predict_gpt(prompt)
    return response

def extract_code(text):
    try:
        matches = []
        pattern = r"```python(.*?)```"
        if text:
            matches = re.findall(pattern, text, re.DOTALL)
        if matches:
            return matches[0]
        else:
            raise Exception("Error extracting code: No match")
    except Exception as e:
        raise Exception("Error extracting code: ",e) from e
    
def execute(code,namespace):
    try:
        buffer = io.StringIO()
        sys.stdout = buffer
        exec(libraries+code,namespace)

        sys.stdout = sys.__stdout__

        return buffer.getvalue()

    except Exception as e:
        raise Exception("Error executing: ",e) from e



def run(namespace,description,columns,question,method):
    try:
        if question.lower().startswith('plot'):
            prompt = prompt_template_visual.format(description=description,columns=columns,question=question)
        else:
            prompt = prompt_template_textual.format(description=description,columns=columns,question=question)
        full_response= None
        extracted_code= None
        execution= None
        error = None
        try:
            if method == 'server':
                request = {
                'url' : os.environ.get("MODEL_URL"),
                'payload' : json.dumps({"prompt": prompt}),
                'headers' : {
                'Content-Type': 'application/json'
                }}
                full_response = requests.request("POST", request['url'], headers=request['headers'], data=request['payload']).json()["response"]
            
            elif method == 'local':
                full_response = generate_response(prompt)

            elif method == 'api':
                full_response = predict_deepseek(prompt)

            else:
                return {'execution': 'Wrong model method'}
            
            extracted_code = extract_code(full_response)
            execution = execute(extracted_code,namespace)
        
        except Exception as e:
                error = e
        
        

        data = {   
            'question': question,
            'prompt':prompt,
            'full_response': full_response,
            'extracted_code': extracted_code,
            'execution': execution,
            'error': error
                }

        logging.info(data)
        with open("log.json", 'w') as file:
            json.dump(data, file, indent=4) 
        
        return data

    except Exception as e:
        print(e)