noorulamean444's picture
Update utils.py
b4fb210 verified
raw
history blame
No virus
2.99 kB
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