Spaces:
Build error
Build error
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
|
|
@@ -13,7 +14,7 @@ def load_model_artifacts():
|
|
| 13 |
"""Load model and tokenizer artifacts for Arabic text generation."""
|
| 14 |
global model, tokenizer, max_len_seq
|
| 15 |
|
| 16 |
-
# Paths for files in
|
| 17 |
model_path = 'model.h5'
|
| 18 |
tokenizer_path = 'tokenizer.json'
|
| 19 |
config_path = 'config.json'
|
|
@@ -53,90 +54,33 @@ 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 |
-
"description": """Use AI to generate Arabic text!
|
| 71 |
-
Enter a seed text and choose the number of words to generate.
|
| 72 |
-
The text will be generated in Arabic based on the seed text you provide.""",
|
| 73 |
-
"input_label": "Enter your seed text",
|
| 74 |
-
"output_label": "Generated Text",
|
| 75 |
-
"num_words_label": "Number of Words to Generate",
|
| 76 |
-
"placeholder": "Start your text here...",
|
| 77 |
-
}
|
| 78 |
-
}
|
| 79 |
-
|
| 80 |
-
def create_interface(language="ar"):
|
| 81 |
-
"""Create a Gradio interface based on the selected language."""
|
| 82 |
-
lang_texts = texts[language]
|
| 83 |
-
rtl = "rtl" if language == "ar" else "ltr"
|
| 84 |
-
alignment = "right" if language == "ar" else "left"
|
| 85 |
-
|
| 86 |
-
return gr.Interface(
|
| 87 |
-
fn=generate_arabic_text,
|
| 88 |
-
inputs=[
|
| 89 |
-
gr.Textbox(
|
| 90 |
-
label=lang_texts["input_label"],
|
| 91 |
-
placeholder=lang_texts["placeholder"],
|
| 92 |
-
value="اه ماشي" if language == "ar" else "Hello",
|
| 93 |
-
elem_id="input-textbox"
|
| 94 |
-
),
|
| 95 |
-
gr.Slider(
|
| 96 |
-
minimum=1,
|
| 97 |
-
maximum=50,
|
| 98 |
-
value=10,
|
| 99 |
-
step=1,
|
| 100 |
-
label=lang_texts["num_words_label"]
|
| 101 |
-
)
|
| 102 |
-
],
|
| 103 |
-
outputs=gr.Textbox(
|
| 104 |
-
label=lang_texts["output_label"],
|
| 105 |
-
elem_id="output-textbox"
|
| 106 |
),
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
#output-textbox textarea {{
|
| 119 |
-
text-align: {alignment};
|
| 120 |
-
direction: {rtl};
|
| 121 |
-
font-family: 'Cairo', sans-serif;
|
| 122 |
-
font-size: 18px;
|
| 123 |
-
}}
|
| 124 |
-
|
| 125 |
-
body {{
|
| 126 |
-
font-family: 'Cairo', sans-serif;
|
| 127 |
-
background: linear-gradient(to right, #4facfe, #00f2fe);
|
| 128 |
-
}}
|
| 129 |
-
"""
|
| 130 |
-
)
|
| 131 |
-
|
| 132 |
-
# Language selection interface
|
| 133 |
-
def toggle_language(lang):
|
| 134 |
-
global iface
|
| 135 |
-
iface = create_interface(language=lang)
|
| 136 |
-
iface.launch()
|
| 137 |
-
|
| 138 |
-
# Default to Arabic interface
|
| 139 |
-
iface = create_interface(language="ar")
|
| 140 |
|
|
|
|
| 141 |
if __name__ == "__main__":
|
| 142 |
-
iface.launch()
|
|
|
|
| 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
|
|
|
|
| 14 |
"""Load model and tokenizer artifacts for Arabic text generation."""
|
| 15 |
global model, tokenizer, max_len_seq
|
| 16 |
|
| 17 |
+
# Paths for files in Kaggle
|
| 18 |
model_path = 'model.h5'
|
| 19 |
tokenizer_path = 'tokenizer.json'
|
| 20 |
config_path = 'config.json'
|
|
|
|
| 54 |
except Exception as e:
|
| 55 |
return f"Error generating text: {str(e)}"
|
| 56 |
|
| 57 |
+
# Create the Gradio interface
|
| 58 |
+
iface = gr.Interface(
|
| 59 |
+
fn=generate_arabic_text,
|
| 60 |
+
outputs=gr.Textbox(label="النص المُنتج"),
|
| 61 |
+
title="مولد نصوص بالعربية | Arbic text Generator with Hugging Face",
|
| 62 |
+
description="""استخدم الذكاء الاصطناعي لتوليد نصوص باللغة العربية!
|
| 63 |
+
أدخل نصاً أولياً واختر عدد الكلمات التي تريد توليدها.
|
| 64 |
+
سيتم توليد النص باللغة العربية بناءً على النص الأولي الذي أدخلته.""",
|
| 65 |
+
|
| 66 |
+
inputs=[
|
| 67 |
+
gr.Textbox(
|
| 68 |
+
label="أدخل النص الأولي الخاص بك",
|
| 69 |
+
placeholder="ابدأ النص هنا...",
|
| 70 |
+
value="اه ماشي"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
),
|
| 72 |
+
gr.Slider(
|
| 73 |
+
minimum=1,
|
| 74 |
+
maximum=50,
|
| 75 |
+
value=10,
|
| 76 |
+
step=1,
|
| 77 |
+
label="عدد الكلمات المراد توليدها | Num of words"
|
| 78 |
+
)
|
| 79 |
+
],
|
| 80 |
+
|
| 81 |
+
theme=gr.themes.Base()
|
| 82 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
|
| 84 |
+
# Launch the app
|
| 85 |
if __name__ == "__main__":
|
| 86 |
+
iface.launch()
|