Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -13,7 +13,7 @@ except ImportError:
|
|
13 |
|
14 |
from transformers import pipeline, AutoTokenizer, AutoModelForCausalLM
|
15 |
|
16 |
-
# CSS for custom styling
|
17 |
custom_css = """
|
18 |
.gradio-container {
|
19 |
background: linear-gradient(to bottom right, #1a1a2e, #16213e);
|
@@ -32,9 +32,12 @@ footer {
|
|
32 |
background-color: #16213e !important;
|
33 |
border-radius: 10px !important;
|
34 |
padding: 15px !important;
|
35 |
-
font-family: '
|
36 |
-
line-height: 1.
|
37 |
color: #ffffff !important;
|
|
|
|
|
|
|
38 |
}
|
39 |
.example-btn button {
|
40 |
background-color: #e94560 !important;
|
@@ -56,6 +59,12 @@ footer {
|
|
56 |
font-size: 1.2em !important;
|
57 |
margin-top: 5px;
|
58 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
59 |
"""
|
60 |
|
61 |
# Load the model and tokenizer
|
@@ -70,44 +79,44 @@ model.to("cuda")
|
|
70 |
# Create a pipeline
|
71 |
generator = pipeline("text-generation", model=model, tokenizer=tokenizer, device=0)
|
72 |
|
73 |
-
# Example prompts for the user to try
|
74 |
example_prompts = [
|
75 |
-
"
|
76 |
-
"
|
77 |
-
"
|
78 |
-
"
|
79 |
-
"
|
80 |
]
|
81 |
|
82 |
-
# Story genres for the dropdown
|
83 |
story_genres = [
|
84 |
-
"
|
85 |
-
"
|
86 |
-
"
|
87 |
-
"
|
88 |
-
"
|
89 |
-
"
|
90 |
-
"
|
91 |
-
"
|
92 |
-
"
|
93 |
-
"
|
94 |
]
|
95 |
|
96 |
# Decorate the function that needs GPU with @spaces.GPU
|
97 |
@spaces.GPU
|
98 |
-
def generate_story(prompt, max_length=300, temperature=0.7, top_p=0.9, genre="
|
99 |
"""
|
100 |
Generate a story based on the user's prompt and specified parameters.
|
101 |
GPU is allocated only while this function is running in ZeroGPU environments.
|
102 |
"""
|
103 |
-
# Enhance the prompt with the genre and creativity instructions
|
104 |
creativity_map = {
|
105 |
-
"
|
106 |
-
"
|
107 |
-
"
|
108 |
}
|
109 |
|
110 |
-
enhanced_prompt = f"{creativity_map.get(creativity_level, '
|
111 |
|
112 |
# Generate the story
|
113 |
response = generator(
|
@@ -126,7 +135,7 @@ def generate_story(prompt, max_length=300, temperature=0.7, top_p=0.9, genre="Ad
|
|
126 |
else:
|
127 |
formatted_story = generated_text
|
128 |
|
129 |
-
# Return the
|
130 |
return formatted_story
|
131 |
|
132 |
# Function to get a random example prompt
|
@@ -135,51 +144,52 @@ def get_random_example():
|
|
135 |
|
136 |
# Function to update the UI based on creativity level
|
137 |
def update_creativity_params(creativity):
|
138 |
-
if creativity == "
|
139 |
return 0.5, 0.85
|
140 |
-
elif creativity == "
|
141 |
return 0.7, 0.9
|
142 |
-
elif creativity == "
|
143 |
return 0.9, 0.95
|
144 |
return 0.7, 0.9
|
145 |
|
146 |
# Main function for Gradio interface
|
147 |
def create_interface():
|
148 |
with gr.Blocks(css=custom_css) as demo:
|
149 |
-
# Title and subtitle
|
150 |
-
gr.HTML("<h1 class='title'>✨
|
151 |
-
gr.HTML("<p class='subtitle'
|
152 |
|
153 |
# Input section
|
154 |
with gr.Row():
|
155 |
with gr.Column():
|
156 |
gr.HTML("<div class='container'>")
|
157 |
prompt = gr.Textbox(
|
158 |
-
placeholder="
|
159 |
-
label="
|
160 |
-
lines=5
|
|
|
161 |
)
|
162 |
|
163 |
with gr.Row():
|
164 |
genre = gr.Dropdown(
|
165 |
choices=story_genres,
|
166 |
-
value="
|
167 |
-
label="
|
168 |
)
|
169 |
creativity = gr.Radio(
|
170 |
-
choices=["
|
171 |
-
value="
|
172 |
-
label="
|
173 |
)
|
174 |
|
175 |
-
with gr.Accordion("
|
176 |
with gr.Row():
|
177 |
temperature = gr.Slider(
|
178 |
minimum=0.1,
|
179 |
maximum=1.0,
|
180 |
step=0.05,
|
181 |
value=0.7,
|
182 |
-
label="
|
183 |
)
|
184 |
top_p = gr.Slider(
|
185 |
minimum=0.5,
|
@@ -194,15 +204,15 @@ def create_interface():
|
|
194 |
maximum=1500,
|
195 |
step=50,
|
196 |
value=500,
|
197 |
-
label="
|
198 |
)
|
199 |
|
200 |
-
gr.HTML("<div class='info-text'><p>✨ <strong
|
201 |
|
202 |
with gr.Row():
|
203 |
-
submit_btn = gr.Button("
|
204 |
with gr.Column(elem_classes=["example-btn"]):
|
205 |
-
example_btn = gr.Button("
|
206 |
gr.HTML("</div>") # Close container div
|
207 |
|
208 |
# Output section
|
@@ -210,12 +220,12 @@ def create_interface():
|
|
210 |
with gr.Column():
|
211 |
gr.HTML("<div class='container'>")
|
212 |
output = gr.Textbox(
|
213 |
-
label="
|
214 |
lines=12,
|
215 |
elem_classes=["story-output"]
|
216 |
)
|
217 |
|
218 |
-
gr.HTML("<div class='info-text' style='text-align: center; margin-top: 10px;'><p>💡
|
219 |
gr.HTML("</div>") # Close container div
|
220 |
|
221 |
# Set up the submit action
|
@@ -238,7 +248,7 @@ def create_interface():
|
|
238 |
outputs=[temperature, top_p]
|
239 |
)
|
240 |
|
241 |
-
gr.HTML("<div style='text-align: center; margin-top: 20px; color: #cccccc;'><p
|
242 |
|
243 |
return demo
|
244 |
|
|
|
13 |
|
14 |
from transformers import pipeline, AutoTokenizer, AutoModelForCausalLM
|
15 |
|
16 |
+
# CSS for custom styling with RTL support
|
17 |
custom_css = """
|
18 |
.gradio-container {
|
19 |
background: linear-gradient(to bottom right, #1a1a2e, #16213e);
|
|
|
32 |
background-color: #16213e !important;
|
33 |
border-radius: 10px !important;
|
34 |
padding: 15px !important;
|
35 |
+
font-family: 'Arial', sans-serif !important;
|
36 |
+
line-height: 1.8 !important;
|
37 |
color: #ffffff !important;
|
38 |
+
direction: rtl !important; /* Right-to-left for Arabic */
|
39 |
+
text-align: right !important;
|
40 |
+
font-size: 16px !important;
|
41 |
}
|
42 |
.example-btn button {
|
43 |
background-color: #e94560 !important;
|
|
|
59 |
font-size: 1.2em !important;
|
60 |
margin-top: 5px;
|
61 |
}
|
62 |
+
/* RTL support for prompt input */
|
63 |
+
.rtl-text textarea {
|
64 |
+
direction: rtl !important;
|
65 |
+
text-align: right !important;
|
66 |
+
font-size: 16px !important;
|
67 |
+
}
|
68 |
"""
|
69 |
|
70 |
# Load the model and tokenizer
|
|
|
79 |
# Create a pipeline
|
80 |
generator = pipeline("text-generation", model=model, tokenizer=tokenizer, device=0)
|
81 |
|
82 |
+
# Example prompts for the user to try (in Arabic)
|
83 |
example_prompts = [
|
84 |
+
"في عالم حيث يمكن مشاركة الأحلام، يكتشف محقق شاب أن قاتلًا متسلسلًا يستخدم الأحلام لاصطياد ضحاياه...",
|
85 |
+
"يكتشف شقيقان خريطة قديمة في علية جدتهما تقودهما إلى عالم سحري مخفي في الغابة خلف منزلهما...",
|
86 |
+
"طباخ لديه القدرة على تذوق المشاعر في الطعام يُحضّر وجبة لزبون غامض يبدو أنه بلا مشاعر على الإطلاق...",
|
87 |
+
"بعد حدث عالمي تسبب في توقف جميع التكنولوجيا عن العمل، تكتشف مجموعة من المراهقين أن لديهم قدرات جديدة غريبة...",
|
88 |
+
"يصل مسافر عبر الزمن إلى يومنا الحاضر مع تحذير عاجل، لكن لا أحد يصدقه لأن...",
|
89 |
]
|
90 |
|
91 |
+
# Story genres for the dropdown (in Arabic)
|
92 |
story_genres = [
|
93 |
+
"مغامرة",
|
94 |
+
"خيال",
|
95 |
+
"خيال علمي",
|
96 |
+
"غموض",
|
97 |
+
"رومانسية",
|
98 |
+
"رعب",
|
99 |
+
"كوميديا",
|
100 |
+
"دراما",
|
101 |
+
"حكاية خيالية",
|
102 |
+
"مستقبل قاتم"
|
103 |
]
|
104 |
|
105 |
# Decorate the function that needs GPU with @spaces.GPU
|
106 |
@spaces.GPU
|
107 |
+
def generate_story(prompt, max_length=300, temperature=0.7, top_p=0.9, genre="مغامرة", creativity_level="متوسط"):
|
108 |
"""
|
109 |
Generate a story based on the user's prompt and specified parameters.
|
110 |
GPU is allocated only while this function is running in ZeroGPU environments.
|
111 |
"""
|
112 |
+
# Enhance the prompt with the genre and creativity instructions (in Arabic)
|
113 |
creativity_map = {
|
114 |
+
"تقليدي": "اكتب قصة تقليدية ومباشرة من نوع",
|
115 |
+
"متوسط": "اكتب قصة إبداعية وجذابة من نوع",
|
116 |
+
"إبداعي": "اكتب قصة خيالية وفريدة من نوع"
|
117 |
}
|
118 |
|
119 |
+
enhanced_prompt = f"{creativity_map.get(creativity_level, 'اكتب قصة جذابة من نوع')} {genre}: {prompt}"
|
120 |
|
121 |
# Generate the story
|
122 |
response = generator(
|
|
|
135 |
else:
|
136 |
formatted_story = generated_text
|
137 |
|
138 |
+
# Return the generated story
|
139 |
return formatted_story
|
140 |
|
141 |
# Function to get a random example prompt
|
|
|
144 |
|
145 |
# Function to update the UI based on creativity level
|
146 |
def update_creativity_params(creativity):
|
147 |
+
if creativity == "تقليدي":
|
148 |
return 0.5, 0.85
|
149 |
+
elif creativity == "متوسط":
|
150 |
return 0.7, 0.9
|
151 |
+
elif creativity == "إبداعي":
|
152 |
return 0.9, 0.95
|
153 |
return 0.7, 0.9
|
154 |
|
155 |
# Main function for Gradio interface
|
156 |
def create_interface():
|
157 |
with gr.Blocks(css=custom_css) as demo:
|
158 |
+
# Title and subtitle (in Arabic)
|
159 |
+
gr.HTML("<h1 class='title'>✨ مولد القصص الذكي ✨</h1>")
|
160 |
+
gr.HTML("<p class='subtitle'>أطلق العنان لخيالك مع سرد القصص بمساعدة الذكاء الاصطناعي</p>")
|
161 |
|
162 |
# Input section
|
163 |
with gr.Row():
|
164 |
with gr.Column():
|
165 |
gr.HTML("<div class='container'>")
|
166 |
prompt = gr.Textbox(
|
167 |
+
placeholder="أدخل بداية قصتك هنا...",
|
168 |
+
label="بداية القصة",
|
169 |
+
lines=5,
|
170 |
+
elem_classes=["rtl-text"] # Add RTL class
|
171 |
)
|
172 |
|
173 |
with gr.Row():
|
174 |
genre = gr.Dropdown(
|
175 |
choices=story_genres,
|
176 |
+
value="مغامرة",
|
177 |
+
label="نوع القصة"
|
178 |
)
|
179 |
creativity = gr.Radio(
|
180 |
+
choices=["تقليدي", "متوسط", "إبداعي"],
|
181 |
+
value="متوسط",
|
182 |
+
label="مستوى الإبداع"
|
183 |
)
|
184 |
|
185 |
+
with gr.Accordion("إعدادات متقدمة", open=False):
|
186 |
with gr.Row():
|
187 |
temperature = gr.Slider(
|
188 |
minimum=0.1,
|
189 |
maximum=1.0,
|
190 |
step=0.05,
|
191 |
value=0.7,
|
192 |
+
label="درجة الحرارة"
|
193 |
)
|
194 |
top_p = gr.Slider(
|
195 |
minimum=0.5,
|
|
|
204 |
maximum=1500,
|
205 |
step=50,
|
206 |
value=500,
|
207 |
+
label="الحد الأقصى لطول القصة"
|
208 |
)
|
209 |
|
210 |
+
gr.HTML("<div class='info-text' dir='rtl'><p>✨ <strong>نصائح لبدايات قصص رائعة:</strong> كن محددًا، أضف شخصيات وإعدادات وصراعات.</p></div>")
|
211 |
|
212 |
with gr.Row():
|
213 |
+
submit_btn = gr.Button("توليد القصة", variant="primary")
|
214 |
with gr.Column(elem_classes=["example-btn"]):
|
215 |
+
example_btn = gr.Button("جرب مثالًا")
|
216 |
gr.HTML("</div>") # Close container div
|
217 |
|
218 |
# Output section
|
|
|
220 |
with gr.Column():
|
221 |
gr.HTML("<div class='container'>")
|
222 |
output = gr.Textbox(
|
223 |
+
label="قصتك",
|
224 |
lines=12,
|
225 |
elem_classes=["story-output"]
|
226 |
)
|
227 |
|
228 |
+
gr.HTML("<div class='info-text' style='text-align: center; margin-top: 10px;' dir='rtl'><p>💡 كل قصة يتم إنشاؤها بشكل فريد وقد تختلف في الأسلوب والمحتوى.</p></div>")
|
229 |
gr.HTML("</div>") # Close container div
|
230 |
|
231 |
# Set up the submit action
|
|
|
248 |
outputs=[temperature, top_p]
|
249 |
)
|
250 |
|
251 |
+
gr.HTML("<div style='text-align: center; margin-top: 20px; color: #cccccc;' dir='rtl'><p>مدعوم بواسطة ALLaM-7B-Instruct • تم إنشاؤه باستخدام Gradio</p></div>")
|
252 |
|
253 |
return demo
|
254 |
|