| 1 | import datetime |
| 2 | import json |
| 3 | import os |
| 4 | import sys |
| 5 | import threading |
| 6 | import time |
| 7 | from queue import Queue |
| 8 | |
| 9 | from flask import Flask, Response, jsonify, request |
| 10 | from flask_cors import cross_origin |
| 11 | |
| 12 | sys.path.append("/opt/auto-code-rover/") |
| 13 | from test_data import RawGithubTask_for_debug, test_generate_data |
| 14 | |
| 15 | from app import globals, log |
| 16 | from app.main import get_args, run_raw_task |
| 17 | from app.model import common |
| 18 | from app.model.register import register_all_models |
| 19 | from app.raw_tasks import RawGithubTask |
| 20 | |
| 21 | app = Flask(__name__) |
| 22 | |
| 23 | |
| 24 | def initialize_thread_cost(): |
| 25 | common.thread_cost.process_cost = 0.0 |
| 26 | common.thread_cost.process_input_tokens = 0 |
| 27 | common.thread_cost.process_output_tokens = 0 |
| 28 | |
| 29 | |
| 30 | @app.route("/api/run_github_issue", methods=["POST"]) |
| 31 | @cross_origin(origin="http://localhost:3000") # nextjs cross origin |
| 32 | def run_github_issue(): |
| 33 | |
| 34 | if not request.is_json: |
| 35 | return |
| 36 | register_all_models() |
| 37 | |
| 38 | data = request.get_json() |
| 39 | args = get_args( |
| 40 | """github-issue --output-dir output --setup-dir setup --model gpt-4-0125-preview --model-temperature 0.2""" |
| 41 | ) |
| 42 | args.task_id = datetime.datetime.now().strftime("%Y-%m-%d_%H:%M:%S") |
| 43 | args.clone_link = data["repository_link"] |
| 44 | args.commit_hash = data["commit_hash"] |
| 45 | args.issue_link = data["issue_link"] |
| 46 | |
| 47 | globals.output_dir = args.output_dir |
| 48 | if globals.output_dir is not None: |
| 49 | globals.output_dir = os.path.abspath(globals.output_dir) |
| 50 | # set whether brief or verbose log |
| 51 | print_stdout: bool = not args.no_print |
| 52 | log.print_stdout = print_stdout |
| 53 | # model related |
| 54 | common.set_model(args.model) |
| 55 | # FIXME: make temperature part of the Model class |
| 56 | common.MODEL_TEMP = args.model_temperature |
| 57 | # acr related |
| 58 | globals.conv_round_limit = args.conv_round_limit |
| 59 | globals.enable_layered = args.enable_layered |
| 60 | globals.enable_sbfl = args.enable_sbfl |
| 61 | globals.enable_validation = args.enable_validation |
| 62 | globals.enable_angelic = args.enable_angelic |
| 63 | globals.enable_perfect_angelic = args.enable_perfect_angelic |
| 64 | globals.only_save_sbfl_result = args.save_sbfl_result |
| 65 | |
| 66 | try: |
| 67 | if app.debug: |
| 68 | task = RawGithubTask_for_debug( |
| 69 | args.task_id, |
| 70 | args.clone_link, |
| 71 | args.commit_hash, |
| 72 | args.issue_link, |
| 73 | os.path.abspath(args.setup_dir), |
| 74 | ) |
| 75 | else: |
| 76 | task = RawGithubTask( |
| 77 | args.task_id, |
| 78 | args.clone_link, |
| 79 | args.commit_hash, |
| 80 | args.issue_link, |
| 81 | os.path.abspath(args.setup_dir), |
| 82 | ) |
| 83 | except RuntimeError as e: |
| 84 | error_form = {"message": str(e)} |
| 85 | return jsonify(error_form), 400 |
| 86 | |
| 87 | def stream_print(): |
| 88 | print_queue = Queue() |
| 89 | |
| 90 | def callback(data: dict): |
| 91 | print_queue.put(f"{json.dumps(data)}</////json_end>") |
| 92 | |
| 93 | def run(task, callback): |
| 94 | initialize_thread_cost() |
| 95 | callback( |
| 96 | {"category": "issue_info", "problem_statement": task.problem_statement} |
| 97 | ) |
| 98 | run_raw_task(task, callback) |
| 99 | |
| 100 | thread = threading.Thread(target=run, args=(task, callback)) |
| 101 | if app.debug: |
| 102 | callback( |
| 103 | {"category": "issue_info", "problem_statement": task.problem_statement} |
| 104 | ) |
| 105 | thread = threading.Thread( |
| 106 | target=test_generate_data, args=(callback,) |
| 107 | ) # debug |
| 108 | time.sleep(1) |
| 109 | else: |
| 110 | thread = threading.Thread(target=run, args=(task, callback)) |
| 111 | thread.start() |
| 112 | |
| 113 | while thread.is_alive() or not print_queue.empty(): |
| 114 | if not print_queue.empty(): |
| 115 | yield print_queue.get() |
| 116 | else: |
| 117 | time.sleep(0.1) |
| 118 | |
| 119 | return Response(stream_print(), mimetype="text/event-stream") |
| 120 | |
| 121 | |
| 122 | # http://localhost:5000/ |
| 123 | if __name__ == "__main__": |
| 124 | app.run(host="0.0.0.0", port=5000, debug=False) |