douglasgoodwin commited on
Commit
eeade79
·
verified ·
1 Parent(s): 4de911d

fix the bar chart

Browse files
Files changed (1) hide show
  1. app.py +16 -10
app.py CHANGED
@@ -41,11 +41,15 @@ def predict_emotion(text):
41
  predictions = classifier(text)[0]
42
  logger.info(f"Raw predictions: {predictions}")
43
 
44
- # Convert to dictionary format for the bar chart
45
- scores = {pred['label']: float(pred['score']) for pred in predictions}
 
46
  logger.info(f"Processed scores: {scores}")
47
 
48
- return scores
 
 
 
49
 
50
  except Exception as e:
51
  logger.error(f"Error in prediction: {str(e)}")
@@ -60,13 +64,15 @@ try:
60
  demo = gr.Interface(
61
  fn=predict_emotion,
62
  inputs=gr.Textbox(placeholder="Enter text to analyze...", label="Input Text"),
63
- outputs=gr.BarPlot(
64
- x="emotion",
65
- y="probability",
66
- title="Emotion Probabilities",
67
- tooltip=["emotion", "probability"],
68
- height=400
69
- ),
 
 
70
  title="Emotion Detection with DistilBERT",
71
  description="This app uses the DistilBERT model fine-tuned for emotion detection. Enter any text to analyze its emotional content.",
72
  examples=[
 
41
  predictions = classifier(text)[0]
42
  logger.info(f"Raw predictions: {predictions}")
43
 
44
+ # Convert predictions to the format Gradio's BarPlot expects
45
+ # We need a list of tuples (emotion, score)
46
+ scores = [(pred['label'], float(pred['score'])) for pred in predictions]
47
  logger.info(f"Processed scores: {scores}")
48
 
49
+ return (
50
+ [score[0] for score in scores], # x values (emotions)
51
+ [score[1] for score in scores] # y values (probabilities)
52
+ )
53
 
54
  except Exception as e:
55
  logger.error(f"Error in prediction: {str(e)}")
 
64
  demo = gr.Interface(
65
  fn=predict_emotion,
66
  inputs=gr.Textbox(placeholder="Enter text to analyze...", label="Input Text"),
67
+ outputs=[
68
+ gr.BarPlot(
69
+ title="Emotion Probabilities",
70
+ x_title="Emotion",
71
+ y_title="Probability",
72
+ height=400,
73
+ vertical=False
74
+ )
75
+ ],
76
  title="Emotion Detection with DistilBERT",
77
  description="This app uses the DistilBERT model fine-tuned for emotion detection. Enter any text to analyze its emotional content.",
78
  examples=[