Spaces:
Sleeping
Sleeping
app.py
Browse files
app.py
ADDED
@@ -0,0 +1,88 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# ✅ تثبيت المكتبات المطلوبة
|
2 |
+
!pip install -q transformers gradio torch diffusers accelerate speechbrain Pillow
|
3 |
+
|
4 |
+
# ✅ تحميل المكتبات
|
5 |
+
from transformers import pipeline, AutoModelForCausalLM, AutoTokenizer
|
6 |
+
from diffusers import StableDiffusionPipeline
|
7 |
+
import gradio as gr
|
8 |
+
import torch
|
9 |
+
from PIL import Image
|
10 |
+
import speechbrain as sb
|
11 |
+
|
12 |
+
# ✅ تحميل نموذج المحادثة (GPT-like)
|
13 |
+
chat_model = pipeline("text-generation", model="akhooli/gpt2-small-arabic")
|
14 |
+
|
15 |
+
# ✅ تحميل نموذج توليد الصور (Stable Diffusion)
|
16 |
+
image_pipe = StableDiffusionPipeline.from_pretrained("stabilityai/stable-diffusion-xl-base-1.0")
|
17 |
+
image_pipe.to("cuda" if torch.cuda.is_available() else "cpu")
|
18 |
+
|
19 |
+
# ✅ تحميل نموذج تحليل الصور (ResNet)
|
20 |
+
vision_pipe = pipeline("image-classification", model="microsoft/resnet-50")
|
21 |
+
|
22 |
+
# ✅ تحميل نموذج استخراج النصوص (OCR)
|
23 |
+
ocr_pipe = pipeline("image-to-text", model="microsoft/trocr-base-printed")
|
24 |
+
|
25 |
+
# ✅ تحميل نموذج تحويل الكلام إلى نص (STT)
|
26 |
+
asr_pipe = sb.pretrained_models.Wav2Vec2ASR.from_hparams(source="speechbrain/asr-wav2vec2-commonvoice-ar")
|
27 |
+
|
28 |
+
# ✅ دالة المحادثة
|
29 |
+
def chat(user_input):
|
30 |
+
response = chat_model(user_input, max_length=100)[0]['generated_text']
|
31 |
+
return response
|
32 |
+
|
33 |
+
# ✅ دالة توليد الصور
|
34 |
+
def generate_image(prompt):
|
35 |
+
image = image_pipe(prompt).images[0]
|
36 |
+
return image
|
37 |
+
|
38 |
+
# ✅ دالة تحليل الصور
|
39 |
+
def analyze_image(image):
|
40 |
+
result = vision_pipe(image)
|
41 |
+
return [{"label": r["label"], "score": r["score"]} for r in result]
|
42 |
+
|
43 |
+
# ✅ دالة استخراج النصوص من الصور
|
44 |
+
def extract_text(image):
|
45 |
+
text = ocr_pipe(image)[0]['generated_text']
|
46 |
+
return text
|
47 |
+
|
48 |
+
# ✅ دالة تحويل الكلام إلى نص
|
49 |
+
def speech_to_text(audio_file):
|
50 |
+
text = asr_pipe.transcribe_file(audio_file)
|
51 |
+
return text
|
52 |
+
|
53 |
+
# ✅ إنشاء واجهة Gradio
|
54 |
+
with gr.Blocks(title="الذكاء الاصطناعي المتكامل") as app:
|
55 |
+
gr.Markdown("# نظام ذكاء اصطناعي متكامل 🚀")
|
56 |
+
|
57 |
+
with gr.Tab("💬 الدردشة"):
|
58 |
+
chat_input = gr.Textbox(label="اكتب رسالتك...")
|
59 |
+
chat_output = gr.Textbox(label="الرد")
|
60 |
+
chat_btn = gr.Button("إرسال")
|
61 |
+
chat_btn.click(chat, inputs=chat_input, outputs=chat_output)
|
62 |
+
|
63 |
+
with gr.Tab("🎨 توليد الصور"):
|
64 |
+
image_prompt = gr.Textbox(label="وصف الصورة")
|
65 |
+
image_output = gr.Image(label="الصورة المولدة")
|
66 |
+
image_btn = gr.Button("توليد")
|
67 |
+
image_btn.click(generate_image, inputs=image_prompt, outputs=image_output)
|
68 |
+
|
69 |
+
with gr.Tab("🔍 تحليل الصور"):
|
70 |
+
image_input = gr.Image(type="pil", label="الصورة المدخلة")
|
71 |
+
analysis_output = gr.JSON(label="نتيجة التحليل")
|
72 |
+
analyze_btn = gr.Button("حلل")
|
73 |
+
analyze_btn.click(analyze_image, inputs=image_input, outputs=analysis_output)
|
74 |
+
|
75 |
+
with gr.Tab("📜 استخراج النصوص"):
|
76 |
+
ocr_input = gr.Image(type="pil", label="الصورة المدخلة")
|
77 |
+
ocr_output = gr.Textbox(label="النص المستخرج")
|
78 |
+
ocr_btn = gr.Button("استخرج النص")
|
79 |
+
ocr_btn.click(extract_text, inputs=ocr_input, outputs=ocr_output)
|
80 |
+
|
81 |
+
with gr.Tab("🎙️ تحويل الكلام إلى نص"):
|
82 |
+
stt_input = gr.Audio(type="filepath", label="الصوت المدخل")
|
83 |
+
stt_output = gr.Textbox(label="النص المستخرج")
|
84 |
+
stt_btn = gr.Button("حول الكلام إلى نص")
|
85 |
+
stt_btn.click(speech_to_text, inputs=stt_input, outputs=stt_output)
|
86 |
+
|
87 |
+
# ✅ تشغيل التطبيق
|
88 |
+
app.launch(debug=True)
|