ThumbnailAI / app.py
Aibadr's picture
Update app.py
dc120ca verified
import streamlit as st
import torch
from diffusers import StableDiffusionPipeline
from PIL import Image, ImageDraw, ImageFont
import arabic_reshaper
from bidi.algorithm import get_display
import io
import os
st.set_page_config(page_title="Good Religion Final", layout="wide")
st.title("🎬 مولد الثمنيل الاحترافي - Good Religion")
def process_arabic(text):
if not text: return ""
reshaped = arabic_reshaper.reshape(text)
return get_display(reshaped)
def get_font(font_size):
# محاولة إيجاد الخط في عدة مسارات
font_paths = ["Amiri-Bold.ttf", "/home/user/app/Amiri-Bold.ttf", "src/Amiri-Bold.ttf"]
for path in font_paths:
if os.path.exists(path):
return ImageFont.truetype(path, font_size)
return ImageFont.load_default()
def add_overlays(image, title_text, channel_name="Good Religion"):
width, height = image.size
draw = ImageDraw.Draw(image)
# 1. إضافة اسم القناة في الزاوية اليمنى السفلى
try:
c_font = get_font(30)
draw.text((width - 200, height - 50), channel_name, fill="white", font=c_font, stroke_width=1, stroke_fill="black")
except:
draw.text((width - 150, height - 50), channel_name, fill="white")
# 2. إضافة العنوان العربي في المنتصف
if title_text:
try:
t_font = get_font(80)
reshaped_text = process_arabic(title_text)
# حساب مكان النص في المنتصف
w, h = draw.textbbox((0, 0), reshaped_text, font=t_font)[2:]
draw.text(((width - w) / 2, height - 180), reshaped_text, font=t_font, fill="#FFD700", stroke_width=3, stroke_fill="black")
except Exception as e:
st.error(f"مشكلة في الخط العربي: {e}")
return image
# المدخلات
prompt = st.text_input("وصف الصورة (English):", "Ancient islamic architecture, sunset")
title = st.text_input("العنوان العربي:", "قصص الأنبياء")
if st.button("✨ إنتاج الصورة"):
with st.spinner("جاري الرسم والكتابة..."):
try:
model_id = "segmind/tiny-sd"
pipe = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float32)
pipe = pipe.to("cpu")
# توليد الصورة
image = pipe(f"{prompt}, cinematic, masterpiece", num_inference_steps=20).images[0]
image = image.resize((1280, 720), Image.Resampling.LANCZOS)
# إضافة النصوص
final_image = add_overlays(image, title)
st.image(final_image, use_column_width=True)
buf = io.BytesIO()
final_image.save(buf, format="PNG")
st.download_button("📥 تحميل الصورة", buf.getvalue(), "Good_Religion_Thumb.png")
except Exception as e:
st.error(f"خطأ تقني: {e}")