|
|
import gradio as gr |
|
|
from transformers import pipeline |
|
|
|
|
|
|
|
|
model = pipeline("text-classification", |
|
|
model="CAMeL-Lab/bert-base-arabic-camelbert-da-sentiment") |
|
|
|
|
|
from fastapi import FastAPI |
|
|
from pydantic import BaseModel |
|
|
|
|
|
app = FastAPI() |
|
|
|
|
|
class Item(BaseModel): |
|
|
data: list |
|
|
|
|
|
@app.post("/predict") |
|
|
def predict(item: Item): |
|
|
text = item.data[0] |
|
|
|
|
|
|
|
|
result = analyze(text) |
|
|
|
|
|
return {"data": [result]} |
|
|
def analyze(text): |
|
|
if not text.strip(): |
|
|
return "الرجاء إدخال نص للتحليل" |
|
|
|
|
|
result = model(text)[0] |
|
|
return f"المشاعر: {result['label']}, الثقة: {result['score']:.2%}" |
|
|
|
|
|
|
|
|
gr.Interface( |
|
|
fn=analyze, |
|
|
inputs=gr.Textbox(label="أدخل النص العربي"), |
|
|
outputs=gr.Textbox(label="النتيجة"), |
|
|
title="محلل المشاعر العربي" |
|
|
).launch() |