yassirusa commited on
Commit
8f44ab0
·
verified ·
1 Parent(s): d58ea73

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -5
app.py CHANGED
@@ -25,16 +25,24 @@ def predict(text):
25
  # Map probabilities to class labels
26
  class_probabilities = {model.config.id2label[i]: prob for i, prob in enumerate(probabilities)}
27
 
28
- # Format the output as a sorted list of class probabilities
29
  sorted_probabilities = sorted(class_probabilities.items(), key=lambda item: item[1], reverse=True)
30
 
31
- # Create a formatted string to display
32
- result = "\n".join([f"{label}: {prob:.4f}" for label, prob in sorted_probabilities])
 
 
 
33
 
34
- return result
35
 
36
  # Create a Gradio interface
37
- iface = gr.Interface(fn=predict, inputs="text", outputs="text", title="Text Classification Demo with Probabilities")
 
 
 
 
 
38
 
39
  # Launch the interface
40
  iface.launch()
 
25
  # Map probabilities to class labels
26
  class_probabilities = {model.config.id2label[i]: prob for i, prob in enumerate(probabilities)}
27
 
28
+ # Sort probabilities in descending order
29
  sorted_probabilities = sorted(class_probabilities.items(), key=lambda item: item[1], reverse=True)
30
 
31
+ # Prepare HTML output for display
32
+ output_html = "<div style='font-family: Arial, sans-serif;'>"
33
+ for label, prob in sorted_probabilities:
34
+ output_html += f"<div style='margin-bottom: 5px;'>{label}: <div style='display: inline-block; width: {prob * 100:.1f}%; background-color: #9b59b6; height: 12px;'></div> {prob:.4f}</div>"
35
+ output_html += "</div>"
36
 
37
+ return output_html
38
 
39
  # Create a Gradio interface
40
+ iface = gr.Interface(
41
+ fn=predict,
42
+ inputs="text",
43
+ outputs=gr.outputs.HTML(label="Classification Results"),
44
+ title="Text Classification Demo with Probabilities"
45
+ )
46
 
47
  # Launch the interface
48
  iface.launch()