3morixd commited on
Commit
ec05444
·
verified ·
1 Parent(s): b902de8

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +153 -0
app.py ADDED
@@ -0,0 +1,153 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import io
3
+ import gradio as gr
4
+ from huggingface_hub import InferenceClient
5
+ from PIL import Image
6
+
7
+ # --- Configuration -----------------------------------------------------------
8
+ HF_TOKEN = os.environ.get("HF_TOKEN", None)
9
+ MODEL_ID = "black-forest-labs/FLUX.1-schnell"
10
+
11
+ client = InferenceClient(model=MODEL_ID, token=HF_TOKEN)
12
+
13
+ BG_COLOR = "#0A0F1A"
14
+ ACCENT = "#1FE0E6"
15
+
16
+ # Calligraphy styles
17
+ STYLES = {
18
+ "Thuluth": "Thuluth script, elegant large sweeping curves, ornate flowing letters, classical",
19
+ "Naskh": "Naskh script, clean readable traditional book script, rounded letters, classic",
20
+ "Diwani": "Diwani script, decorative curved ornamental Ottoman style, intricate overlapping letters",
21
+ "Kufi": "Kufic script, geometric angular blocky letters, strict grid pattern, ancient architectural style",
22
+ "Modern": "Modern Arabic calligraphy, contemporary stylized abstract elegant fusion of traditional and modern art",
23
+ }
24
+
25
+ # Color schemes: name -> (foreground desc, background desc)
26
+ COLOR_SCHEMES = {
27
+ "Gold on Black": ("gold metallic letters", "deep black textured background"),
28
+ "White on Blue": ("pure white letters", "rich deep blue gradient background"),
29
+ "Silver on Dark": ("silver metallic letters", "dark charcoal gradient background"),
30
+ }
31
+
32
+ # Example prompts in Arabic
33
+ EXAMPLES_AR = [
34
+ "بسم الله الرحمن الرحيم",
35
+ "السلام عليكم ورحمة الله وبركاته",
36
+ "الحب للوطن من اليمان",
37
+ "العلم نور",
38
+ "صبر جميل",
39
+ "توكل على الله",
40
+ " Dispatch AI ",
41
+ ]
42
+
43
+
44
+ # --- Core function -----------------------------------------------------------
45
+ def build_prompt(arabic_text, style, color_scheme):
46
+ """Construct a detailed prompt for FLUX."""
47
+ style_desc = STYLES.get(style, STYLES["Thuluth"])
48
+ fg, bg = COLOR_SCHEMES.get(color_scheme, COLOR_SCHEMES["Gold on Black"])
49
+
50
+ text = arabic_text.strip() if arabic_text.strip() else "بسم الله"
51
+
52
+ prompt = (
53
+ f"Arabic calligraphy art of the text '{text}', "
54
+ f"rendered in {style_desc}, "
55
+ f"with {fg} on {bg}, "
56
+ f"centered composition, elegant ornamental frame, "
57
+ f"high detail, ultra sharp, 4k, luxurious Islamic art style"
58
+ )
59
+ return prompt
60
+
61
+
62
+ def generate_calligraphy(arabic_text, style, color_scheme):
63
+ """Generate Arabic calligraphy art via FLUX.1-schnell."""
64
+ prompt = build_prompt(arabic_text, style, color_scheme)
65
+ w, h = 1024, 1024
66
+
67
+ try:
68
+ image = client.text_to_image(prompt, width=w, height=h)
69
+ if not isinstance(image, Image.Image):
70
+ image = Image.open(io.BytesIO(image)) if hasattr(image, "read") else Image.open(image)
71
+ return image, "✅ Calligraphy generated!", prompt
72
+ except Exception as e:
73
+ img = Image.new("RGB", (w, h), BG_COLOR)
74
+ return img, f"❌ Error: {str(e)}", prompt
75
+
76
+
77
+ # --- UI -----------------------------------------------------------------------
78
+ CUSTOM_CSS = f"""
79
+ .gradio-container {{background-color: {BG_COLOR} !important;}}
80
+ h1, h2, h3 {{color: {ACCENT} !important;}}
81
+ """
82
+
83
+ with gr.Blocks(
84
+ title="Dispatch AI Arabic Calligraphy",
85
+ theme=gr.themes.Base(
86
+ primary_hue="cyan",
87
+ neutral_hue="slate",
88
+ font=("Inter", "system-ui", "sans-serif"),
89
+ ),
90
+ css=CUSTOM_CSS,
91
+ ) as demo:
92
+ gr.Markdown(
93
+ f"""
94
+ <div style="text-align:center;">
95
+ <h1>Dispatch AI Arabic Calligraphy</h1>
96
+ <p style="color:{ACCENT};">Generate beautiful Arabic calligraphy art with FLUX.1-schnell · Dispatch AI (FZE)</p>
97
+ </div>
98
+ """
99
+ )
100
+
101
+ with gr.Row():
102
+ with gr.Column(scale=1):
103
+ arabic_input = gr.Textbox(
104
+ label="Arabic Text",
105
+ placeholder="اكتب النص هنا",
106
+ value="بسم الله الرحمن الرحيم",
107
+ lines=2,
108
+ rtl=True,
109
+ )
110
+ style_select = gr.Dropdown(
111
+ list(STYLES.keys()),
112
+ label="Calligraphy Style",
113
+ value="Thuluth",
114
+ )
115
+ color_select = gr.Dropdown(
116
+ list(COLOR_SCHEMES.keys()),
117
+ label="Color Scheme",
118
+ value="Gold on Black",
119
+ )
120
+ generate_btn = gr.Button("✨ Generate Calligraphy", variant="primary")
121
+ with gr.Column(scale=2):
122
+ output_image = gr.Image(
123
+ label="Generated Calligraphy",
124
+ type="pil",
125
+ show_download_button=True,
126
+ )
127
+ status_box = gr.Textbox(label="Status", interactive=False)
128
+ prompt_box = gr.Textbox(label="Generated Prompt (for debugging)", interactive=False, lines=3)
129
+
130
+ gr.Markdown("### Example Arabic Prompts")
131
+ gr.Examples(
132
+ examples=[[ex] for ex in EXAMPLES_AR],
133
+ inputs=arabic_input,
134
+ label="Click an example to load it into the Arabic text box",
135
+ )
136
+
137
+ # Events
138
+ generate_btn.click(
139
+ generate_calligraphy,
140
+ inputs=[arabic_input, style_select, color_select],
141
+ outputs=[output_image, status_box, prompt_box],
142
+ )
143
+
144
+ gr.Markdown(
145
+ """
146
+ <div style="text-align:center; opacity:0.6; padding-top:20px;">
147
+ <small>Dispatch AI (FZE) · UAE · Model: FLUX.1-schnell · Arabic Calligraphy Engine</small>
148
+ </div>
149
+ """
150
+ )
151
+
152
+ if __name__ == "__main__":
153
+ demo.launch()