Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,24 +7,14 @@ from huggingface_hub import from_pretrained_fastai
|
|
| 7 |
repo_id = "jojimene/entregable3"
|
| 8 |
learn = from_pretrained_fastai(repo_id)
|
| 9 |
|
| 10 |
-
# Define the class mapping
|
| 11 |
-
class_mapping = {
|
| 12 |
-
'negative': '0 risk',
|
| 13 |
-
'neutral': '1 neutral',
|
| 14 |
-
'positive': '2 opportunity'
|
| 15 |
-
}
|
| 16 |
-
|
| 17 |
# Define the prediction function
|
| 18 |
def predict_sentiment(text):
|
| 19 |
# Make prediction using the loaded model
|
| 20 |
pred, _, probs = learn.predict(text)
|
| 21 |
-
# Map the predicted label to the desired format
|
| 22 |
-
mapped_pred = class_mapping.get(pred, pred) # Use original if not in mapping
|
| 23 |
# Get probabilities for each class, ensuring string keys
|
| 24 |
labels = [str(label) for label in learn.dls.vocab[1]] # Convert labels to strings
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
return {"predicted_sentiment": mapped_pred, "probabilities": result}
|
| 28 |
|
| 29 |
# Create Gradio interface
|
| 30 |
iface = gr.Interface(
|
|
@@ -32,7 +22,7 @@ iface = gr.Interface(
|
|
| 32 |
inputs=gr.Textbox(lines=5, placeholder="Enter text for sentiment analysis..."),
|
| 33 |
outputs=gr.JSON(),
|
| 34 |
title="Climate Sentiment Classifier",
|
| 35 |
-
description="Enter a text related to climate sentiment, and the model will predict whether it's
|
| 36 |
examples=[
|
| 37 |
"Renewable energy is the future of our planet!",
|
| 38 |
"Climate change is a serious threat to humanity.",
|
|
|
|
| 7 |
repo_id = "jojimene/entregable3"
|
| 8 |
learn = from_pretrained_fastai(repo_id)
|
| 9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
# Define the prediction function
|
| 11 |
def predict_sentiment(text):
|
| 12 |
# Make prediction using the loaded model
|
| 13 |
pred, _, probs = learn.predict(text)
|
|
|
|
|
|
|
| 14 |
# Get probabilities for each class, ensuring string keys
|
| 15 |
labels = [str(label) for label in learn.dls.vocab[1]] # Convert labels to strings
|
| 16 |
+
result = {label: float(prob) for label, prob in zip(labels, probs)}
|
| 17 |
+
return {"predicted_sentiment": pred, "probabilities": result}
|
|
|
|
| 18 |
|
| 19 |
# Create Gradio interface
|
| 20 |
iface = gr.Interface(
|
|
|
|
| 22 |
inputs=gr.Textbox(lines=5, placeholder="Enter text for sentiment analysis..."),
|
| 23 |
outputs=gr.JSON(),
|
| 24 |
title="Climate Sentiment Classifier",
|
| 25 |
+
description="Enter a text related to climate sentiment, and the model will predict whether it's positive, negative, or neutral.",
|
| 26 |
examples=[
|
| 27 |
"Renewable energy is the future of our planet!",
|
| 28 |
"Climate change is a serious threat to humanity.",
|