Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,16 +5,35 @@ 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 |
|
| 9 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
try:
|
| 11 |
-
|
| 12 |
-
|
| 13 |
except Exception as e:
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
# ืงืืืช ืืื ืืงืก ืืืืืื ืฉื IMDB
|
| 17 |
-
word_index = imdb.get_word_index()
|
| 18 |
|
| 19 |
def preprocess_text(text):
|
| 20 |
# ืืืจื ืืืืืื
|
|
@@ -32,6 +51,13 @@ def preprocess_text(text):
|
|
| 32 |
return vector
|
| 33 |
|
| 34 |
def predict_sentiment(text):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
try:
|
| 36 |
# ืขืืืื ืืืงืกื
|
| 37 |
processed_text = preprocess_text(text)
|
|
@@ -47,9 +73,8 @@ def predict_sentiment(text):
|
|
| 47 |
"Confidence": f"{confidence:.2%}"
|
| 48 |
}
|
| 49 |
except Exception as e:
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
}
|
| 53 |
|
| 54 |
# ืืฆืืจืช ืืืฉืง Gradio
|
| 55 |
iface = gr.Interface(
|
|
@@ -68,6 +93,9 @@ iface = gr.Interface(
|
|
| 68 |
theme=gr.themes.Soft()
|
| 69 |
)
|
| 70 |
|
| 71 |
-
#
|
| 72 |
if __name__ == "__main__":
|
| 73 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
word_index = imdb.get_word_index()
|
| 33 |
+
logger.info("ืืื ืืงืก ืืืืืื ื ืืขื ืืืฆืืื")
|
| 34 |
except Exception as e:
|
| 35 |
+
logger.error(f"ืฉืืืื ืืืขืื ืช ืืื ืืงืก ืืืืืื: {e}")
|
| 36 |
+
raise
|
|
|
|
|
|
|
| 37 |
|
| 38 |
def preprocess_text(text):
|
| 39 |
# ืืืจื ืืืืืื
|
|
|
|
| 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 |
"Confidence": f"{confidence:.2%}"
|
| 74 |
}
|
| 75 |
except Exception as e:
|
| 76 |
+
logger.error(f"ืฉืืืื ืืืืฆืืข ืืืืืื: {e}")
|
| 77 |
+
return {"Error": str(e)}
|
|
|
|
| 78 |
|
| 79 |
# ืืฆืืจืช ืืืฉืง Gradio
|
| 80 |
iface = gr.Interface(
|
|
|
|
| 93 |
theme=gr.themes.Soft()
|
| 94 |
)
|
| 95 |
|
| 96 |
+
# ืืขืื ืช ืืืืื ืืืคืขืื ืืจืืฉืื ืืช
|
| 97 |
if __name__ == "__main__":
|
| 98 |
+
if load_model():
|
| 99 |
+
iface.launch(share=True)
|
| 100 |
+
else:
|
| 101 |
+
logger.error("ืื ื ืืชื ืืืคืขืื ืืช ืืืืฉืง - ืืืืื ืื ื ืืขื ืืืฆืืื")
|