Spaces:
Sleeping
Sleeping
dataframe fix
Browse files
app.py
CHANGED
|
@@ -28,59 +28,51 @@ def predict_emotion(text):
|
|
| 28 |
|
| 29 |
if not text:
|
| 30 |
logger.warning("Empty text received")
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
"fear": 0,
|
| 37 |
-
"surprise": 0
|
| 38 |
-
}
|
| 39 |
|
| 40 |
# Get predictions
|
| 41 |
logger.info("Running prediction...")
|
| 42 |
predictions = classifier(text)[0]
|
| 43 |
logger.info(f"Raw predictions: {predictions}")
|
| 44 |
|
| 45 |
-
# Convert
|
| 46 |
df = pd.DataFrame(predictions)
|
|
|
|
| 47 |
df = df.sort_values('score', ascending=True) # Sort for better visualization
|
| 48 |
logger.info(f"Processed scores as DataFrame: {df.to_dict()}")
|
| 49 |
|
| 50 |
-
return
|
| 51 |
-
df['label'].tolist(), # x values (emotions)
|
| 52 |
-
df['score'].tolist() # y values (probabilities)
|
| 53 |
-
)
|
| 54 |
-
|
| 55 |
-
except Exception as e:
|
| 56 |
-
logger.error(f"Error in prediction: {str(e)}")
|
| 57 |
-
return {
|
| 58 |
-
"error": 1.0,
|
| 59 |
-
"message": f"An error occurred: {str(e)}"
|
| 60 |
-
}
|
| 61 |
|
| 62 |
# Create the Gradio interface with error handling
|
| 63 |
try:
|
| 64 |
logger.info("Setting up Gradio interface...")
|
| 65 |
demo = gr.Interface(
|
| 66 |
fn=predict_emotion,
|
| 67 |
-
inputs=gr.Textbox(
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
|
|
|
|
|
|
|
|
|
| 78 |
description="This app uses the DistilBERT model fine-tuned for emotion detection. Enter any text to analyze its emotional content.",
|
| 79 |
examples=[
|
| 80 |
["I am so happy to see you!"],
|
| 81 |
["I'm really angry about what happened."],
|
| 82 |
["The sunset was absolutely beautiful today."],
|
| 83 |
-
["I'm worried about the upcoming exam."]
|
|
|
|
| 84 |
],
|
| 85 |
allow_flagging="never"
|
| 86 |
)
|
|
@@ -96,4 +88,4 @@ if __name__ == "__main__":
|
|
| 96 |
logger.info("Gradio app launched successfully")
|
| 97 |
except Exception as e:
|
| 98 |
logger.error(f"Failed to launch Gradio app: {str(e)}")
|
| 99 |
-
raise
|
|
|
|
| 28 |
|
| 29 |
if not text:
|
| 30 |
logger.warning("Empty text received")
|
| 31 |
+
# Return empty DataFrame with correct structure
|
| 32 |
+
return pd.DataFrame({
|
| 33 |
+
'emotion': ['sadness', 'joy', 'love', 'anger', 'fear', 'surprise'],
|
| 34 |
+
'score': [0, 0, 0, 0, 0, 0]
|
| 35 |
+
})
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
# Get predictions
|
| 38 |
logger.info("Running prediction...")
|
| 39 |
predictions = classifier(text)[0]
|
| 40 |
logger.info(f"Raw predictions: {predictions}")
|
| 41 |
|
| 42 |
+
# Convert to DataFrame and format for visualization
|
| 43 |
df = pd.DataFrame(predictions)
|
| 44 |
+
df.columns = ['emotion', 'score'] # Rename columns
|
| 45 |
df = df.sort_values('score', ascending=True) # Sort for better visualization
|
| 46 |
logger.info(f"Processed scores as DataFrame: {df.to_dict()}")
|
| 47 |
|
| 48 |
+
return df
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
|
| 50 |
# Create the Gradio interface with error handling
|
| 51 |
try:
|
| 52 |
logger.info("Setting up Gradio interface...")
|
| 53 |
demo = gr.Interface(
|
| 54 |
fn=predict_emotion,
|
| 55 |
+
inputs=gr.Textbox(
|
| 56 |
+
placeholder="Enter text to analyze...",
|
| 57 |
+
label="Input Text",
|
| 58 |
+
lines=4
|
| 59 |
+
),
|
| 60 |
+
outputs=gr.BarPlot(
|
| 61 |
+
title="Emotion Probabilities",
|
| 62 |
+
x="emotion",
|
| 63 |
+
y="score",
|
| 64 |
+
xlabel="Emotion",
|
| 65 |
+
ylabel="Probability",
|
| 66 |
+
height=400
|
| 67 |
+
),
|
| 68 |
+
title="CREATIVE MACHINES: Emotion Detection with DistilBERT",
|
| 69 |
description="This app uses the DistilBERT model fine-tuned for emotion detection. Enter any text to analyze its emotional content.",
|
| 70 |
examples=[
|
| 71 |
["I am so happy to see you!"],
|
| 72 |
["I'm really angry about what happened."],
|
| 73 |
["The sunset was absolutely beautiful today."],
|
| 74 |
+
["I'm worried about the upcoming exam."],
|
| 75 |
+
["Fear is the mind-killer. I will face my fear."]
|
| 76 |
],
|
| 77 |
allow_flagging="never"
|
| 78 |
)
|
|
|
|
| 88 |
logger.info("Gradio app launched successfully")
|
| 89 |
except Exception as e:
|
| 90 |
logger.error(f"Failed to launch Gradio app: {str(e)}")
|
| 91 |
+
raise
|