farouk1's picture
Create app.py
3af5f2a verified
raw
history blame
3.53 kB
import gradio as gr
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
# نموذج أقوى
MODEL = "google/flan-t5-xl"
tokenizer = AutoTokenizer.from_pretrained(MODEL)
model = AutoModelForSeq2SeqLM.from_pretrained(MODEL)
# مولد النصوص
def generate_content(topic, style_choice, lang_choice, length_choice):
# اختيار الطول
if length_choice == "Short":
max_len = 200
elif length_choice == "Medium":
max_len = 400
else: # Long
max_len = 700
# بناء البرومبت
if lang_choice == "Arabic":
if style_choice == "Blog Post (Descriptive)":
prompt = f"اكتب مقالاً احترافياً بأسلوب شخصي عن: {topic}. ركز على التفاصيل والوصف الجذاب. اجعل النص منسقاً بفقرات."
elif style_choice == "Social Media Post (Short & Catchy)":
prompt = f"اكتب منشوراً قصيراً وجذاباً عن: {topic}. أضف إيموجي مناسبة واقترح 3 هاشتاغات."
else: # Video Script
prompt = f"اكتب سيناريو فيديو احترافي عن: {topic}. اجعل الأسلوب قصصي وسردي مع تقسيمه إلى مشاهد."
else: # English
if style_choice == "Blog Post (Descriptive)":
prompt = f"Write a professional blog post about: {topic}. Make it personal and descriptive, well-structured in paragraphs."
elif style_choice == "Social Media Post (Short & Catchy)":
prompt = f"Write a short and catchy social media post about: {topic}, add emojis and suggest 3 hashtags."
else: # Video Script
prompt = f"Write a professional video script about: {topic}. Make it emotional, story-driven, and divided into scenes."
try:
inputs = tokenizer(prompt, return_tensors="pt")
outputs = model.generate(
**inputs,
max_length=max_len,
num_beams=5,
temperature=0.8,
early_stopping=True
)
content = tokenizer.decode(outputs[0], skip_special_tokens=True)
return content
except Exception as e:
return f"⚠️ Error: {str(e)}"
# واجهة Gradio
with gr.Blocks(theme="default") as iface:
gr.Markdown("## ✨ AI Content Pack - Create Blogs, Posts & Scripts in Seconds")
with gr.Row():
topic = gr.Textbox(label="Topic / الموضوع", placeholder="مثال: رحلتي إلى باريس وتجربة برج إيفل...")
with gr.Row():
style_choice = gr.Radio(
["Blog Post (Descriptive)", "Social Media Post (Short & Catchy)", "Video Script (Storytelling)"],
label="Style / نوع المحتوى"
)
lang_choice = gr.Radio(["Arabic", "English"], label="Language / اللغة")
length_choice = gr.Radio(["Short", "Medium", "Long"], label="Length / طول النص", value="Medium")
output = gr.Textbox(label="Generated Content", lines=20)
download_btn = gr.File(label="Download", file_types=[".txt"])
def save_file(content):
with open("generated.txt", "w", encoding="utf-8") as f:
f.write(content)
return "generated.txt"
btn = gr.Button("🚀 Generate Content")
btn.click(fn=generate_content, inputs=[topic, style_choice, lang_choice, length_choice], outputs=output)
output.change(fn=save_file, inputs=output, outputs=download_btn)
if __name__ == "__main__":
iface.launch()