Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
sentiment_analysis = pipeline(model=("HuggingFaceFW/fineweb-edu-classifier"))#"finiteautomata/bertweet-base-sentiment-analysis")
|
| 7 |
+
test_output = sentiment_analysis("Testing the model with a simple sentence.")
|
| 8 |
+
print("Model test output:", test_output)
|
| 9 |
+
except Exception as e:
|
| 10 |
+
print(f"Failed to load or run model: {e}")
|
| 11 |
+
|
| 12 |
+
# Prediction function with error handling
|
| 13 |
+
def predict_sentiment(text):
|
| 14 |
+
try:
|
| 15 |
+
predictions = sentiment_analysis(text)
|
| 16 |
+
return f"Label: {predictions[0]['label']}, Score: {predictions[0]['score']:.4f}"
|
| 17 |
+
except Exception as e:
|
| 18 |
+
return f"Error processing input: {e}"
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
# Define example inputs
|
| 22 |
+
exams = [
|
| 23 |
+
"I absolutely love this product! It has changed my life.",
|
| 24 |
+
"This is the worst movie I have ever seen. Completely disappointing.",
|
| 25 |
+
"I'm not sure how I feel about this new update. It has some good points, but also many drawbacks.",
|
| 26 |
+
"The customer service was fantastic! Very helpful and polite.",
|
| 27 |
+
"Honestly, this was quite a mediocre experience. Nothing special.",
|
| 28 |
+
"Learning new skills in mathematics can significantly improve problem-solving abilities."
|
| 29 |
+
]
|
| 30 |
+
|
| 31 |
+
# Gradio interface setup
|
| 32 |
+
iface = gr.Interface(fn=predict_sentiment,
|
| 33 |
+
title="education_text_recognizer",
|
| 34 |
+
description="Enter text to analyze education relation. Powered by Hugging Face Transformers.",
|
| 35 |
+
inputs="text",
|
| 36 |
+
outputs="text",
|
| 37 |
+
examples=exams)
|
| 38 |
+
|
| 39 |
+
iface.launch()
|