douglasgoodwin commited on
Commit
7c493ae
·
verified ·
1 Parent(s): 94800e4

let's debug

Browse files
Files changed (1) hide show
  1. app.py +82 -40
app.py CHANGED
@@ -1,50 +1,92 @@
1
  # app.py
2
  import gradio as gr
3
  from transformers import pipeline
 
 
4
 
5
- # Initialize the emotion classification pipeline
6
- classifier = pipeline("text-classification",
7
- model="bhadresh-savani/distilbert-base-uncased-emotion",
8
- return_all_scores=True)
 
 
 
 
 
 
 
 
 
 
 
 
9
 
10
  def predict_emotion(text):
11
- if not text:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
  return {
13
- "sadness": 0,
14
- "joy": 0,
15
- "love": 0,
16
- "anger": 0,
17
- "fear": 0,
18
- "surprise": 0
19
  }
20
-
21
- # Get predictions
22
- predictions = classifier(text)[0]
23
-
24
- # Convert to dictionary format for the bar chart
25
- scores = {pred['label']: pred['score'] for pred in predictions}
26
- return scores
27
 
28
- # Create the Gradio interface
29
- demo = gr.Interface(
30
- fn=predict_emotion,
31
- inputs=gr.Textbox(placeholder="Enter text to analyze...", label="Input Text"),
32
- outputs=gr.BarPlot(
33
- x="emotion",
34
- y="probability",
35
- title="Emotion Probabilities",
36
- tooltip=["emotion", "probability"],
37
- height=400
38
- ),
39
- title="Emotion Detection with DistilBERT",
40
- description="This app uses the DistilBERT model fine-tuned for emotion detection. Enter any text to analyze its emotional content.",
41
- examples=[
42
- ["I am so happy to see you!"],
43
- ["I'm really angry about what happened."],
44
- ["The sunset was absolutely beautiful today."],
45
- ["I'm worried about the upcoming exam."]
46
- ],
47
- allow_flagging="never"
48
- )
 
 
 
 
 
 
49
 
50
- demo.launch()
 
 
 
 
 
 
 
 
1
  # app.py
2
  import gradio as gr
3
  from transformers import pipeline
4
+ import logging
5
+ import sys
6
 
7
+ # Set up logging
8
+ logging.basicConfig(level=logging.INFO, stream=sys.stdout,
9
+ format='%(asctime)s - %(levelname)s - %(message)s')
10
+ logger = logging.getLogger(__name__)
11
+
12
+ try:
13
+ logger.info("Initializing emotion classification pipeline...")
14
+ classifier = pipeline(
15
+ "text-classification",
16
+ model="bhadresh-savani/distilbert-base-uncased-emotion",
17
+ return_all_scores=True
18
+ )
19
+ logger.info("Pipeline initialized successfully")
20
+ except Exception as e:
21
+ logger.error(f"Failed to initialize pipeline: {str(e)}")
22
+ raise
23
 
24
  def predict_emotion(text):
25
+ try:
26
+ logger.info(f"Received input text: {text}")
27
+
28
+ if not text:
29
+ logger.warning("Empty text received")
30
+ return {
31
+ "sadness": 0,
32
+ "joy": 0,
33
+ "love": 0,
34
+ "anger": 0,
35
+ "fear": 0,
36
+ "surprise": 0
37
+ }
38
+
39
+ # Get predictions
40
+ logger.info("Running prediction...")
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)}")
52
  return {
53
+ "error": 1.0,
54
+ "message": f"An error occurred: {str(e)}"
 
 
 
 
55
  }
 
 
 
 
 
 
 
56
 
57
+ # Create the Gradio interface with error handling
58
+ try:
59
+ logger.info("Setting up Gradio interface...")
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=[
73
+ ["I am so happy to see you!"],
74
+ ["I'm really angry about what happened."],
75
+ ["The sunset was absolutely beautiful today."],
76
+ ["I'm worried about the upcoming exam."]
77
+ ],
78
+ allow_flagging="never"
79
+ )
80
+ logger.info("Gradio interface setup complete")
81
+ except Exception as e:
82
+ logger.error(f"Failed to create Gradio interface: {str(e)}")
83
+ raise
84
 
85
+ if __name__ == "__main__":
86
+ try:
87
+ logger.info("Launching Gradio app...")
88
+ demo.launch(debug=True)
89
+ logger.info("Gradio app launched successfully")
90
+ except Exception as e:
91
+ logger.error(f"Failed to launch Gradio app: {str(e)}")
92
+ raise