Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,9 @@
|
|
| 1 |
from transformers import pipeline
|
| 2 |
import gradio as gr
|
| 3 |
|
|
|
|
|
|
|
|
|
|
| 4 |
# Attempt to load the model and run a test prediction
|
| 5 |
try:
|
| 6 |
# Explicitly specify the model
|
|
@@ -14,6 +17,8 @@ except Exception as e:
|
|
| 14 |
# Prediction function with error handling
|
| 15 |
def predict_sentiment(text):
|
| 16 |
try:
|
|
|
|
|
|
|
| 17 |
predictions = sentiment_analysis(text)
|
| 18 |
return f"Label: {predictions[0]['label']}, Score: {predictions[0]['score']:.4f}"
|
| 19 |
except Exception as e:
|
|
@@ -36,4 +41,5 @@ iface = gr.Interface(fn=predict_sentiment,
|
|
| 36 |
outputs="text",
|
| 37 |
examples=examples)
|
| 38 |
|
| 39 |
-
|
|
|
|
|
|
| 1 |
from transformers import pipeline
|
| 2 |
import gradio as gr
|
| 3 |
|
| 4 |
+
# Initialize sentiment_analysis as None
|
| 5 |
+
sentiment_analysis = None
|
| 6 |
+
|
| 7 |
# Attempt to load the model and run a test prediction
|
| 8 |
try:
|
| 9 |
# Explicitly specify the model
|
|
|
|
| 17 |
# Prediction function with error handling
|
| 18 |
def predict_sentiment(text):
|
| 19 |
try:
|
| 20 |
+
if sentiment_analysis is None:
|
| 21 |
+
raise ValueError("Model not loaded.")
|
| 22 |
predictions = sentiment_analysis(text)
|
| 23 |
return f"Label: {predictions[0]['label']}, Score: {predictions[0]['score']:.4f}"
|
| 24 |
except Exception as e:
|
|
|
|
| 41 |
outputs="text",
|
| 42 |
examples=examples)
|
| 43 |
|
| 44 |
+
if __name__ == "__main__":
|
| 45 |
+
iface.launch()
|