fahad11182 commited on
Commit
c48e36a
·
verified ·
1 Parent(s): deca52a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -29
app.py CHANGED
@@ -4,6 +4,7 @@ from diffusers import StableDiffusionInstructPix2PixPipeline
4
  import gradio as gr
5
  from PIL import Image
6
  import random
 
7
 
8
  # Load the InstructPix2Pix model
9
  model_id = "timbrooks/instruct-pix2pix"
@@ -29,7 +30,7 @@ def change_style():
29
  seed = torch.manual_seed(torch.randint(0, 10000, (1,)).item())
30
  return f"تم تغيير النمط. المعرف الجديد: {seed}"
31
 
32
- # Dictionary to map Arabic colors to English
33
  arabic_to_english_colors = {
34
  "أبيض": "White",
35
  "أسود": "Black",
@@ -48,16 +49,31 @@ arabic_to_english_colors = {
48
  "بيج": "Beige"
49
  }
50
 
51
- # Function to translate Arabic color to English and change the wall color
52
- def change_color(image, color):
53
- # Translate Arabic color to English using the dictionary
54
- color_in_english = arabic_to_english_colors.get(color, None)
55
-
56
- # If color not found in the dictionary, return an error message
57
- if not color_in_english:
58
- return f"اللون '{color}' غير موجود في القائمة. يرجى إدخال لون صحيح."
59
 
60
- # Construct the furniture prompt in English
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61
  prompt = f"paint the walls with {color_in_english} color"
62
 
63
  # Text CFG (guidance_scale) controls how strongly the model follows the prompt
@@ -66,7 +82,7 @@ def change_color(image, color):
66
  # Image CFG: Simulated value for preserving the original image content
67
  image_cfg = 1.5
68
 
69
- # Apply the edit using InstructPix2Pix, with text CFG and image CFG influencing the guidance scale
70
  edited_image = pipe(
71
  prompt=prompt,
72
  image=image,
@@ -78,7 +94,7 @@ def change_color(image, color):
78
 
79
  return edited_image
80
 
81
- # Gradio interface for image editing in Arabic
82
  def image_interface():
83
  with gr.Blocks(css=".gradio-container {direction: rtl}") as demo_color:
84
  gr.Markdown("## تطبيق لتغيير لون الجدران")
@@ -86,15 +102,11 @@ def image_interface():
86
  # Image upload (translated to Arabic)
87
  image_input = gr.Image(type="pil", label="قم برفع صورة للغرفة")
88
 
89
- # List of common painting colors in Arabic
90
- common_colors = [
91
- "أبيض", "أسود", "أزرق", "أخضر", "أحمر", "أصفر",
92
- "رمادي", "برتقالي", "بنفسجي", "وردي", "بني",
93
- "كحلي", "زهري", "فيروزي", "بيج"
94
- ]
95
 
96
- # Dropdown for wall color (Arabic)
97
- color_input = gr.Dropdown(common_colors, label="اختر لون الجدران")
98
 
99
  # Display output image
100
  result_image = gr.Image(label="الصورة المعدلة")
@@ -103,7 +115,7 @@ def image_interface():
103
  submit_button = gr.Button("قم بتغيير لون الجدران")
104
 
105
  # Define action on button click (directly pass dropdown color input to the function)
106
- submit_button.click(fn=change_color, inputs=[image_input, color_input], outputs=result_image)
107
 
108
  return demo_color
109
 
@@ -116,10 +128,15 @@ def translate_prompt(prompt_ar):
116
  return prompt_en
117
 
118
 
119
- # General image editing function
120
- def edit_image(image, instruction_ar):
121
- # Translate Arabic instruction to English
122
- instruction_en = translate_prompt(instruction_ar)
 
 
 
 
 
123
 
124
  # Text CFG (guidance_scale) controls how strongly the model follows the prompt
125
  text_cfg = 12.0
@@ -140,16 +157,16 @@ def edit_image(image, instruction_ar):
140
  return edited_image
141
 
142
 
143
- # Gradio interface for general image editing in Arabic
144
  def general_editing_interface():
145
  with gr.Blocks(css=".gradio-container {direction: rtl}") as demo_general:
146
  gr.Markdown("## تطبيق تحرير الصور العام")
147
 
148
- # Image upload in Arabic
149
  image_input = gr.Image(type="pil", label="قم بتحميل صورة")
150
 
151
- # Textbox for instruction in Arabic
152
- instruction_input = gr.Textbox(label="أدخل التعليمات", placeholder="وصف التعديلات (مثل: 'اجعل الجو مثلج')")
153
 
154
  # Display output image
155
  result_image = gr.Image(label="الصورة المعدلة")
 
4
  import gradio as gr
5
  from PIL import Image
6
  import random
7
+ from langdetect import detect, LangDetectException
8
 
9
  # Load the InstructPix2Pix model
10
  model_id = "timbrooks/instruct-pix2pix"
 
30
  seed = torch.manual_seed(torch.randint(0, 10000, (1,)).item())
31
  return f"تم تغيير النمط. المعرف الجديد: {seed}"
32
 
33
+ # Dictionary to map Arabic to English colors
34
  arabic_to_english_colors = {
35
  "أبيض": "White",
36
  "أسود": "Black",
 
49
  "بيج": "Beige"
50
  }
51
 
52
+ # List of English colors
53
+ english_colors = [
54
+ "White", "Black", "Blue", "Green", "Red", "Yellow",
55
+ "Gray", "Orange", "Purple", "Pink", "Brown",
56
+ "Navy", "Coral", "Teal", "Beige"
57
+ ]
 
 
58
 
59
+ # Function to detect whether the text is in Arabic using langdetect
60
+ def is_arabic(text):
61
+ try:
62
+ return detect(text) == 'ar' # 'ar' is the ISO 639-1 code for Arabic
63
+ except LangDetectException:
64
+ return False # If detection fails, assume it's not Arabic
65
+
66
+ # Function to translate Arabic color to English and change the wall color
67
+ def change_color(image, arabic_color, english_color):
68
+ # If a color is chosen from the Arabic dropdown, use it, otherwise use the English dropdown
69
+ if arabic_color:
70
+ color_in_english = arabic_to_english_colors.get(arabic_color)
71
+ elif english_color:
72
+ color_in_english = english_color
73
+ else:
74
+ return "Please select a color."
75
+
76
+ # Construct the prompt to change the wall color
77
  prompt = f"paint the walls with {color_in_english} color"
78
 
79
  # Text CFG (guidance_scale) controls how strongly the model follows the prompt
 
82
  # Image CFG: Simulated value for preserving the original image content
83
  image_cfg = 1.5
84
 
85
+ # Apply the edit using InstructPix2Pix
86
  edited_image = pipe(
87
  prompt=prompt,
88
  image=image,
 
94
 
95
  return edited_image
96
 
97
+ # Gradio interface for image editing with separate Arabic and English color dropdowns
98
  def image_interface():
99
  with gr.Blocks(css=".gradio-container {direction: rtl}") as demo_color:
100
  gr.Markdown("## تطبيق لتغيير لون الجدران")
 
102
  # Image upload (translated to Arabic)
103
  image_input = gr.Image(type="pil", label="قم برفع صورة للغرفة")
104
 
105
+ # Dropdown for Arabic wall color
106
+ arabic_color_input = gr.Dropdown(list(arabic_to_english_colors.keys()), label="اختر لون الجدران (بالعربية)")
 
 
 
 
107
 
108
+ # Dropdown for English wall color
109
+ english_color_input = gr.Dropdown(english_colors, label="Choose Wall Color (English)")
110
 
111
  # Display output image
112
  result_image = gr.Image(label="الصورة المعدلة")
 
115
  submit_button = gr.Button("قم بتغيير لون الجدران")
116
 
117
  # Define action on button click (directly pass dropdown color input to the function)
118
+ submit_button.click(fn=change_color, inputs=[image_input, arabic_color_input, english_color_input], outputs=result_image)
119
 
120
  return demo_color
121
 
 
128
  return prompt_en
129
 
130
 
131
+ # General image editing function (supports both Arabic and English)
132
+ def edit_image(image, instruction):
133
+ # Detect if the instruction is in Arabic
134
+ if is_arabic(instruction):
135
+ # Translate Arabic instruction to English
136
+ instruction_en = translate_prompt(instruction)
137
+ else:
138
+ # If it's in English, use it directly
139
+ instruction_en = instruction
140
 
141
  # Text CFG (guidance_scale) controls how strongly the model follows the prompt
142
  text_cfg = 12.0
 
157
  return edited_image
158
 
159
 
160
+ # Gradio interface for general image editing in Arabic and English
161
  def general_editing_interface():
162
  with gr.Blocks(css=".gradio-container {direction: rtl}") as demo_general:
163
  gr.Markdown("## تطبيق تحرير الصور العام")
164
 
165
+ # Image upload in Arabic and English
166
  image_input = gr.Image(type="pil", label="قم بتحميل صورة")
167
 
168
+ # Textbox for instruction in Arabic or English
169
+ instruction_input = gr.Textbox(label="أدخل التعليمات", placeholder="وصف التعديلات (مثل: 'اجعل الجو مثلج') أو 'Make it snowy'")
170
 
171
  # Display output image
172
  result_image = gr.Image(label="الصورة المعدلة")