Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 4 |
+
|
| 5 |
+
# تحميل النموذج العربي
|
| 6 |
+
model_name = "CAMeL-Lab/bert-base-arabic-camelbert-da-sentiment"
|
| 7 |
+
|
| 8 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 9 |
+
model = AutoModelForSequenceClassification.from_pretrained(model_name)
|
| 10 |
+
|
| 11 |
+
labels = ["negative", "neutral", "positive"]
|
| 12 |
+
|
| 13 |
+
# دالة تحليل المشاعر
|
| 14 |
+
def analyze_arabic_sentiment(text):
|
| 15 |
+
if not text.strip():
|
| 16 |
+
return "الرجاء إدخال نص"
|
| 17 |
+
|
| 18 |
+
inputs = tokenizer(text, return_tensors="pt", truncation=True)
|
| 19 |
+
outputs = model(**inputs)
|
| 20 |
+
probs = torch.nn.functional.softmax(outputs.logits, dim=1)
|
| 21 |
+
label_id = torch.argmax(probs).item()
|
| 22 |
+
confidence = float(probs[0][label_id])
|
| 23 |
+
|
| 24 |
+
return f"التصنيف: {labels[label_id]}\nدرجة الثقة: {round(confidence, 3)}"
|
| 25 |
+
|
| 26 |
+
# واجهة Gradio
|
| 27 |
+
with gr.Blocks() as demo:
|
| 28 |
+
gr.Markdown("## 🔍 Arabic Sentiment Analyzer")
|
| 29 |
+
gr.Markdown("اكتب نصًا عربيًا وسيتم تحديد نوع المشاعر.")
|
| 30 |
+
|
| 31 |
+
text_input = gr.Textbox(label="النص")
|
| 32 |
+
text_output = gr.Textbox(label="النتيجة")
|
| 33 |
+
|
| 34 |
+
analyze_btn = gr.Button("تحليل")
|
| 35 |
+
|
| 36 |
+
analyze_btn.click(fn=analyze_arabic_sentiment, inputs=text_input, outputs=text_output)
|
| 37 |
+
|
| 38 |
+
demo.launch()
|