Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import re
|
| 2 |
+
|
| 3 |
+
def preprocess_arabic_text(text):
|
| 4 |
+
# Remove diacritics
|
| 5 |
+
text = re.sub(r'[\u064B-\u0652]', '', text)
|
| 6 |
+
# Remove punctuation and non-Arabic characters
|
| 7 |
+
text = re.sub(r'[^\u0600-\u06FF\s]', '', text)
|
| 8 |
+
# Normalize Arabic letters
|
| 9 |
+
text = re.sub(r'\u0629', '\u0647', text) # Replace Teh Marbuta with Heh
|
| 10 |
+
text = re.sub(r'\u064A', '\u0649', text) # Replace Yeh with Alef Maqsura
|
| 11 |
+
|
| 12 |
+
# Remove diacritics (optional, depending on use case)
|
| 13 |
+
text = re.sub(r'[\u064B-\u065F]', '', text)
|
| 14 |
+
|
| 15 |
+
# Normalize elongated letters (e.g., "جدااا" -> "جدا")
|
| 16 |
+
text = re.sub(r'(.)\1{2,}', r'\1\1', text)
|
| 17 |
+
|
| 18 |
+
# Remove non-Arabic characters (e.g., English words, numbers, special symbols)
|
| 19 |
+
text = re.sub(r'[^\u0600-\u06FF\s]', '', text)
|
| 20 |
+
|
| 21 |
+
# Normalize whitespace
|
| 22 |
+
text = re.sub(r'\s+', ' ', text).strip()
|
| 23 |
+
|
| 24 |
+
return text
|
| 25 |
+
|
| 26 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 27 |
+
import torch
|
| 28 |
+
|
| 29 |
+
# Load the tokenizer and model
|
| 30 |
+
model_name = 'aubmindlab/bert-base-arabertv02'
|
| 31 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 32 |
+
model = AutoModelForSequenceClassification.from_pretrained(model_name, num_labels=3)
|
| 33 |
+
|
| 34 |
+
def analyze_sentiment(text):
|
| 35 |
+
inputs = tokenizer(text, return_tensors='pt', truncation=True, padding=True)
|
| 36 |
+
with torch.no_grad():
|
| 37 |
+
outputs = model(**inputs)
|
| 38 |
+
logits = outputs.logits
|
| 39 |
+
predicted_class = torch.argmax(logits, dim=1).item()
|
| 40 |
+
sentiment_map = {0: 'Negative', 1: 'Neutral', 2: 'Positive'}
|
| 41 |
+
return sentiment_map[predicted_class]
|
| 42 |
+
|
| 43 |
+
import gradio as gr
|
| 44 |
+
|
| 45 |
+
def process_text_and_analyze_sentiment(text):
|
| 46 |
+
preprocessed_text = preprocess_arabic_text(text)
|
| 47 |
+
sentiment = analyze_sentiment(preprocessed_text)
|
| 48 |
+
return preprocessed_text, sentiment
|
| 49 |
+
|
| 50 |
+
# Create the Gradio interface
|
| 51 |
+
iface = gr.Interface(
|
| 52 |
+
fn=process_text_and_analyze_sentiment,
|
| 53 |
+
inputs=gr.Textbox(label="Enter Arabic Text"),
|
| 54 |
+
outputs=[
|
| 55 |
+
gr.Textbox(label="Preprocessed Text"),
|
| 56 |
+
gr.Textbox(label="Sentiment")
|
| 57 |
+
],
|
| 58 |
+
title="Arabic Text Analysis",
|
| 59 |
+
description="This application preprocesses Arabic text using regex and analyzes sentiment using a pre-trained model."
|
| 60 |
+
)
|
| 61 |
+
|
| 62 |
+
# Launch the interface
|
| 63 |
+
if __name__ == "__main__":
|
| 64 |
+
iface.launch(share=True)
|
| 65 |
+
|