Analysisofsent / app.py
ahmedyoussef1's picture
Create app.py
ee4fcdc verified
raw
history blame
1.71 kB
import gradio as gr
import numpy as np
import tensorflow as tf
from huggingface_hub import hf_hub_download
from tensorflow.keras.preprocessing.sequence import pad_sequences
import pickle
# -------- تحميل النموذج --------
# تحميل النموذج من Hugging Face
model_path = hf_hub_download(repo_id="ahmedyoussef1/Asentement_analysis_rnn", filename="rnn_Bi.h5")
model = tf.keras.models.load_model(model_path)
# تحميل tokenizer
tokenizer_path = hf_hub_download(repo_id="ahmedyoussef1/sentiment_rnn", filename="tokenizer.pkl")
with open(tokenizer_path, 'rb') as f:
tokenizer = pickle.load(f)
# طول التسلسل (من وقت التدريب)
max_len = 100
# أسماء التصنيفات
class_names = ['Negative', 'Positive']
# -------- دالة التنبؤ --------
def predict_sentiment(text):
sequence = tokenizer.texts_to_sequences([text])
padded = pad_sequences(sequence, maxlen=max_len, padding='post')
prediction = model.predict(padded)[0][0] # لأنه binary: توقع قيمة واحدة بين 0 و 1
# نسبة الاحتمال
result = {
"Negative": float(1 - prediction),
"Positive": float(prediction)
}
return result
# -------- واجهة Gradio --------
interface = gr.Interface(
fn=predict_sentiment,
inputs=gr.Textbox(lines=3, placeholder="اكتب جملة لتحليل المشاعر..."),
outputs=gr.Label(num_top_classes=2),
title="تحليل المشاعر باستخدام Bi-RNN",
description="ادخل جملة وسيتم تصنيفها كـ مشاعر إيجابية أو سلبية باستخدام نموذج Bi-RNN.",
allow_flagging="never"
)
interface.launch()