Update app.py
Browse files
app.py
CHANGED
|
@@ -1,59 +1,47 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from transformers import pipeline
|
| 3 |
import random
|
|
|
|
| 4 |
|
| 5 |
-
# المرحلة 1: توليد
|
| 6 |
-
|
| 7 |
-
"
|
| 8 |
-
model="
|
| 9 |
)
|
| 10 |
|
| 11 |
-
# المرحلة 2: تحويل
|
| 12 |
poetry_generator = pipeline(
|
| 13 |
"text-generation",
|
| 14 |
-
model="akhooli/gpt2-small-arabic-poetry"
|
| 15 |
)
|
| 16 |
|
| 17 |
def dual_inspire(future, value, skill):
|
| 18 |
if not future or not value or not skill:
|
| 19 |
-
return "⚠️ رجاءً أجيبي على جميع
|
| 20 |
|
| 21 |
-
#
|
| 22 |
idea_prompt = (
|
| 23 |
f"اكتب جملة قصيرة بالعربية تصف مستقبل شخص يريد {future}، "
|
| 24 |
f"ويؤمن بـ {value}، ويمتلك مهارة {skill}. "
|
| 25 |
-
"لتكن بسيطة،
|
| 26 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
idea_text = idea_result[0]["generated_text"].strip()
|
| 31 |
-
if not idea_text or len(idea_text) < 10:
|
| 32 |
-
idea_text = "تمضي نحو غدٍ تصنع فيه أثرًا من ضوءٍ وإصرار."
|
| 33 |
-
|
| 34 |
-
# المرحلة الثانية: تحويل الجملة إلى شعر
|
| 35 |
-
poetry_prompt = (
|
| 36 |
-
f"حوّل الجملة التالية إلى بيت أو سطر شعري جميل بالعربية: {idea_text}"
|
| 37 |
-
)
|
| 38 |
-
|
| 39 |
-
poetic_result = poetry_generator(
|
| 40 |
-
poetry_prompt,
|
| 41 |
-
max_new_tokens=60,
|
| 42 |
-
temperature=1.2,
|
| 43 |
-
top_p=0.95
|
| 44 |
-
)
|
| 45 |
-
poetic_text = poetic_result[0]["generated_text"].strip()
|
| 46 |
-
|
| 47 |
-
# تنظيف المخرجات
|
| 48 |
-
if poetic_text.startswith(poetry_prompt):
|
| 49 |
-
poetic_text = poetic_text[len(poetry_prompt):].strip()
|
| 50 |
-
|
| 51 |
-
poetic_text = poetic_text.replace("\n", " ").split(".")[0].strip()
|
| 52 |
-
|
| 53 |
-
return f"🌱 الجملة الأساسية:\n{idea_text}\n\n🎨 النسخة الشاعرية:\n{poetic_text}"
|
| 54 |
-
|
| 55 |
-
except Exception as e:
|
| 56 |
-
return f"⚠️ حدث خطأ أثناء التوليد: {e}"
|
| 57 |
|
| 58 |
# واجهة Gradio
|
| 59 |
demo = gr.Interface(
|
|
@@ -63,9 +51,9 @@ demo = gr.Interface(
|
|
| 63 |
gr.Textbox(label="💡 الأثر الذي تريدين تركه"),
|
| 64 |
gr.Textbox(label="⚙️ مهارتك المميزة"),
|
| 65 |
],
|
| 66 |
-
outputs=gr.Textbox(label="🌟 الجملة النهائية (
|
| 67 |
title="ثنائية الإلهام – من الفكرة إلى الشعر 💫",
|
| 68 |
-
description="الذكاء الاصطناعي
|
| 69 |
theme="soft"
|
| 70 |
)
|
| 71 |
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
import random
|
| 3 |
+
from transformers import pipeline
|
| 4 |
|
| 5 |
+
# المرحلة 1: نموذج توليد الفكرة (عربي فعلي)
|
| 6 |
+
idea_generator = pipeline(
|
| 7 |
+
"text-generation",
|
| 8 |
+
model="akhooli/gpt2-small-arabic",
|
| 9 |
)
|
| 10 |
|
| 11 |
+
# المرحلة 2: نموذج تحويل الفكرة إلى شعر
|
| 12 |
poetry_generator = pipeline(
|
| 13 |
"text-generation",
|
| 14 |
+
model="akhooli/gpt2-small-arabic-poetry",
|
| 15 |
)
|
| 16 |
|
| 17 |
def dual_inspire(future, value, skill):
|
| 18 |
if not future or not value or not skill:
|
| 19 |
+
return "⚠️ رجاءً أجيبي على جميع الحقول."
|
| 20 |
|
| 21 |
+
# 1️⃣ توليد الفكرة الأساسية
|
| 22 |
idea_prompt = (
|
| 23 |
f"اكتب جملة قصيرة بالعربية تصف مستقبل شخص يريد {future}، "
|
| 24 |
f"ويؤمن بـ {value}، ويمتلك مهارة {skill}. "
|
| 25 |
+
"لتكن جملة بسيطة، واضحة، وملهمة."
|
| 26 |
)
|
| 27 |
+
idea = idea_generator(idea_prompt, max_new_tokens=40, temperature=0.9, top_p=0.9)[0]["generated_text"]
|
| 28 |
+
# تنظيف النص
|
| 29 |
+
idea = idea.replace("\n", " ").strip().split(".")[0]
|
| 30 |
+
if len(idea) < 15:
|
| 31 |
+
idea = "يسعى نحو غدٍ يصنع فيه أثرًا من ضوءٍ وإصرار."
|
| 32 |
+
|
| 33 |
+
# 2️⃣ توليد النسخة الشعرية بناءً على الفكرة
|
| 34 |
+
poetry_prompt = (
|
| 35 |
+
f"حوّل الجملة التالية إلى شعر عربي جميل:\n"
|
| 36 |
+
f"\"{idea}\"\n"
|
| 37 |
+
"لتكن شاعرية بإحساس إنساني ولغة عاطفية."
|
| 38 |
+
)
|
| 39 |
+
poem = poetry_generator(poetry_prompt, max_new_tokens=60, temperature=1.2, top_p=0.95)[0]["generated_text"]
|
| 40 |
+
# تنظيف الشعر
|
| 41 |
+
poem = poem.replace(poetry_prompt, "").strip().split(".")[0]
|
| 42 |
|
| 43 |
+
# 3️⃣ دمج النتيجة
|
| 44 |
+
return f"🌱 الفكرة الأساسية:\n{idea}\n\n🎨 النسخة الشاعرية:\n{poem}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
# واجهة Gradio
|
| 47 |
demo = gr.Interface(
|
|
|
|
| 51 |
gr.Textbox(label="💡 الأثر الذي تريدين تركه"),
|
| 52 |
gr.Textbox(label="⚙️ مهارتك المميزة"),
|
| 53 |
],
|
| 54 |
+
outputs=gr.Textbox(label="🌟 الجملة النهائية (فكرة + شعر)", lines=6),
|
| 55 |
title="ثنائية الإلهام – من الفكرة إلى الشعر 💫",
|
| 56 |
+
description="الذكاء الاصطناعي يولّد فكرة عربية ثم يحولها إلى شعر جميل يعبّر عنك.",
|
| 57 |
theme="soft"
|
| 58 |
)
|
| 59 |
|