File size: 5,433 Bytes
026b316
 
 
 
 
 
 
 
30b9ad3
026b316
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30b9ad3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
026b316
 
 
 
 
 
30b9ad3
026b316
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30b9ad3
026b316
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ec45c15
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import gradio as gr
import json
import subprocess

def run_pipeline(pdf_file, system_prompt, max_step, learning_rate, epochs, model_name):
    # Construct job input
    data = {
        "input": {
            "pdf_file": pdf_file.name,
            "system_prompt": system_prompt,
            "max_step": max_step,
            "learning_rate": learning_rate,
            "epochs": epochs,
            "model_name": model_name
        }
    }

    try:
        # Call handler.py using the constructed input
        input_json = json.dumps(data)
        process = subprocess.Popen(
            ['python3', 'handler.py', '--test_input', input_json],
            stdout=subprocess.PIPE,
            stderr=subprocess.PIPE,
            text=True
        )
        stdout, stderr = process.communicate()

        # Extract JSON object from the output
        output_lines = stdout.splitlines()
        handler_output = None
        for line in output_lines:
            try:
                parsed_line = json.loads(line)
                if isinstance(parsed_line, dict) and "status" in parsed_line:
                    handler_output = parsed_line
                    break
            except json.JSONDecodeError:
                continue

        if handler_output is None:
            return {"status": "error", "details": f"No valid JSON found in output: {stdout}"}

        # Check the status in the parsed JSON
        if handler_output.get("status") == "success":
            # Extract and format the result
            model_name = handler_output.get("model_name", "N/A")
            processing_time = handler_output.get("processing_time", "N/A")
            evaluation_results = handler_output.get("evaluation_results", {})

            return {
                "model_name": model_name,
                "processing_time": processing_time,
                "evaluation_results": evaluation_results
            }
        else:
            # Return error details from the handler output
            return handler_output

    except FileNotFoundError:
        return {"status": "error", "details": "Handler script not found"}
    except Exception as e:
        return {"status": "error", "details": str(e)}


# Define Gradio interface
with gr.Blocks(css='''
    .gradio-container {
        background-color: #121212;  /* Dark background */
        color: #f1f1f1;             /* Light text color */
        padding: 20px;
        font-family: 'Arial', sans-serif;
    }

    .gr-row {
        margin-bottom: 20px;
    }

    /* Styling for Textboxes and Numbers */
    input[type="text"], input[type="number"], textarea {
        background-color: #f0f0f0;  /* Light grey background for inputs */
        border: 1px solid #ccc;     /* Light grey border */
        color: #000;                /* Black text inside the inputs */
        border-radius: 8px;
        padding: 10px;
        font-size: 16px;
        width: 100%;
        box-sizing: border-box;
    }

    /* Styling specific to textarea placeholder */
    textarea::placeholder {
        color: #999;  /* Slightly darker grey placeholder text */
    }

    /* Button styling */
    button {
        background-color: #4CAF50; /* Green button */
        color: white;
        border: none;
        padding: 12px 20px;
        cursor: pointer;
        font-weight: bold;
        font-size: 16px;
        transition: background-color 0.3s ease;
        border-radius: 8px;
    }

    button:hover {
        background-color: #3e8e41;  /* Darker green hover effect */
    }

    /* Styling for JSON output */
    .gr-json {
        background-color: #333;     /* Dark background for JSON output */
        border: 1px solid #444;     /* Slightly lighter border */
        padding: 12px;
        font-size: 14px;
        max-height: 300px;
        overflow-y: auto;
        margin-top: 10px;
        color: #f1f1f1;              /* Light text color */
    }

    /* Adjust margins for all inputs */
    .gr-row .gr-textbox, .gr-row .gr-number {
        margin-bottom: 15px;
    }
''') as demo:

    # Add Heading at the top
    gr.Markdown(
        '<h2 style="color: #87CEEB; text-align: center;">🤖 Fine-tuning Pipeline Configurator</h2>'
    )

    # Layout structure with improved spacing
    with gr.Row():
        with gr.Column(scale=2):
            pdf_file = gr.File(label="Upload PDF File", file_types=[".pdf"])  # Updated to accept file uploads
        with gr.Column(scale=3):
            system_prompt = gr.Textbox(label="System Prompt", placeholder="Enter system instructions or context", value="You are a helpful assistant that provides detailed information based on the provided text.")
        with gr.Column(scale=2):
            max_step = gr.Number(label="Max Steps", value=150)
        with gr.Column(scale=2):
            learning_rate = gr.Number(label="Learning Rate", value=2e-4)
        with gr.Column(scale=2):
            epochs = gr.Number(label="Epochs", value=10)
        with gr.Column(scale=3):
            model_name = gr.Textbox(label="Model Name", placeholder="Enter the model name")

    result_output = gr.JSON(label="Pipeline Results")
    run_button = gr.Button("Run Pipeline")

    # Trigger the function when the button is clicked
    run_button.click(
        run_pipeline,
        inputs=[pdf_file, system_prompt, max_step, learning_rate, epochs, model_name],
        outputs=[result_output]
    )

# Run Gradio app
demo.launch()