File size: 2,994 Bytes
26fdb6b
351417e
26fdb6b
 
 
351417e
26fdb6b
 
351417e
26fdb6b
351417e
26fdb6b
d920f18
6c2d26d
 
 
 
 
d920f18
 
b4fb210
d920f18
 
 
 
 
b4fb210
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d920f18
 
 
 
b4fb210
d920f18
6c2d26d
d920f18
 
b4fb210
 
 
 
 
 
d920f18
b4fb210
6c2d26d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
def package_installer(import_name,package_name=None):
  import importlib
  import subprocess
  import sys
    
  package_name = package_name if package_name else import_name
  sys_output = subprocess.run([sys.executable,"-m","pip","show",import_name])
  import_status = sys_output.returncode

  if import_status == 1:
    subprocess.run([sys.executable,'-m','pip','install',package_name,'--quiet'])
  else:
      pass
  
def query(url,headers,payload):
    import requests
    response = requests.post(url, headers=headers, json=payload,timeout=120)
    return response.json()


def context_identifier(input:str,token:str):

    API_URL = "https://api-inference.huggingface.co/models/mistralai/Mistral-7B-Instruct-v0.2"
    headers = {"Authorization": f"Bearer {token}"}
    
    prompt = f"""
    <s>[INST] Your task is to identify whether the given statement belongs to previous_cell_context or notebook_cell_context.
    Given the statement, you have to give output as a single word. Watch out for keywords to indentify the context.

    'previous_cell_context keywords': 'previous response', 'previous explanation', 'above response', 'above explanation'
    'notebook_cell_context keywords': 'cell number', 'cell'

    <Examples>
    Statement: 'explain me the previous response given by you again'
    Answer: 'previous_cell_context'

    Statement: 'explain the above response in a different way'
    Answer: 'previous_cell_context'

    Statement: 'Summarize the above response in 50 words'
    Answer: 'previous_cell_context'

    Statement: 'Explain me the code present in 4th cell'
    Answer: 'notebook_cell_context'

    Statement: 'Optimize the code in cell number 24'
    Answer: 'notebook_cell_context'

    Statement: 'explain the error caused by code present in cell number 15 and compare it with previous response by you'
    Answer: 'notebook_cell_context'

    Statement: 'what are the similarities between previous response and 9th cell?'
    Answer: 'notebook_cell_context'

    The statement to answer is delimited by ####.
        
    #### {input} ####
    
    [\INST]
    """
    
    output = query(url=API_URL,headers=headers,payload={"inputs": prompt})

    output = output[0]['generated_text']
    output = output.replace(prompt,'').strip()

    if 'previous_cell_context' in output.lower():
       result = 'previous_cell_context'
    elif 'notebook_cell_context' in output.lower():
       result = 'notebook_cell_context'
    elif 'both' in output.lower():
       result = 'both'

    return result

def convert_ipynb_to_html(input_file):
    import nbformat
    import nbconvert
    # Load .ipynb file into a nbformat.NotebookNode object
    notebook = nbformat.read(input_file, as_version=4)

    # Convert using HTML exporter
    html_exporter = nbconvert.HTMLExporter()
    (body, resources) = html_exporter.from_notebook_node(notebook)
    # Write to output html file
    # with open(output_file, 'w') as f:
    #     f.write(body)
        
    return body