Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,6 +3,7 @@ import tensorflow as tf
|
|
| 3 |
import numpy as np
|
| 4 |
from tensorflow.keras.preprocessing.sequence import pad_sequences
|
| 5 |
import json
|
|
|
|
| 6 |
|
| 7 |
# Initialize global variables
|
| 8 |
model = None
|
|
@@ -53,60 +54,106 @@ def generate_arabic_text(seed_text, num_words):
|
|
| 53 |
except Exception as e:
|
| 54 |
return f"Error generating text: {str(e)}"
|
| 55 |
|
| 56 |
-
#
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
|
|
|
|
|
|
| 74 |
),
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
|
| 110 |
# Launch the app
|
| 111 |
if __name__ == "__main__":
|
| 112 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
import numpy as np
|
| 4 |
from tensorflow.keras.preprocessing.sequence import pad_sequences
|
| 5 |
import json
|
| 6 |
+
import os
|
| 7 |
|
| 8 |
# Initialize global variables
|
| 9 |
model = None
|
|
|
|
| 54 |
except Exception as e:
|
| 55 |
return f"Error generating text: {str(e)}"
|
| 56 |
|
| 57 |
+
# UI text for both languages
|
| 58 |
+
texts = {
|
| 59 |
+
"ar": {
|
| 60 |
+
"title": "مولد نصوص بالعربية",
|
| 61 |
+
"description": """استخدم الذكاء الاصطناعي لتوليد نصوص باللغة العربية!
|
| 62 |
+
أدخل نصاً أولياً واختر عدد الكلمات التي تريد توليدها.
|
| 63 |
+
سيتم توليد النص باللغة العربية بناءً على النص الأولي الذي أدخلته.""",
|
| 64 |
+
"input_label": "أدخل النص الأولي الخاص بك",
|
| 65 |
+
"output_label": "النص المُنتج",
|
| 66 |
+
"num_words_label": "عدد الكلمات المراد توليدها",
|
| 67 |
+
"placeholder": "ابدأ النص هنا...",
|
| 68 |
+
},
|
| 69 |
+
"en": {
|
| 70 |
+
"title": "Arabic Text Generator",
|
| 71 |
+
"description": """Use AI to generate Arabic text!
|
| 72 |
+
Enter a seed text and choose the number of words to generate.
|
| 73 |
+
The text will be generated in Arabic based on the seed text you provide.""",
|
| 74 |
+
"input_label": "Enter your seed text",
|
| 75 |
+
"output_label": "Generated Text",
|
| 76 |
+
"num_words_label": "Number of Words to Generate",
|
| 77 |
+
"placeholder": "Start your text here...",
|
| 78 |
+
}
|
| 79 |
+
}
|
| 80 |
|
| 81 |
+
def get_interface(language="ar"):
|
| 82 |
+
"""Create a Gradio interface based on the selected language."""
|
| 83 |
+
lang_texts = texts[language]
|
| 84 |
+
rtl = "rtl" if language == "ar" else "ltr"
|
| 85 |
+
alignment = "right" if language == "ar" else "left"
|
| 86 |
+
|
| 87 |
+
return gr.Interface(
|
| 88 |
+
fn=generate_arabic_text,
|
| 89 |
+
outputs=gr.Textbox(
|
| 90 |
+
label=lang_texts["output_label"],
|
| 91 |
+
elem_id="output-textbox"
|
| 92 |
),
|
| 93 |
+
title=lang_texts["title"],
|
| 94 |
+
description=lang_texts["description"],
|
| 95 |
+
inputs=[
|
| 96 |
+
gr.Textbox(
|
| 97 |
+
label=lang_texts["input_label"],
|
| 98 |
+
placeholder=lang_texts["placeholder"],
|
| 99 |
+
value="اه ماشي" if language == "ar" else "Hello",
|
| 100 |
+
elem_id="input-textbox"
|
| 101 |
+
),
|
| 102 |
+
gr.Slider(
|
| 103 |
+
minimum=1,
|
| 104 |
+
maximum=50,
|
| 105 |
+
value=10,
|
| 106 |
+
step=1,
|
| 107 |
+
label=lang_texts["num_words_label"]
|
| 108 |
+
)
|
| 109 |
+
],
|
| 110 |
+
theme=gr.themes.Default(primary_hue="blue", secondary_hue="green", neutral_hue="slate"),
|
| 111 |
+
css=f"""
|
| 112 |
+
#input-textbox textarea {{
|
| 113 |
+
text-align: {alignment};
|
| 114 |
+
direction: {rtl};
|
| 115 |
+
font-family: 'Cairo', sans-serif;
|
| 116 |
+
font-size: 18px;
|
| 117 |
+
}}
|
| 118 |
+
|
| 119 |
+
#output-textbox textarea {{
|
| 120 |
+
text-align: {alignment};
|
| 121 |
+
direction: {rtl};
|
| 122 |
+
font-family: 'Cairo', sans-serif;
|
| 123 |
+
font-size: 18px;
|
| 124 |
+
}}
|
| 125 |
+
|
| 126 |
+
body {{
|
| 127 |
+
background: linear-gradient(to right, #4facfe, #00f2fe);
|
| 128 |
+
font-family: 'Cairo', sans-serif;
|
| 129 |
+
}}
|
| 130 |
+
|
| 131 |
+
.gr-button {{
|
| 132 |
+
background: #007bff;
|
| 133 |
+
color: white;
|
| 134 |
+
border-radius: 8px;
|
| 135 |
+
font-weight: bold;
|
| 136 |
+
}}
|
| 137 |
+
"""
|
| 138 |
+
)
|
| 139 |
+
|
| 140 |
+
# Language toggle function
|
| 141 |
+
def toggle_language(lang):
|
| 142 |
+
global iface
|
| 143 |
+
iface = get_interface(language=lang)
|
| 144 |
+
iface.launch(share=True)
|
| 145 |
+
|
| 146 |
+
# Initial interface
|
| 147 |
+
iface = get_interface(language="ar")
|
| 148 |
|
| 149 |
# Launch the app
|
| 150 |
if __name__ == "__main__":
|
| 151 |
+
with gr.Blocks() as demo:
|
| 152 |
+
gr.Markdown("# اختر اللغة | Choose Language")
|
| 153 |
+
gr.Row([
|
| 154 |
+
gr.Button("العربية").click(fn=lambda: toggle_language("ar")),
|
| 155 |
+
gr.Button("English").click(fn=lambda: toggle_language("en"))
|
| 156 |
+
])
|
| 157 |
+
demo.append(iface)
|
| 158 |
+
|
| 159 |
+
demo.launch()
|