|
import gradio as gr |
|
import pandas as pd |
|
from transformers import pipeline |
|
|
|
|
|
summarizer = pipeline("summarization", model="microsoft/mdeberta-v3-base") |
|
|
|
|
|
def load_and_analyze(file): |
|
global df |
|
df = pd.read_csv(file.name) |
|
return f"تم تحميل البيانات بنجاح! عدد الصفوف: {df.shape[0]}, عدد الأعمدة: {df.shape[1]}" |
|
|
|
|
|
def answer_query(query): |
|
global df |
|
if df is None: |
|
return "❌ الرجاء تحميل ملف البيانات أولاً." |
|
|
|
query = query.lower() |
|
if "المتوسط" in query or "المعدل" in query: |
|
result = df.mean().to_string() |
|
elif "أكبر قيمة" in query or "القيمة العليا" in query: |
|
result = df.max().to_string() |
|
elif "أصغر قيمة" in query or "القيمة الدنيا" in query: |
|
result = df.min().to_string() |
|
elif "ملخص" in query: |
|
stats = df.describe().to_string() |
|
summary = summarizer(stats, max_length=100, do_sample=False)[0]['summary_text'] |
|
result = f"📊 **ملخص البيانات:**\n{summary}" |
|
else: |
|
result = "❓ لم أفهم السؤال. حاول إعادة صياغته." |
|
|
|
return result |
|
|
|
|
|
with gr.Blocks() as app: |
|
gr.Markdown("## 🤖 Chatbot لمحللي البيانات") |
|
|
|
file_input = gr.File(label="📂 رفع ملف CSV") |
|
file_output = gr.Textbox(label="🔍 حالة البيانات") |
|
file_input.change(load_and_analyze, inputs=file_input, outputs=file_output) |
|
|
|
chat_input = gr.Textbox(label="💬 اسأل عن البيانات") |
|
chat_output = gr.Textbox(label="🤖 إجابة الذكاء الاصطناعي") |
|
chat_input.submit(answer_query, inputs=chat_input, outputs=chat_output) |
|
|
|
app.launch() |