File size: 3,742 Bytes
73ac0c0
 
 
 
 
 
1e1aa53
73ac0c0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1e1aa53
73ac0c0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# ✅ تحميل المكتبات
from transformers import pipeline, AutoModelForCausalLM, AutoTokenizer
from diffusers import StableDiffusionPipeline
import gradio as gr
import torch
from PIL import Image
from speechbrain.pretrained import EncoderASR  # تم التعديل هنا

# ✅ تحميل نموذج المحادثة (GPT-like)
chat_model = pipeline("text-generation", model="akhooli/gpt2-small-arabic")

# ✅ تحميل نموذج توليد الصور (Stable Diffusion)
image_pipe = StableDiffusionPipeline.from_pretrained("stabilityai/stable-diffusion-xl-base-1.0")
image_pipe.to("cuda" if torch.cuda.is_available() else "cpu")

# ✅ تحميل نموذج تحليل الصور (ResNet)
vision_pipe = pipeline("image-classification", model="microsoft/resnet-50")

# ✅ تحميل نموذج استخراج النصوص (OCR)
ocr_pipe = pipeline("image-to-text", model="microsoft/trocr-base-printed")

# ✅ تحميل نموذج تحويل الكلام إلى نص (STT)
asr_pipe = EncoderASR.from_hparams(source="speechbrain/asr-wav2vec2-commonvoice-ar", savedir="pretrained_models/asr-wav2vec2-commonvoice-ar")  # تم التعديل هنا

# ✅ دالة المحادثة
def chat(user_input):
    response = chat_model(user_input, max_length=100)[0]['generated_text']
    return response

# ✅ دالة توليد الصور
def generate_image(prompt):
    image = image_pipe(prompt).images[0]
    return image

# ✅ دالة تحليل الصور
def analyze_image(image):
    result = vision_pipe(image)
    return [{"label": r["label"], "score": r["score"]} for r in result]

# ✅ دالة استخراج النصوص من الصور
def extract_text(image):
    text = ocr_pipe(image)[0]['generated_text']
    return text

# ✅ دالة تحويل الكلام إلى نص
def speech_to_text(audio_file):
    text = asr_pipe.transcribe_file(audio_file)
    return text

# ✅ إنشاء واجهة Gradio
with gr.Blocks(title="الذكاء الاصطناعي المتكامل") as app:
    gr.Markdown("# نظام ذكاء اصطناعي متكامل 🚀")
    
    with gr.Tab("💬 الدردشة"):
        chat_input = gr.Textbox(label="اكتب رسالتك...")
        chat_output = gr.Textbox(label="الرد")
        chat_btn = gr.Button("إرسال")
        chat_btn.click(chat, inputs=chat_input, outputs=chat_output)
    
    with gr.Tab("🎨 توليد الصور"):
        image_prompt = gr.Textbox(label="وصف الصورة")
        image_output = gr.Image(label="الصورة المولدة")
        image_btn = gr.Button("توليد")
        image_btn.click(generate_image, inputs=image_prompt, outputs=image_output)
    
    with gr.Tab("🔍 تحليل الصور"):
        image_input = gr.Image(type="pil", label="الصورة المدخلة")
        analysis_output = gr.JSON(label="نتيجة التحليل")
        analyze_btn = gr.Button("حلل")
        analyze_btn.click(analyze_image, inputs=image_input, outputs=analysis_output)
    
    with gr.Tab("📜 استخراج النصوص"):
        ocr_input = gr.Image(type="pil", label="الصورة المدخلة")
        ocr_output = gr.Textbox(label="النص المستخرج")
        ocr_btn = gr.Button("استخرج النص")
        ocr_btn.click(extract_text, inputs=ocr_input, outputs=ocr_output)
    
    with gr.Tab("🎙️ تحويل الكلام إلى نص"):
        stt_input = gr.Audio(type="filepath", label="الصوت المدخل")
        stt_output = gr.Textbox(label="النص المستخرج")
        stt_btn = gr.Button("حول الكلام إلى نص")
        stt_btn.click(speech_to_text, inputs=stt_input, outputs=stt_output)

# ✅ تشغيل التطبيق
app.launch(debug=True)