Diksha2001 commited on
Commit
79a8be6
·
verified ·
1 Parent(s): d289937

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +149 -117
app.py CHANGED
@@ -1,35 +1,66 @@
1
  import gradio as gr
2
  import json
3
  import subprocess
 
 
 
 
 
 
4
 
5
- def run_pipeline(pdf_file, system_prompt, max_step, learning_rate, epochs, model_name):
6
- # Construct job input
7
- data = {
8
- "input": {
9
- "pdf_file": pdf_file.name,
10
- "system_prompt": system_prompt,
11
- "max_step": max_step,
12
- "learning_rate": learning_rate,
13
- "epochs": epochs,
14
- "model_name": model_name
15
- }
16
- }
17
 
 
 
18
  try:
19
- # Call handler.py using the constructed input
20
- input_json = json.dumps(data)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
  process = subprocess.Popen(
22
- ['python3', 'handler.py', '--test_input', input_json],
23
  stdout=subprocess.PIPE,
24
  stderr=subprocess.PIPE,
25
  text=True
26
  )
27
- stdout, stderr = process.communicate()
28
-
29
- # Extract JSON object from the output
30
- output_lines = stdout.splitlines()
 
 
 
 
 
 
 
 
 
 
 
 
 
31
  handler_output = None
32
- for line in output_lines:
33
  try:
34
  parsed_line = json.loads(line)
35
  if isinstance(parsed_line, dict) and "status" in parsed_line:
@@ -37,119 +68,120 @@ def run_pipeline(pdf_file, system_prompt, max_step, learning_rate, epochs, model
37
  break
38
  except json.JSONDecodeError:
39
  continue
40
-
41
- if handler_output is None:
42
- return {"status": "error", "details": f"No valid JSON found in output: {stdout}"}
43
-
44
- # Check the status in the parsed JSON
45
- if handler_output.get("status") == "success":
46
- # Extract and format the result
47
- model_name = handler_output.get("model_name", "N/A")
48
- processing_time = handler_output.get("processing_time", "N/A")
49
- evaluation_results = handler_output.get("evaluation_results", {})
50
-
51
  return {
52
- "model_name": model_name,
53
- "processing_time": processing_time,
54
- "evaluation_results": evaluation_results
55
  }
56
  else:
57
- # Return error details from the handler output
58
- return handler_output
59
-
60
- except FileNotFoundError:
61
- return {"status": "error", "details": "Handler script not found"}
62
  except Exception as e:
63
- return {"status": "error", "details": str(e)}
64
-
 
 
 
65
 
66
- # Define Gradio interface
67
  with gr.Blocks(css='''
68
  .gradio-container {
69
- background-color: #121212; /* Dark background */
70
- color: #f1f1f1; /* Light text color */
71
- padding: 20px;
72
- font-family: 'Arial', sans-serif;
73
  }
74
-
75
- .gr-row {
76
- margin-bottom: 20px;
 
 
 
77
  }
78
-
79
- /* Styling for Textboxes and Numbers */
80
- input[type="text"], input[type="number"], textarea {
81
- background-color: #f0f0f0; /* Light grey background for inputs */
82
- border: 1px solid #ccc; /* Light grey border */
83
- color: #000; /* Black text inside the inputs */
84
- border-radius: 8px;
85
- padding: 10px;
86
- font-size: 16px;
87
- width: 100%;
88
- box-sizing: border-box;
89
  }
90
-
91
- /* Styling specific to textarea placeholder */
92
- textarea::placeholder {
93
- color: #999; /* Slightly darker grey placeholder text */
94
- }
95
-
96
- /* Button styling */
97
- button {
98
- background-color: #4CAF50; /* Green button */
99
- color: white;
100
- border: none;
101
- padding: 12px 20px;
102
- cursor: pointer;
103
- font-weight: bold;
104
- font-size: 16px;
105
- transition: background-color 0.3s ease;
106
- border-radius: 8px;
107
  }
108
-
109
- button:hover {
110
- background-color: #3e8e41; /* Darker green hover effect */
111
  }
112
-
113
- /* Styling for JSON output */
114
- .gr-json {
115
- background-color: #333; /* Dark background for JSON output */
116
- border: 1px solid #444; /* Slightly lighter border */
117
- padding: 12px;
118
- font-size: 14px;
119
- max-height: 300px;
120
- overflow-y: auto;
121
- margin-top: 10px;
122
- color: #f1f1f1; /* Light text color */
123
  }
124
-
125
- /* Adjust margins for all inputs */
126
- .gr-row .gr-textbox, .gr-row .gr-number {
127
- margin-bottom: 15px;
128
  }
129
  ''') as demo:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
130
 
131
- # Add Heading at the top
132
- gr.Markdown(
133
- '<h2 style="color: #87CEEB; text-align: center;">🤖 Fine-tuning Pipeline Configurator</h2>'
134
- )
135
-
136
- # Layout structure with improved spacing
137
- with gr.Row():
138
- with gr.Column(scale=2):
139
- pdf_file = gr.File(label="Upload PDF File", file_types=[".pdf"]) # Updated to accept file uploads
140
- with gr.Column(scale=3):
141
- 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.")
142
- with gr.Column(scale=2):
143
- max_step = gr.Number(label="Max Steps", value=150)
144
- with gr.Column(scale=2):
145
- learning_rate = gr.Number(label="Learning Rate", value=2e-4)
146
- with gr.Column(scale=2):
147
- epochs = gr.Number(label="Epochs", value=10)
148
- with gr.Column(scale=3):
149
- model_name = gr.Textbox(label="Model Name", placeholder="Enter the model name")
150
-
 
 
 
 
 
 
 
 
 
 
 
151
  result_output = gr.JSON(label="Pipeline Results")
152
- run_button = gr.Button("Run Pipeline")
153
 
154
  # Trigger the function when the button is clicked
155
  run_button.click(
@@ -159,4 +191,4 @@ with gr.Blocks(css='''
159
  )
160
 
161
  # Run Gradio app
162
- demo.launch()
 
1
  import gradio as gr
2
  import json
3
  import subprocess
4
+ import logging
5
+ import sys
6
+ import io
7
+ import threading
8
+ import queue
9
+ from contextlib import redirect_stdout, redirect_stderr
10
 
11
+ class LiveLogHandler(logging.Handler):
12
+ def __init__(self, log_queue):
13
+ super().__init__()
14
+ self.log_queue = log_queue
15
+
16
+ def emit(self, record):
17
+ msg = self.format(record)
18
+ self.log_queue.put(msg)
 
 
 
 
19
 
20
+
21
+ def run_pipeline(pdf_file, system_prompt, max_step, learning_rate, epochs, model_name):
22
  try:
23
+ # Construct job input
24
+ data = {
25
+ "input": {
26
+ "pdf_file": pdf_file.name,
27
+ "system_prompt": system_prompt,
28
+ "max_step": max_step,
29
+ "learning_rate": learning_rate,
30
+ "epochs": epochs,
31
+ "model_name": model_name
32
+ }
33
+ }
34
+
35
+ # Print the start of pipeline to terminal
36
+ print("Pipeline started with inputs:", json.dumps(data, indent=2))
37
+
38
+ # Run the handler in a separate process
39
  process = subprocess.Popen(
40
+ ['python3', 'handler.py', '--test_input', json.dumps(data)],
41
  stdout=subprocess.PIPE,
42
  stderr=subprocess.PIPE,
43
  text=True
44
  )
45
+
46
+ # Read output in real-time and print to terminal
47
+ stdout_lines = []
48
+ stderr_lines = []
49
+
50
+ for line in process.stdout:
51
+ print(line.strip()) # Print stdout to terminal
52
+ stdout_lines.append(line.strip())
53
+
54
+ for line in process.stderr:
55
+ print(f"ERROR: {line.strip()}") # Print stderr to terminal
56
+ stderr_lines.append(line.strip())
57
+
58
+ # Wait for process to complete
59
+ process.wait()
60
+
61
+ # Try to extract JSON result
62
  handler_output = None
63
+ for line in stdout_lines:
64
  try:
65
  parsed_line = json.loads(line)
66
  if isinstance(parsed_line, dict) and "status" in parsed_line:
 
68
  break
69
  except json.JSONDecodeError:
70
  continue
71
+
72
+ # Prepare result
73
+ if handler_output and handler_output.get("status") == "success":
 
 
 
 
 
 
 
 
74
  return {
75
+ "model_name": handler_output.get("model_name", "N/A"),
76
+ "processing_time": handler_output.get("processing_time", "N/A"),
77
+ "evaluation_results": handler_output.get("evaluation_results", {})
78
  }
79
  else:
80
+ return {
81
+ "status": "error",
82
+ "details": handler_output or "No valid output received"
83
+ }
84
+
85
  except Exception as e:
86
+ print(f"Pipeline execution error: {str(e)}")
87
+ return {
88
+ "status": "error",
89
+ "details": str(e)
90
+ }
91
 
92
+ # Define Gradio interface with dark theme and light blue buttons
93
  with gr.Blocks(css='''
94
  .gradio-container {
95
+ background-color: #121212;
96
+ color: #e0e0e0;
97
+ max-width: 800px;
98
+ margin: 0 auto;
99
  }
100
+ .custom-input {
101
+ min-height: 50px;
102
+ height: auto;
103
+ background-color: #1e1e1e !important;
104
+ color: #e0e0e0 !important;
105
+ border: 1px solid #333 !important;
106
  }
107
+ .custom-input textarea,
108
+ .custom-input input,
109
+ .custom-input .upload-icon {
110
+ background-color: #1e1e1e !important;
111
+ color: #e0e0e0 !important;
 
 
 
 
 
 
112
  }
113
+ .gr-button {
114
+ background-color: #87CEFA !important; /* Light Sky Blue */
115
+ color: #121212 !important;
116
+ border: none !important;
117
+ font-weight: bold !important;
 
 
 
 
 
 
 
 
 
 
 
 
118
  }
119
+ .gr-button:hover {
120
+ background-color: #87CEEB !important; /* Slightly different light blue on hover */
 
121
  }
122
+ .gr-form {
123
+ background-color: #1e1e1e !important;
124
+ border-color: #333 !important;
 
 
 
 
 
 
 
 
125
  }
126
+ .gr-label {
127
+ color: #87CEFA !important; /* Light Sky Blue for labels */
 
 
128
  }
129
  ''') as demo:
130
+ # Add Heading at the top, centered and light blue
131
+ title = "🤖 Fine-tuning Pipeline Configurator"
132
+ header_style = "color: #87CEFA;" # Light Sky Blue
133
+
134
+ html_content = f"""
135
+ <div style="text-align: center;">
136
+ <h2><span style="{header_style}">{title}</span></h2>
137
+ </div>
138
+ """
139
+
140
+ gr.Markdown(html_content)
141
+
142
+ with gr.Column(scale=1):
143
+ # System Prompt with consistent styling
144
+ system_prompt = gr.Textbox(
145
+ label="System Prompt",
146
+ placeholder="Enter system instructions",
147
+ lines=4,
148
+ elem_classes=['custom-input'],
149
+ value="You are a helpful assistant that provides detailed information based on the provided text."
150
+ )
151
 
152
+ # Numeric and model name inputs in a row with consistent styling
153
+ with gr.Row():
154
+ # PDF File Upload
155
+ pdf_file = gr.File(
156
+ label="Upload PDF",
157
+ file_types=[".pdf"],
158
+ elem_classes=['custom-input']
159
+ )
160
+
161
+ max_step = gr.Number(
162
+ label="Max Steps",
163
+ value=150,
164
+ elem_classes=['custom-input']
165
+ )
166
+ learning_rate = gr.Number(
167
+ label="Learning Rate",
168
+ value=2e-4,
169
+ elem_classes=['custom-input']
170
+ )
171
+ epochs = gr.Number(
172
+ label="Epochs",
173
+ value=10,
174
+ elem_classes=['custom-input']
175
+ )
176
+ model_name = gr.Textbox(
177
+ label="Model Name",
178
+ placeholder="Enter model name",
179
+ elem_classes=['custom-input']
180
+ )
181
+
182
+ # Results and Run Button
183
  result_output = gr.JSON(label="Pipeline Results")
184
+ run_button = gr.Button("Run Pipeline", variant="primary")
185
 
186
  # Trigger the function when the button is clicked
187
  run_button.click(
 
191
  )
192
 
193
  # Run Gradio app
194
+ demo.launch(share=True)