Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,35 +5,18 @@ from huggingface_hub import from_pretrained_keras
|
|
| 5 |
import numpy as np
|
| 6 |
from keras.preprocessing.sequence import pad_sequences
|
| 7 |
from keras.datasets import imdb
|
| 8 |
-
import logging
|
| 9 |
|
| 10 |
-
# ืืืืจืช ืืืืจ ืืกืืกื
|
| 11 |
-
logging.basicConfig(level=logging.INFO)
|
| 12 |
-
logger = logging.getLogger(__name__)
|
| 13 |
|
| 14 |
-
# ืืืืจืช ืืืืื ืืืฉืชื ื ืืืืืื
|
| 15 |
-
global model
|
| 16 |
model = None
|
| 17 |
-
|
| 18 |
-
# ืคืื ืงืฆืื ืืืขืื ืช ืืืืื
|
| 19 |
-
def load_model():
|
| 20 |
-
global model
|
| 21 |
-
try:
|
| 22 |
-
logger.info("ืืชืืืช ืืขืื ืช ืืืืื...")
|
| 23 |
-
model = from_pretrained_keras("GiladtheFixer/Sentiment_Analysis")
|
| 24 |
-
logger.info("ืืืืื ื ืืขื ืืืฆืืื!")
|
| 25 |
-
return True
|
| 26 |
-
except Exception as e:
|
| 27 |
-
logger.error(f"ืฉืืืื ืืืขืื ืช ืืืืื: {e}")
|
| 28 |
-
return False
|
| 29 |
-
|
| 30 |
-
# ืืขืื ืช ืืื ืืงืก ืืืืืื
|
| 31 |
try:
|
| 32 |
-
|
| 33 |
-
|
| 34 |
except Exception as e:
|
| 35 |
-
|
| 36 |
-
|
|
|
|
|
|
|
| 37 |
|
| 38 |
def preprocess_text(text):
|
| 39 |
# ืืืจื ืืืืืื
|
|
@@ -51,13 +34,6 @@ def preprocess_text(text):
|
|
| 51 |
return vector
|
| 52 |
|
| 53 |
def predict_sentiment(text):
|
| 54 |
-
global model
|
| 55 |
-
|
| 56 |
-
# ืืืืงื ืื ืืืืื ืืขืื
|
| 57 |
-
if model is None:
|
| 58 |
-
if not load_model():
|
| 59 |
-
return {"Error": "Failed to load model"}
|
| 60 |
-
|
| 61 |
try:
|
| 62 |
# ืขืืืื ืืืงืกื
|
| 63 |
processed_text = preprocess_text(text)
|
|
@@ -73,8 +49,9 @@ def predict_sentiment(text):
|
|
| 73 |
"Confidence": f"{confidence:.2%}"
|
| 74 |
}
|
| 75 |
except Exception as e:
|
| 76 |
-
|
| 77 |
-
|
|
|
|
| 78 |
|
| 79 |
# ืืฆืืจืช ืืืฉืง Gradio
|
| 80 |
iface = gr.Interface(
|
|
@@ -93,9 +70,6 @@ iface = gr.Interface(
|
|
| 93 |
theme=gr.themes.Soft()
|
| 94 |
)
|
| 95 |
|
| 96 |
-
#
|
| 97 |
if __name__ == "__main__":
|
| 98 |
-
|
| 99 |
-
iface.launch(share=True)
|
| 100 |
-
else:
|
| 101 |
-
logger.error("ืื ื ืืชื ืืืคืขืื ืืช ืืืืฉืง - ืืืืื ืื ื ืืขื ืืืฆืืื")
|
|
|
|
| 5 |
import numpy as np
|
| 6 |
from keras.preprocessing.sequence import pad_sequences
|
| 7 |
from keras.datasets import imdb
|
|
|
|
| 8 |
|
|
|
|
|
|
|
|
|
|
| 9 |
|
|
|
|
|
|
|
| 10 |
model = None
|
| 11 |
+
# ืืขืื ืช ืืืืื ื-Hugging Face Hub
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
try:
|
| 13 |
+
model = from_pretrained_keras("GiladtheFixer/Sentiment_Analysis")
|
| 14 |
+
print("Model loaded successfully!")
|
| 15 |
except Exception as e:
|
| 16 |
+
print(f"Error loading model: {e}")
|
| 17 |
+
|
| 18 |
+
# ืงืืืช ืืื ืืงืก ืืืืืื ืฉื IMDB
|
| 19 |
+
word_index = imdb.get_word_index()
|
| 20 |
|
| 21 |
def preprocess_text(text):
|
| 22 |
# ืืืจื ืืืืืื
|
|
|
|
| 34 |
return vector
|
| 35 |
|
| 36 |
def predict_sentiment(text):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
try:
|
| 38 |
# ืขืืืื ืืืงืกื
|
| 39 |
processed_text = preprocess_text(text)
|
|
|
|
| 49 |
"Confidence": f"{confidence:.2%}"
|
| 50 |
}
|
| 51 |
except Exception as e:
|
| 52 |
+
return {
|
| 53 |
+
"Error": str(e)
|
| 54 |
+
}
|
| 55 |
|
| 56 |
# ืืฆืืจืช ืืืฉืง Gradio
|
| 57 |
iface = gr.Interface(
|
|
|
|
| 70 |
theme=gr.themes.Soft()
|
| 71 |
)
|
| 72 |
|
| 73 |
+
# ืืคืขืืช ืืืืฉืง
|
| 74 |
if __name__ == "__main__":
|
| 75 |
+
iface.launch(share=True) # ืฉื ื ื-share=False ืื ืืชื ืื ืจืืฆื ืืืืฆืจ ืงืืฉืืจ ืฆืืืืจื
|
|
|
|
|
|
|
|
|