Spaces:
Sleeping
Sleeping
# ✅ تحميل المكتبات | |
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) |