Team Ai
Apppublic

vineyard03/Code-Repair-With-LLMs

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
FrontPage.py100 linesDownload Raw Back to ui
1import gradio2import os3import sys4 5sys.path.insert(0, os.path.abspath("./"))6 7from source.api_calls.initiate_pipeline_call import initiate_pipeline_call8from utils.process_steps import process_steps9 10pipeline_steps = 2111 12class FrontPage:13    def __init__(self):14        with gradio.Blocks(css=self.custom_css()) as self.page:15            gradio.Markdown("# Code Repair with LLMs")16            gradio.Markdown("Upload a Python, Java, C, or C++ file for processing")17            18            with gradio.Row():19                file_input = gradio.File(label="Upload  Code File", file_types=[".py", ".java", ".c", ".cpp"])20                language_input = gradio.Dropdown(choices=["Python", "Java", "C", "C++"], label="Language")21            22            file_content = gradio.Code(label="File Contents", language="python", interactive=False, elem_classes=["fixed-height"])23            process_button = gradio.Button("Process")24 25            with gradio.Row():26                stage1 = gradio.Textbox(label="Fault Localization", value="Pending", interactive=False, elem_classes=["stage-box"])27                stage2 = gradio.Textbox(label="Pattern Matching", value="Pending", interactive=False, elem_classes=["stage-box"])28                stage3 = gradio.Textbox(label="Patch Generation", value="Pending", interactive=False, elem_classes=["stage-box"])29                stage4 = gradio.Textbox(label="Patch Validation", value="Pending", interactive=False, elem_classes=["stage-box"])30 31            output = gradio.Code(label="Processed Output", language="python", elem_classes=["fixed-height"])32 33            pipeline_steps_input = gradio.Number(value=pipeline_steps, label="Pipeline Steps")34 35            file_input.change(fn=self.display_file_content, inputs=[file_input], outputs=[file_content])36            process_button.click(fn=self.initiate_pipeline, 37                                 inputs=[file_input, language_input, pipeline_steps_input], outputs=[])38 39    def custom_css(self):40        return """41        .fixed-height {42            height: 300px !important;43            overflow-y: auto !important;44        }45        .stage-box {46            text-align: center !important;47            font-weight: bold !important;48        }49        """50 51    def display_file_content(self, file):52        if file is None:53            return "No file uploaded yet."54        try:55            if isinstance(file, str):56                with open(file, 'r') as f:57                    return f.read()58            elif hasattr(file, 'name'):59                return file.name60            elif hasattr(file, 'read'):61                return file.read().decode('utf-8')62            else:63                return str(file)64        except Exception as e:65            return f"An error occurred while reading the file: {str(e)}"66 67 68    def initiate_pipeline(self, file_input, language_input, pipeline_steps):69        if file_input is None or (isinstance(file_input, list) and len(file_input) == 0):70            return "No file uploaded."71 72        self.process_file(file_input, language_input)73        74        # If file_input is a list, take the first file75        if isinstance(file_input, list):76            file_input = file_input[0]77            78        print(f"Processing file: {file_input}")79        return initiate_pipeline_call(file_input, pipeline_steps)80    81    def process_file(self, file, language):82        if file is None:83            return "Skipped", "Skipped", "Skipped", "Skipped", "Please upload a file."84        85        try:86            content = self.display_file_content(file)87            stage1_result = "Complete"88            stage2_result = "Complete"89            stage3_result = "Complete"90            stage4_result = "Complete"91            processed_content = self.process_content(content, language)92            return stage1_result, stage2_result, stage3_result, stage4_result, processed_content93        except Exception as e:94            return "Error", "Error", "Error", "Error", f"An error occurred: {str(e)}"95 96    def process_content(self, content, language):97        # This is where you would implement your actual processing logic98        # For now, we'll just return the file content with a message99        return f"Processing {language} code:\n\n{content}\n\nProcessing complete."100