karths commited on
Commit
bcb3aca
·
verified ·
1 Parent(s): dea8c6e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -38
app.py CHANGED
@@ -29,8 +29,6 @@ torch.manual_seed(seed)
29
  if torch.cuda.is_available():
30
  torch.cuda.manual_seed_all(seed)
31
 
32
-
33
-
34
  model_paths = [
35
  'karths/binary_classification_train_port',
36
  'karths/binary_classification_train_perf',
@@ -58,7 +56,6 @@ models = {path: AutoModelForSequenceClassification.from_pretrained(path) for pat
58
  def get_quality_name(model_name):
59
  return quality_mapping.get(model_name.split('/')[-1], "Unknown Quality")
60
 
61
-
62
  def model_prediction(model, text, device):
63
  model.to(device)
64
  model.eval()
@@ -127,7 +124,6 @@ def llama_generate(
127
  torch.cuda.empty_cache()
128
  return output_text.strip()
129
 
130
-
131
  def generate_explanation(issue_text, top_quality):
132
  """Generates an explanation for the *single* top quality above threshold."""
133
  if not top_quality:
@@ -157,7 +153,6 @@ Provide your response directly without preamble. Use a clear separation between
157
  logging.error(f"Error during Llama generation: {e}")
158
  return "<div style='color: red;'>An error occurred while generating the explanation.</div>"
159
 
160
-
161
  @spaces.GPU(duration=60)
162
  def main_interface(text):
163
  if not text.strip():
@@ -188,42 +183,21 @@ def main_interface(text):
188
  output_html = "<div style='color: red;'>No quality tag met the prediction probability threshold (>= 0.95).</div>"
189
  explanation = ""
190
 
191
-
192
  return output_html, "", explanation
193
 
194
  def render_html_output(top_qualities):
195
- #Simplified to show only the top prediction
196
- styles = """
197
- <style>
198
- .quality-container {
199
- font-family: Arial, sans-serif;
200
- text-align: center;
201
- margin-top: 20px;
202
- }
203
- .quality-label, .ranking {
204
- display: inline-block;
205
- padding: 0.5em 1em;
206
- font-size: 18px;
207
- font-weight: bold;
208
- color: white;
209
- background-color: #007bff;
210
- border-radius: 0.5rem;
211
- margin-right: 10px;
212
- box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
213
- }
214
- </style>
215
- """
216
  if not top_qualities: # Handle empty case
217
- return styles + "<div class='quality-container'>No Top Prediction</div>"
218
 
219
- quality, _ = top_qualities[0] #We know there is only one
220
  html_content = f"""
221
  <div class="quality-container">
222
  <span class="ranking">Top Prediction</span>
223
  <span class="quality-label">{quality}</span>
224
  </div>
225
  """
226
- return styles + html_content
227
 
228
  example_texts = [
229
  ["The algorithm does not accurately distinguish between the positive and negative classes during edge cases.\n\nEnvironment: Production\nReproduction: Run the classifier on the test dataset with known edge cases."],
@@ -231,44 +205,66 @@ example_texts = [
231
  ["There is frequent miscommunication between the development and QA teams regarding feature specifications.\n\nEnvironment: Inter-team meetings\nReproduction: Audit recent communication logs and meeting notes between the teams."],
232
  ["The service-oriented architecture does not effectively isolate failures, leading to cascading failures across services.\n\nEnvironment: Microservices architecture\nReproduction: Simulate a service failure and observe the impact on other services."]
233
  ]
234
- # Improved CSS for better layout and appearance
 
235
  css = """
236
  .quality-container {
237
  font-family: Arial, sans-serif;
238
  text-align: center;
239
  margin-top: 20px;
240
  padding: 10px;
241
- border: 1px solid #ddd;
242
  border-radius: 8px;
243
- background-color: #f9f9f9;
 
244
  }
 
245
  .quality-label, .ranking {
246
  display: inline-block;
247
  padding: 0.5em 1em;
248
  font-size: 18px;
249
  font-weight: bold;
250
- color: white;
251
  background-color: #007bff;
252
  border-radius: 0.5rem;
253
  margin-right: 10px;
254
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
255
  }
 
256
  .explanation-box {
257
- border: 1px solid #ccc;
258
  padding: 15px;
259
  margin-top: 15px;
260
  border-radius: 8px;
261
- background-color: #fff;
 
262
  box-shadow: 0 1px 3px rgba(0,0,0,0.1);
263
  line-height: 1.5;
264
  }
 
265
  .explanation-box p {
266
  margin: 8px 0;
267
  }
 
268
  .explanation-box b {
269
  color: #007bff;
270
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
271
  """
 
272
  interface = gr.Interface(
273
  fn=main_interface,
274
  inputs=gr.Textbox(lines=7, label="Issue Description", placeholder="Enter your issue text here"),
@@ -281,6 +277,8 @@ interface = gr.Interface(
281
  description="This tool classifies text into different quality domains such as Security, Usability, Mantainability, Reliability etc., and provides explanations.",
282
  examples=example_texts,
283
  css=css,
284
- cache_examples=False# Apply the CSS
285
  )
286
- interface.launch()
 
 
 
29
  if torch.cuda.is_available():
30
  torch.cuda.manual_seed_all(seed)
31
 
 
 
32
  model_paths = [
33
  'karths/binary_classification_train_port',
34
  'karths/binary_classification_train_perf',
 
56
  def get_quality_name(model_name):
57
  return quality_mapping.get(model_name.split('/')[-1], "Unknown Quality")
58
 
 
59
  def model_prediction(model, text, device):
60
  model.to(device)
61
  model.eval()
 
124
  torch.cuda.empty_cache()
125
  return output_text.strip()
126
 
 
127
  def generate_explanation(issue_text, top_quality):
128
  """Generates an explanation for the *single* top quality above threshold."""
129
  if not top_quality:
 
153
  logging.error(f"Error during Llama generation: {e}")
154
  return "<div style='color: red;'>An error occurred while generating the explanation.</div>"
155
 
 
156
  @spaces.GPU(duration=60)
157
  def main_interface(text):
158
  if not text.strip():
 
183
  output_html = "<div style='color: red;'>No quality tag met the prediction probability threshold (>= 0.95).</div>"
184
  explanation = ""
185
 
 
186
  return output_html, "", explanation
187
 
188
  def render_html_output(top_qualities):
189
+ # Cleaned up: Removed the duplicate inline <style> tags here.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
190
  if not top_qualities: # Handle empty case
191
+ return "<div class='quality-container'>No Top Prediction</div>"
192
 
193
+ quality, _ = top_qualities[0] # We know there is only one
194
  html_content = f"""
195
  <div class="quality-container">
196
  <span class="ranking">Top Prediction</span>
197
  <span class="quality-label">{quality}</span>
198
  </div>
199
  """
200
+ return html_content
201
 
202
  example_texts = [
203
  ["The algorithm does not accurately distinguish between the positive and negative classes during edge cases.\n\nEnvironment: Production\nReproduction: Run the classifier on the test dataset with known edge cases."],
 
205
  ["There is frequent miscommunication between the development and QA teams regarding feature specifications.\n\nEnvironment: Inter-team meetings\nReproduction: Audit recent communication logs and meeting notes between the teams."],
206
  ["The service-oriented architecture does not effectively isolate failures, leading to cascading failures across services.\n\nEnvironment: Microservices architecture\nReproduction: Simulate a service failure and observe the impact on other services."]
207
  ]
208
+
209
+ # Improved CSS for better layout and appearance in BOTH Light and Dark modes
210
  css = """
211
  .quality-container {
212
  font-family: Arial, sans-serif;
213
  text-align: center;
214
  margin-top: 20px;
215
  padding: 10px;
216
+ border: 1px solid var(--border-color-primary, #ddd);
217
  border-radius: 8px;
218
+ background-color: var(--background-fill-secondary, #f9f9f9);
219
+ color: var(--body-text-color, #000);
220
  }
221
+
222
  .quality-label, .ranking {
223
  display: inline-block;
224
  padding: 0.5em 1em;
225
  font-size: 18px;
226
  font-weight: bold;
227
+ color: white; /* Always keep text white inside the blue badge */
228
  background-color: #007bff;
229
  border-radius: 0.5rem;
230
  margin-right: 10px;
231
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
232
  }
233
+
234
  .explanation-box {
235
+ border: 1px solid var(--border-color-primary, #ccc);
236
  padding: 15px;
237
  margin-top: 15px;
238
  border-radius: 8px;
239
+ background-color: var(--background-fill-primary, #fff);
240
+ color: var(--body-text-color, #000);
241
  box-shadow: 0 1px 3px rgba(0,0,0,0.1);
242
  line-height: 1.5;
243
  }
244
+
245
  .explanation-box p {
246
  margin: 8px 0;
247
  }
248
+
249
  .explanation-box b {
250
  color: #007bff;
251
  }
252
+
253
+ /* Fallback explicit styling for Gradio Dark Mode */
254
+ .dark .quality-container {
255
+ background-color: #1f2937;
256
+ border-color: #374151;
257
+ color: #f3f4f6;
258
+ }
259
+
260
+ .dark .explanation-box {
261
+ background-color: #1f2937;
262
+ border-color: #374151;
263
+ color: #f3f4f6;
264
+ box-shadow: 0 1px 3px rgba(255,255,255,0.05);
265
+ }
266
  """
267
+
268
  interface = gr.Interface(
269
  fn=main_interface,
270
  inputs=gr.Textbox(lines=7, label="Issue Description", placeholder="Enter your issue text here"),
 
277
  description="This tool classifies text into different quality domains such as Security, Usability, Mantainability, Reliability etc., and provides explanations.",
278
  examples=example_texts,
279
  css=css,
280
+ cache_examples=False
281
  )
282
+
283
+ if __name__ == "__main__":
284
+ interface.launch()