{
  "prefix": "# Path: codeviz\\app.py\n# Compare this snippet from codeviz\\predictions.py:\n# import json\n# import sys\n# import time\n# from manifest import Manifest\n# \n# sys.path.append(__file__ + \"/..\")\n# from common import module_codes, module_deps, module_categories, data_dir, cur_dir\n# \n# gold_annots = json.loads(open(data_dir / \"gold_annotations.js\").read().replace(\"let gold_annotations = \", \"\"))\n# \n# M = Manifest(\n#     client_name = \"openai\",\n#     client_connection = open(cur_dir / \".openai-api-key\").read().strip(),\n#     cache_name = \"sqlite\",\n#     cache_connection = \"codeviz_openai_cache.db\",\n#     engine = \"code-davinci-002\",\n# )\n# \n# def predict_with_retries(*args, **kwargs):\n#     for _ in range(5):\n#         try:\n#             return M.run(*args, **kwargs)\n#         except Exception as e:\n#             if \"too many requests\" in str(e).lower():\n#                 print(\"Too many requests, waiting 30 seconds...\")\n#                 time.sleep(30)\n#                 continue\n#             else:\n#                 raise e\n#     raise Exception(\"Too many retries\")\n# \n# def collect_module_prediction_context(module_id):\n#     module_exports = module_deps[module_id][\"exports\"]\n#     module_exports = [m for m in module_exports if m != \"default\" and \"complex-export\" not in m]\n#     if len(module_exports) == 0:\n#         module_exports = \"\"\n#     else:\n#         module_exports = \"It exports the following symbols: \" + \", \".join(module_exports)\n#     \n#     # get module snippet\n#     module_code_snippet = module_codes[module_id]\n#     # snip to first 50 lines:\n#     module_code_snippet = module_code_snippet.split(\"\\n\")\n#     if len(module_code_snippet) > 50:\n#         module_code_snippet = \"\\n\".join(module_code_snippet[:50]) + \"\\n...\"\n#     else:\n#         module_code_snippet = \"\\n\".join(module_code_snippet)\n#     \n#     return {\"exports\": module_exports, \"snippet\": module_code_snippet}\n# \n# #### Name prediction ####\n# \n# def _get_prompt_for_module_name_prediction(module_id):\n#     context = collect_module_prediction_context(module_id)\n#     module_exports = context[\"exports\"]\n#     module_code_snippet = context[\"snippet\"]\n# \n#     prompt = f\"\"\"\\\n# Consider the code snippet of an unmodule named.\n# \nimport json\nfrom flask import Flask, render_template, request, send_from_directory\nfrom common import *\nfrom predictions import predict_snippet_description, predict_module_name\n\napp = Flask(__name__)\n\[email protected]('/')\ndef home():\n    return render_template('code-viz.html')\n\[email protected]('/data/<path:filename>')\ndef get_data_files(filename):\n    return send_from_directory(data_dir, filename)\n\[email protected]('/api/describe_snippet', methods=['POST'])\ndef describe_snippet():\n    module_id = request.json['module_id']\n    module_name = request.json['module_name']\n    snippet = request.json['snippet']\n    description = predict_snippet_description(\n        module_id,\n        module_name,\n        snippet,\n    )\n    return json.dumps({'description': description})\n\n# predict name of a module given its id\[email protected]('/api/predict_module_name', methods=['POST'])\ndef suggest_module_name():\n    module_id = request.json['module_id']\n    module_name = predict_module_name(module_id)\n",
  "suffix": "if __name__ == '__main__':\r\n    app.run(debug=True)",
  "isFimEnabled": true,
  "promptElementRanges": [
    { "kind": "PathMarker", "start": 0, "end": 23 },
    { "kind": "SimilarFile", "start": 23, "end": 2219 },
    { "kind": "BeforeCursor", "start": 2219, "end": 3142 }
  ]
}

Expanded:

# Path: codeviz\app.py
# Compare this snippet from codeviz\predictions.py:
# import json
# import sys
# import time
# from manifest import Manifest
# 
# sys.path.append(__file__ + "/..")
# from common import module_codes, module_deps, module_categories, data_dir, cur_dir
# 
# gold_annots = json.loads(open(data_dir / "gold_annotations.js").read().replace("let gold_annotations = ", ""))
# 
# M = Manifest(
#     client_name = "openai",
#     client_connection = open(cur_dir / ".openai-api-key").read().strip(),
#     cache_name = "sqlite",
#     cache_connection = "codeviz_openai_cache.db",
#     engine = "code-davinci-002",
# )
# 
# def predict_with_retries(*args, **kwargs):
#     for _ in range(5):
#         try:
#             return M.run(*args, **kwargs)
#         except Exception as e:
#             if "too many requests" in str(e).lower():
#                 print("Too many requests, waiting 30 seconds...")
#                 time.sleep(30)
#                 continue
#             else:
#                 raise e
#     raise Exception("Too many retries")
# 
# def collect_module_prediction_context(module_id):
#     module_exports = module_deps[module_id]["exports"]
#     module_exports = [m for m in module_exports if m != "default" and "complex-export" not in m]
#     if len(module_exports) == 0:
#         module_exports = ""
#     else:
#         module_exports = "It exports the following symbols: " + ", ".join(module_exports)
#     
#     # get module snippet
#     module_code_snippet = module_codes[module_id]
#     # snip to first 50 lines:
#     module_code_snippet = module_code_snippet.split("\n")
#     if len(module_code_snippet) > 50:
#         module_code_snippet = "\n".join(module_code_snippet[:50]) + "\n..."
#     else:
#         module_code_snippet = "\n".join(module_code_snippet)
#     
#     return {"exports": module_exports, "snippet": module_code_snippet}
# 
# #### Name prediction ####
# 
# def _get_prompt_for_module_name_prediction(module_id):
#     context = collect_module_prediction_context(module_id)
#     module_exports = context["exports"]
#     module_code_snippet = context["snippet"]
# 
#     prompt = f"""\
# Consider the code snippet of an unmodule named.
# 
import json
from flask import Flask, render_template, request, send_from_directory
from common import *
from predictions import predict_snippet_description, predict_module_name

app = Flask(__name__)

@app.route('/')
def home():
    return render_template('code-viz.html')

@app.route('/data/<path:filename>')
def get_data_files(filename):
    return send_from_directory(data_dir, filename)

@app.route('/api/describe_snippet', methods=['POST'])
def describe_snippet():
    module_id = request.json['module_id']
    module_name = request.json['module_name']
    snippet = request.json['snippet']
    description = predict_snippet_description(
        module_id,
        module_name,
        snippet,
    )
    return json.dumps({'description': description})

# predict name of a module given its id
@app.route('/api/predict_module_name', methods=['POST'])
def suggest_module_name():
    module_id = request.json['module_id']
    module_name = predict_module_name(module_id)