Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from fastai.text.all import load_learner
|
| 3 |
+
|
| 4 |
+
# Load FastAI model once
|
| 5 |
+
learn = load_learner("emotion_classifier.pkl")
|
| 6 |
+
learn.push_to_hub("fastai-emotion-classifier")
|
| 7 |
+
learn = load_learner("https://huggingface.co/haripriyaram/fastai-emotion-classifier/resolve/main/export.pkl")
|
| 8 |
+
|
| 9 |
+
# Prediction function
|
| 10 |
+
def predict_emotion(text):
|
| 11 |
+
pred_label, _, probs = learn.predict(text)
|
| 12 |
+
probs_dict = {label: float(prob) for label, prob in zip(learn.dls.vocab, probs)}
|
| 13 |
+
return pred_label, probs_dict
|
| 14 |
+
|
| 15 |
+
# Gradio UI
|
| 16 |
+
iface = gr.Interface(
|
| 17 |
+
fn=predict_emotion,
|
| 18 |
+
inputs=gr.Textbox(lines=2, placeholder="Enter a sentence..."),
|
| 19 |
+
outputs=[
|
| 20 |
+
gr.Label(label="Predicted Emotion"),
|
| 21 |
+
gr.JSON(label="Confidence Scores")
|
| 22 |
+
],
|
| 23 |
+
title="🎭 Emotion Classifier (FastAI)",
|
| 24 |
+
description="Enter a sentence and the model will predict the corresponding emotion.",
|
| 25 |
+
allow_flagging="never"
|
| 26 |
+
)
|
| 27 |
+
|
| 28 |
+
if __name__ == "__main__":
|
| 29 |
+
iface.launch()
|