Spaces:
Build error
Build error
Create app.py
Browse files
app.py
CHANGED
|
@@ -1,44 +1,78 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
| 3 |
|
| 4 |
-
#
|
| 5 |
-
MODEL = "google/flan-t5-
|
| 6 |
tokenizer = AutoTokenizer.from_pretrained(MODEL)
|
| 7 |
model = AutoModelForSeq2SeqLM.from_pretrained(MODEL)
|
| 8 |
|
| 9 |
-
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
if lang_choice == "Arabic":
|
| 12 |
if style_choice == "Blog Post (Descriptive)":
|
| 13 |
-
prompt = f"اكتب مقالاً
|
| 14 |
elif style_choice == "Social Media Post (Short & Catchy)":
|
| 15 |
-
prompt = f"اكتب منشوراً قصيراً وجذاباً
|
| 16 |
-
else:
|
| 17 |
-
prompt = f"اكتب سيناريو فيديو
|
| 18 |
-
else:
|
| 19 |
if style_choice == "Blog Post (Descriptive)":
|
| 20 |
-
prompt = f"Write a
|
| 21 |
elif style_choice == "Social Media Post (Short & Catchy)":
|
| 22 |
-
prompt = f"Write a short and catchy social media post
|
| 23 |
-
else:
|
| 24 |
-
prompt = f"Write a video script
|
| 25 |
|
| 26 |
try:
|
| 27 |
inputs = tokenizer(prompt, return_tensors="pt")
|
| 28 |
-
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
except Exception as e:
|
| 31 |
-
return f"
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
gr.
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
if __name__ == "__main__":
|
| 44 |
-
iface.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
| 3 |
|
| 4 |
+
# نموذج أقوى
|
| 5 |
+
MODEL = "google/flan-t5-xl"
|
| 6 |
tokenizer = AutoTokenizer.from_pretrained(MODEL)
|
| 7 |
model = AutoModelForSeq2SeqLM.from_pretrained(MODEL)
|
| 8 |
|
| 9 |
+
# مولد النصوص
|
| 10 |
+
def generate_content(topic, style_choice, lang_choice, length_choice):
|
| 11 |
+
# اختيار الطول
|
| 12 |
+
if length_choice == "Short":
|
| 13 |
+
max_len = 200
|
| 14 |
+
elif length_choice == "Medium":
|
| 15 |
+
max_len = 400
|
| 16 |
+
else: # Long
|
| 17 |
+
max_len = 700
|
| 18 |
+
|
| 19 |
+
# بناء البرومبت
|
| 20 |
if lang_choice == "Arabic":
|
| 21 |
if style_choice == "Blog Post (Descriptive)":
|
| 22 |
+
prompt = f"اكتب مقالاً احترافياً بأسلوب شخصي عن: {topic}. ركز على التفاصيل والوصف الجذاب. اجعل النص منسقاً بفقرات."
|
| 23 |
elif style_choice == "Social Media Post (Short & Catchy)":
|
| 24 |
+
prompt = f"اكتب منشوراً قصيراً وجذاباً عن: {topic}. أضف إيموجي مناسبة واقترح 3 هاشتاغات."
|
| 25 |
+
else: # Video Script
|
| 26 |
+
prompt = f"اكتب سيناريو فيديو احترافي عن: {topic}. اجعل الأسلوب قصصي وسردي مع تقسيمه إلى مشاهد."
|
| 27 |
+
else: # English
|
| 28 |
if style_choice == "Blog Post (Descriptive)":
|
| 29 |
+
prompt = f"Write a professional blog post about: {topic}. Make it personal and descriptive, well-structured in paragraphs."
|
| 30 |
elif style_choice == "Social Media Post (Short & Catchy)":
|
| 31 |
+
prompt = f"Write a short and catchy social media post about: {topic}, add emojis and suggest 3 hashtags."
|
| 32 |
+
else: # Video Script
|
| 33 |
+
prompt = f"Write a professional video script about: {topic}. Make it emotional, story-driven, and divided into scenes."
|
| 34 |
|
| 35 |
try:
|
| 36 |
inputs = tokenizer(prompt, return_tensors="pt")
|
| 37 |
+
outputs = model.generate(
|
| 38 |
+
**inputs,
|
| 39 |
+
max_length=max_len,
|
| 40 |
+
num_beams=5,
|
| 41 |
+
temperature=0.8,
|
| 42 |
+
early_stopping=True
|
| 43 |
+
)
|
| 44 |
+
content = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 45 |
+
|
| 46 |
+
return content
|
| 47 |
except Exception as e:
|
| 48 |
+
return f"⚠️ Error: {str(e)}"
|
| 49 |
+
|
| 50 |
+
# واجهة Gradio
|
| 51 |
+
with gr.Blocks(theme="default") as iface:
|
| 52 |
+
gr.Markdown("## ✨ AI Content Pack - Create Blogs, Posts & Scripts in Seconds")
|
| 53 |
+
|
| 54 |
+
with gr.Row():
|
| 55 |
+
topic = gr.Textbox(label="Topic / الموضوع", placeholder="مثال: رحلتي إلى باريس وتجربة برج إيفل...")
|
| 56 |
+
|
| 57 |
+
with gr.Row():
|
| 58 |
+
style_choice = gr.Radio(
|
| 59 |
+
["Blog Post (Descriptive)", "Social Media Post (Short & Catchy)", "Video Script (Storytelling)"],
|
| 60 |
+
label="Style / نوع المحتوى"
|
| 61 |
+
)
|
| 62 |
+
lang_choice = gr.Radio(["Arabic", "English"], label="Language / اللغة")
|
| 63 |
+
length_choice = gr.Radio(["Short", "Medium", "Long"], label="Length / طول النص", value="Medium")
|
| 64 |
+
|
| 65 |
+
output = gr.Textbox(label="Generated Content", lines=20)
|
| 66 |
+
download_btn = gr.File(label="Download", file_types=[".txt"])
|
| 67 |
+
|
| 68 |
+
def save_file(content):
|
| 69 |
+
with open("generated.txt", "w", encoding="utf-8") as f:
|
| 70 |
+
f.write(content)
|
| 71 |
+
return "generated.txt"
|
| 72 |
+
|
| 73 |
+
btn = gr.Button("🚀 Generate Content")
|
| 74 |
+
btn.click(fn=generate_content, inputs=[topic, style_choice, lang_choice, length_choice], outputs=output)
|
| 75 |
+
output.change(fn=save_file, inputs=output, outputs=download_btn)
|
| 76 |
|
| 77 |
if __name__ == "__main__":
|
| 78 |
+
iface.launch()
|