Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -60,14 +60,30 @@ def load_models():
|
|
| 60 |
print(f"Error loading model: {str(e)}")
|
| 61 |
return None, None
|
| 62 |
|
| 63 |
-
#
|
| 64 |
def clean_response(text):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
text = re.sub(r'[-–—]\s*موقع\s+[^\n]+', '', text)
|
| 66 |
text = re.sub(r'[-–—]\s*[^\n]*المصطبه[^\n]*', '', text)
|
| 67 |
-
|
|
|
|
| 68 |
text = re.sub(r'من هو [^؟?]+\؟', '', text)
|
|
|
|
| 69 |
text = re.sub(r'ما هو [^؟?]+\؟', '', text)
|
|
|
|
|
|
|
|
|
|
| 70 |
text = re.sub(r'\s+', ' ', text).strip()
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
return text
|
| 72 |
|
| 73 |
# Generate text function with GPU access
|
|
@@ -81,8 +97,8 @@ def generate_text(prompt):
|
|
| 81 |
if tokenizer is None or model is None:
|
| 82 |
return "خطأ في تحميل النموذج."
|
| 83 |
|
| 84 |
-
#
|
| 85 |
-
full_prompt =
|
| 86 |
|
| 87 |
# Tokenize and generate
|
| 88 |
inputs = tokenizer(full_prompt, return_tensors="pt").to(model.device)
|
|
@@ -96,7 +112,10 @@ def generate_text(prompt):
|
|
| 96 |
|
| 97 |
# Get only new content
|
| 98 |
generated_text = tokenizer.decode(outputs[0][inputs.input_ids.shape[1]:], skip_special_tokens=True)
|
|
|
|
|
|
|
| 99 |
final_text = clean_response(generated_text)
|
|
|
|
| 100 |
return final_text
|
| 101 |
|
| 102 |
except Exception as e:
|
|
@@ -115,112 +134,128 @@ def set_example3():
|
|
| 115 |
def set_example4():
|
| 116 |
return "ماهو شهر رمضان؟"
|
| 117 |
|
| 118 |
-
# Create custom CSS that fixes text visibility
|
| 119 |
custom_css = """
|
| 120 |
-
/*
|
| 121 |
-
* {
|
| 122 |
-
box-sizing: border-box;
|
| 123 |
-
margin: 0;
|
| 124 |
-
padding: 0;
|
| 125 |
-
}
|
| 126 |
-
|
| 127 |
-
/* Basic colors and fonts */
|
| 128 |
:root {
|
| 129 |
-
--border-color: #2c3e50;
|
| 130 |
-
--background-color: #f8f9fa;
|
| 131 |
--primary-color: #1F4287;
|
| 132 |
-
--
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 133 |
}
|
| 134 |
|
| 135 |
-
/*
|
| 136 |
-
|
| 137 |
-
font-family: '
|
| 138 |
-
|
| 139 |
-
|
|
|
|
|
|
|
| 140 |
}
|
| 141 |
|
| 142 |
-
/*
|
| 143 |
h1, h2, h3 {
|
| 144 |
-
text-align: center;
|
| 145 |
-
margin-bottom: 1rem;
|
| 146 |
color: var(--primary-color);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 147 |
}
|
| 148 |
|
| 149 |
-
/*
|
| 150 |
-
|
|
|
|
| 151 |
color: black !important;
|
| 152 |
background-color: white !important;
|
| 153 |
-
|
| 154 |
padding: 10px !important;
|
| 155 |
-
|
|
|
|
| 156 |
border-radius: 6px !important;
|
| 157 |
width: 100% !important;
|
| 158 |
direction: rtl !important;
|
|
|
|
| 159 |
}
|
| 160 |
|
| 161 |
-
|
| 162 |
-
|
|
|
|
| 163 |
opacity: 1 !important;
|
| 164 |
}
|
| 165 |
|
| 166 |
-
/*
|
| 167 |
button {
|
| 168 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 169 |
color: white !important;
|
| 170 |
border: none !important;
|
| 171 |
-
padding: 8px 16px !important;
|
| 172 |
-
border-radius: 4px !important;
|
| 173 |
-
cursor: pointer !important;
|
| 174 |
-
margin: 5px !important;
|
| 175 |
}
|
| 176 |
|
| 177 |
-
button
|
| 178 |
-
|
|
|
|
|
|
|
| 179 |
}
|
| 180 |
|
| 181 |
-
/* Example
|
| 182 |
.example-btn {
|
| 183 |
-
background-color: #
|
|
|
|
| 184 |
color: var(--primary-color) !important;
|
| 185 |
-
|
|
|
|
|
|
|
|
|
|
| 186 |
}
|
| 187 |
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
margin-bottom: 1rem !important;
|
| 192 |
-
width: 100% !important;
|
| 193 |
}
|
| 194 |
|
| 195 |
-
/*
|
| 196 |
-
.
|
| 197 |
-
|
| 198 |
-
|
|
|
|
| 199 |
}
|
| 200 |
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
min-height: 100px !important;
|
| 206 |
}
|
| 207 |
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
background-color: white !important;
|
| 211 |
-
min-height: 200px !important;
|
| 212 |
}
|
| 213 |
|
| 214 |
-
/*
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
|
|
|
| 218 |
}
|
| 219 |
"""
|
| 220 |
|
| 221 |
-
# Create a
|
| 222 |
-
with gr.Blocks(css=custom_css) as demo:
|
| 223 |
-
gr.Markdown("# ⭐ نموذج
|
| 224 |
|
| 225 |
with gr.Row():
|
| 226 |
with gr.Column():
|
|
@@ -228,12 +263,11 @@ with gr.Blocks(css=custom_css) as demo:
|
|
| 228 |
label="النص الإدخال | Input Prompt",
|
| 229 |
placeholder="أدخل سؤالك باللغة العربية هنا...",
|
| 230 |
lines=4,
|
| 231 |
-
elem_id="input-
|
| 232 |
-
value=""
|
| 233 |
)
|
| 234 |
|
| 235 |
-
# Examples in a grid layout
|
| 236 |
gr.Markdown("### أمثلة سريعة | Quick Examples")
|
|
|
|
| 237 |
with gr.Row():
|
| 238 |
ex1 = gr.Button('من كتب قصيدة "على قدر أهل العزم تأتي العزائم"؟', elem_classes=["example-btn"])
|
| 239 |
ex2 = gr.Button("ما هي عاصمة السعودية؟", elem_classes=["example-btn"])
|
|
@@ -242,16 +276,15 @@ with gr.Blocks(css=custom_css) as demo:
|
|
| 242 |
ex3 = gr.Button("اشرح مفهوم الذكاء الاصطناعي", elem_classes=["example-btn"])
|
| 243 |
ex4 = gr.Button("ماهو شهر رمضان؟", elem_classes=["example-btn"])
|
| 244 |
|
| 245 |
-
# Submit and clear buttons
|
| 246 |
with gr.Row():
|
| 247 |
-
submit_btn = gr.Button("توليد النص | Generate")
|
| 248 |
-
clear_btn = gr.Button("مسح | Clear")
|
| 249 |
|
| 250 |
with gr.Column():
|
| 251 |
output_text = gr.Textbox(
|
| 252 |
label="النص المولد | Generated Text",
|
| 253 |
lines=10,
|
| 254 |
-
elem_id="output-
|
| 255 |
)
|
| 256 |
|
| 257 |
# Set up the event handlers
|
|
|
|
| 60 |
print(f"Error loading model: {str(e)}")
|
| 61 |
return None, None
|
| 62 |
|
| 63 |
+
# Enhanced clean response function with better metadata cleaning
|
| 64 |
def clean_response(text):
|
| 65 |
+
# Step 1: Aggressively remove the common metadata pattern at the beginning
|
| 66 |
+
text = re.sub(r'^\s*[\?؟]\s*موضوع:.*?التالي:\s*[^؟?]*[؟?]\s*[^\n]*\d{4},\s*\d{1,2}:\d{2}\s*[apm]+\s*', '', text)
|
| 67 |
+
|
| 68 |
+
# Step 2: Remove date and timestamp patterns
|
| 69 |
+
text = re.sub(r'\d{1,2}\s+[^ ]+\s+\d{4}\s*[-,]\s*\d{1,2}:\d{2}\s*[صمaApP][مmMnN]?', '', text)
|
| 70 |
+
|
| 71 |
+
# Step 3: Remove website references
|
| 72 |
text = re.sub(r'[-–—]\s*موقع\s+[^\n]+', '', text)
|
| 73 |
text = re.sub(r'[-–—]\s*[^\n]*المصطبه[^\n]*', '', text)
|
| 74 |
+
|
| 75 |
+
# Step 4: Remove unrelated questions that might appear
|
| 76 |
text = re.sub(r'من هو [^؟?]+\؟', '', text)
|
| 77 |
+
text = re.sub(r'من هي [^؟?]+\؟', '', text)
|
| 78 |
text = re.sub(r'ما هو [^؟?]+\؟', '', text)
|
| 79 |
+
text = re.sub(r'ما هي [^؟?]+\؟', '', text)
|
| 80 |
+
|
| 81 |
+
# Step 5: Clean up format and spacing
|
| 82 |
text = re.sub(r'\s+', ' ', text).strip()
|
| 83 |
+
|
| 84 |
+
# If text begins with a colon or similar punctuation, clean it
|
| 85 |
+
text = re.sub(r'^[:.،,؛;-]+\s*', '', text)
|
| 86 |
+
|
| 87 |
return text
|
| 88 |
|
| 89 |
# Generate text function with GPU access
|
|
|
|
| 97 |
if tokenizer is None or model is None:
|
| 98 |
return "خطأ في تحميل النموذج."
|
| 99 |
|
| 100 |
+
# Using minimal prompt without system context
|
| 101 |
+
full_prompt = prompt
|
| 102 |
|
| 103 |
# Tokenize and generate
|
| 104 |
inputs = tokenizer(full_prompt, return_tensors="pt").to(model.device)
|
|
|
|
| 112 |
|
| 113 |
# Get only new content
|
| 114 |
generated_text = tokenizer.decode(outputs[0][inputs.input_ids.shape[1]:], skip_special_tokens=True)
|
| 115 |
+
|
| 116 |
+
# Apply enhanced cleaning to remove metadata
|
| 117 |
final_text = clean_response(generated_text)
|
| 118 |
+
|
| 119 |
return final_text
|
| 120 |
|
| 121 |
except Exception as e:
|
|
|
|
| 134 |
def set_example4():
|
| 135 |
return "ماهو شهر رمضان؟"
|
| 136 |
|
| 137 |
+
# Create custom CSS that fixes text visibility while matching the current look
|
| 138 |
custom_css = """
|
| 139 |
+
/* Enhanced styles for Arabic Cohere model interface */
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 140 |
:root {
|
|
|
|
|
|
|
| 141 |
--primary-color: #1F4287;
|
| 142 |
+
--secondary-color: #278EA5;
|
| 143 |
+
--bg-color: #f9fafb;
|
| 144 |
+
--border-color: #d1d5db;
|
| 145 |
+
--btn-primary: #2C5282;
|
| 146 |
+
--btn-secondary: #E2E8F0;
|
| 147 |
+
--text-color: #000000;
|
| 148 |
}
|
| 149 |
|
| 150 |
+
/* Base styles */
|
| 151 |
+
body, html {
|
| 152 |
+
font-family: 'Cairo', 'Segoe UI', Tahoma, sans-serif;
|
| 153 |
+
margin: 0;
|
| 154 |
+
padding: 0;
|
| 155 |
+
background-color: var(--bg-color);
|
| 156 |
+
overflow-x: hidden;
|
| 157 |
}
|
| 158 |
|
| 159 |
+
/* Typography */
|
| 160 |
h1, h2, h3 {
|
|
|
|
|
|
|
| 161 |
color: var(--primary-color);
|
| 162 |
+
text-align: center;
|
| 163 |
+
}
|
| 164 |
+
|
| 165 |
+
/* Fix input and output containers */
|
| 166 |
+
.input-container, .output-container {
|
| 167 |
+
border: 1px solid var(--border-color);
|
| 168 |
+
border-radius: 8px;
|
| 169 |
+
padding: 10px;
|
| 170 |
+
margin-bottom: 15px;
|
| 171 |
+
background-color: white;
|
| 172 |
}
|
| 173 |
|
| 174 |
+
/* CRITICAL FIX: Make sure text is visible in textboxes */
|
| 175 |
+
textarea, .output-text {
|
| 176 |
+
font-family: 'Cairo', 'Segoe UI', Tahoma, sans-serif !important;
|
| 177 |
color: black !important;
|
| 178 |
background-color: white !important;
|
| 179 |
+
border: 1px solid #d1d5db !important;
|
| 180 |
padding: 10px !important;
|
| 181 |
+
font-size: 16px !important;
|
| 182 |
+
line-height: 1.5 !important;
|
| 183 |
border-radius: 6px !important;
|
| 184 |
width: 100% !important;
|
| 185 |
direction: rtl !important;
|
| 186 |
+
min-height: 80px !important;
|
| 187 |
}
|
| 188 |
|
| 189 |
+
/* Ensure text and placeholder are visible */
|
| 190 |
+
textarea::placeholder {
|
| 191 |
+
color: #9ca3af !important;
|
| 192 |
opacity: 1 !important;
|
| 193 |
}
|
| 194 |
|
| 195 |
+
/* Button styling to match current design */
|
| 196 |
button {
|
| 197 |
+
border-radius: 6px !important;
|
| 198 |
+
padding: 8px 16px !important;
|
| 199 |
+
font-weight: 600 !important;
|
| 200 |
+
transition: all 0.2s !important;
|
| 201 |
+
}
|
| 202 |
+
|
| 203 |
+
button.primary {
|
| 204 |
+
background-color: var(--btn-primary) !important;
|
| 205 |
color: white !important;
|
| 206 |
border: none !important;
|
|
|
|
|
|
|
|
|
|
|
|
|
| 207 |
}
|
| 208 |
|
| 209 |
+
button.secondary {
|
| 210 |
+
background-color: var(--btn-secondary) !important;
|
| 211 |
+
color: var(--primary-color) !important;
|
| 212 |
+
border: 1px solid var(--border-color) !important;
|
| 213 |
}
|
| 214 |
|
| 215 |
+
/* Example buttons styling */
|
| 216 |
.example-btn {
|
| 217 |
+
background-color: #f8fafc !important;
|
| 218 |
+
border: 1px solid #cbd5e1 !important;
|
| 219 |
color: var(--primary-color) !important;
|
| 220 |
+
padding: 8px 12px !important;
|
| 221 |
+
border-radius: 6px !important;
|
| 222 |
+
margin: 4px !important;
|
| 223 |
+
font-size: 14px !important;
|
| 224 |
}
|
| 225 |
|
| 226 |
+
.example-btn:hover {
|
| 227 |
+
background-color: #f1f5f9 !important;
|
| 228 |
+
border-color: #94a3b8 !important;
|
|
|
|
|
|
|
| 229 |
}
|
| 230 |
|
| 231 |
+
/* Layout containers */
|
| 232 |
+
.container {
|
| 233 |
+
max-width: 1200px;
|
| 234 |
+
margin: 0 auto;
|
| 235 |
+
padding: 20px;
|
| 236 |
}
|
| 237 |
|
| 238 |
+
.row {
|
| 239 |
+
display: flex;
|
| 240 |
+
gap: 20px;
|
| 241 |
+
margin-bottom: 15px;
|
|
|
|
| 242 |
}
|
| 243 |
|
| 244 |
+
.col {
|
| 245 |
+
flex: 1;
|
|
|
|
|
|
|
| 246 |
}
|
| 247 |
|
| 248 |
+
/* Explicitly force element visibility */
|
| 249 |
+
#input-text, #output-text {
|
| 250 |
+
visibility: visible !important;
|
| 251 |
+
display: block !important;
|
| 252 |
+
opacity: 1 !important;
|
| 253 |
}
|
| 254 |
"""
|
| 255 |
|
| 256 |
+
# Create a Gradio interface that matches the current design
|
| 257 |
+
with gr.Blocks(css=custom_css, title="Cohere Arabic Model") as demo:
|
| 258 |
+
gr.Markdown("""# ⭐ نموذج أرحب للغة العربية | Command R7B Arabic Model""")
|
| 259 |
|
| 260 |
with gr.Row():
|
| 261 |
with gr.Column():
|
|
|
|
| 263 |
label="النص الإدخال | Input Prompt",
|
| 264 |
placeholder="أدخل سؤالك باللغة العربية هنا...",
|
| 265 |
lines=4,
|
| 266 |
+
elem_id="input-text"
|
|
|
|
| 267 |
)
|
| 268 |
|
|
|
|
| 269 |
gr.Markdown("### أمثلة سريعة | Quick Examples")
|
| 270 |
+
|
| 271 |
with gr.Row():
|
| 272 |
ex1 = gr.Button('من كتب قصيدة "على قدر أهل العزم تأتي العزائم"؟', elem_classes=["example-btn"])
|
| 273 |
ex2 = gr.Button("ما هي عاصمة السعودية؟", elem_classes=["example-btn"])
|
|
|
|
| 276 |
ex3 = gr.Button("اشرح مفهوم الذكاء الاصطناعي", elem_classes=["example-btn"])
|
| 277 |
ex4 = gr.Button("ماهو شهر رمضان؟", elem_classes=["example-btn"])
|
| 278 |
|
|
|
|
| 279 |
with gr.Row():
|
| 280 |
+
submit_btn = gr.Button("توليد النص | Generate", elem_classes=["primary"])
|
| 281 |
+
clear_btn = gr.Button("مسح | Clear", elem_classes=["secondary"])
|
| 282 |
|
| 283 |
with gr.Column():
|
| 284 |
output_text = gr.Textbox(
|
| 285 |
label="النص المولد | Generated Text",
|
| 286 |
lines=10,
|
| 287 |
+
elem_id="output-text"
|
| 288 |
)
|
| 289 |
|
| 290 |
# Set up the event handlers
|