Lubna25 commited on
Commit
7c1e602
1 Parent(s): fe4e4c5

Delete app.py

Browse files
Files changed (1) hide show
  1. app.py +0 -373
app.py DELETED
@@ -1,373 +0,0 @@
1
- #Imports
2
- import gradio as gr
3
- from PIL import Image, UnidentifiedImageError
4
- from gtts import gTTS
5
- import requests
6
- import re
7
- import torch
8
- from transformers import CLIPProcessor, CLIPModel, pipeline
9
- from sentence_transformers import SentenceTransformer, util
10
- from langdetect import detect
11
- from io import BytesIO
12
- import pandas as pd
13
- import numpy as np
14
- import soundfile as sf
15
- import os
16
- import subprocess
17
-
18
- # Run the setup script to install espeak-ng
19
- subprocess.run(['bash', 'setup.sh'], check=True)
20
-
21
-
22
- # DataFrame with information about the Paintings as image url, Title, description , stroy
23
-
24
- data = {
25
- "image_url": [
26
- "https://s.turbifycdn.com/aah/gallerydirectart/vincent-van-gogh-estate-signed-limited-edition-giclee-starry-night-47.png", # Starry Night
27
- "https://cdn.mos.cms.futurecdn.net/xRqbwS4odpkSQscn3jHECh-1200-80.jpg", # Mona Lisa
28
- "https://upload.wikimedia.org/wikipedia/en/d/dd/The_Persistence_of_Memory.jpg", # The Persistence of Memory
29
- "https://static.wixstatic.com/media/1071a8_cf1930f883e043e28d03d5a26a5960ef~mv2.jpg/v1/fill/w_568,h_718,al_c,q_85,usm_0.66_1.00_0.01,enc_auto/1071a8_cf1930f883e043e28d03d5a26a5960ef~mv2.jpg", # The Scream
30
- "https://images.artbrokerage.com/artthumb/magritte_158194_1/625x559/Rene_Magritte_Le_Fils_De_lhomme_the_Son_of_Man_1973.jpg", # The Son of Man
31
- "https://www.artic.edu/iiif/2/25c31d8d-21a4-9ea1-1d73-6a2eca4dda7e/full/843,/0/default.jpg", # The Bedroom
32
- "https://images.desenio.com/zoom/17047_1.jpg", # Girl with a Pearl Earring
33
- "https://www.hastingsindependentpress.co.uk/wp-content/uploads/2021/03/Whistlers-Mother.jpg", # Whistler’s Mother
34
- "https://live.staticflickr.com/7173/6713746433_652c3d9d4e_c.jpg" # The Basket of Apples
35
- ],
36
- "Title": [
37
- "Starry Night", "Mona Lisa", "The Persistence of Memory", "The Scream",
38
- "The Son of Man", "The Bedroom",
39
- "Girl with a Pearl Earring", "Whistler’s Mother", "The Basket of Apples"
40
- ],
41
- "Description": [
42
- # Starry Night
43
- ("Starry Night by Vincent van Gogh, painted in 1889, is one of the most famous works of art in the world. "
44
- "It depicts a swirling night sky filled with stars over a small town. The painting uses vibrant colors like blue and yellow, "
45
- "with exaggerated swirling patterns that create a dreamlike, almost chaotic feeling."),
46
-
47
- # Mona Lisa
48
- ("The Mona Lisa by Leonardo da Vinci, painted between 1503 and 1506, is a portrait of a woman with a subtle, enigmatic smile. "
49
- "The use of muted colors, including soft browns, greens, and black, emphasizes the serene and mysterious nature of the subject. "
50
- "It is one of the most studied and recognized works of art in history."),
51
-
52
- # The Persistence of Memory
53
- ("The Persistence of Memory, created by Salvador Dalí in 1931, features melting clocks draped over a surreal landscape. "
54
- "The painting, primarily in soft shades of brown, blue, and yellow, explores themes of time and memory. The abstract shapes "
55
- "and dreamlike atmosphere make it one of Dalí’s most famous surrealist works."),
56
-
57
- # The Scream
58
- ("The Scream by Edvard Munch, painted in 1893, is one of the most iconic images in modern art. "
59
- "It depicts a figure standing on a bridge, clutching their face in agony, as a blood-red sky swirls behind them. "
60
- "The painting uses bold reds, oranges, and blues to evoke a sense of horror and existential despair."),
61
-
62
- # The Son of Man
63
- ("The Son of Man by René Magritte, painted in 1964, is a surrealist self-portrait of the artist. "
64
- "It depicts a man in a bowler hat and suit, with his face obscured by a floating green apple. "
65
- "The background features a cloudy sky and a low wall, contributing to the dreamlike atmosphere. The painting is rich in symbolism, "
66
- "exploring themes of identity, concealment, and perception."),
67
-
68
- # The Bedroom
69
- ("The Bedroom by Vincent van Gogh, painted in 1888, depicts the artist’s simple bedroom in Arles, France. "
70
- "The painting uses bold, contrasting colors—yellow, red, and blue—to create a vibrant, almost childlike view of the space. "
71
- "Van Gogh painted this scene three times, each version representing his sense of comfort and sanctuary in his personal space."),
72
-
73
- # Girl with a Pearl Earring
74
- ("Girl with a Pearl Earring by Johannes Vermeer, painted in 1665, is often referred to as the 'Mona Lisa of the North.' "
75
- "The painting shows a young girl looking over her shoulder, wearing a large pearl earring. The use of light and shadow, "
76
- "combined with soft colors like blue and yellow, creates a lifelike, intimate portrait."),
77
-
78
- # Whistler’s Mother
79
- ("Whistler’s Mother by James McNeill Whistler, painted in 1871, is a portrait of the artist’s mother seated in profile. "
80
- "The painting uses muted tones of black, gray, and brown, reflecting the simplicity and dignity of the subject. "
81
- "It has become an icon of motherhood and restraint."),
82
-
83
- # The Basket of Apples
84
- ("The Basket of Apples by Paul Cézanne, painted around 1895, is a still life that challenges traditional perspectives. "
85
- "The painting shows a table with a basket of apples, a bottle, and bread. The use of soft colors, including browns, reds, and greens, "
86
- "along with the tilted angles, makes the objects seem to float, blurring the line between realism and abstraction.")
87
- ],
88
- "Story": [
89
- # Starry Night
90
- ("Vincent van Gogh painted 'Starry Night' while in a mental asylum in Saint-Rémy-de-Provence, France. "
91
- "It was created from memory and imagination, rather than a direct view from his window. The swirling patterns "
92
- "are thought to represent his emotional turbulence at the time. The painting is celebrated for its bold brushstrokes "
93
- "and imaginative use of color, representing the tension between beauty and chaos in the natural world."),
94
-
95
- # Mona Lisa
96
- ("'Mona Lisa' was painted by Leonardo da Vinci during the Renaissance period. The subject of the painting, "
97
- "believed to be Lisa Gherardini, is famed for her mysterious smile. The painting's sfumato technique, blending "
98
- "soft transitions between light and shadow, creates a lifelike, three-dimensional appearance. The Mona Lisa has inspired "
99
- "countless studies and interpretations over the centuries, and its theft in 1911 only increased its mystique."),
100
-
101
- # The Persistence of Memory
102
- ("Salvador Dalí's 'The Persistence of Memory' is a surrealist masterpiece that reflects the fluidity of time and memory. "
103
- "The melting clocks draped over the landscape suggest the passage of time becoming meaningless. The inspiration for the painting "
104
- "came from a melting camembert cheese. Dalí’s fascination with dream states and Freud's theories of the unconscious mind "
105
- "are evident in this strange, dreamlike scene."),
106
-
107
- # The Scream
108
- ("'The Scream' by Edvard Munch is a vivid expression of anxiety and existential dread. Munch was inspired to create the work "
109
- "after a walk during which he felt the 'great scream' of nature overwhelm him. The distorted figure and fiery red sky reflect "
110
- "Munch’s inner turmoil. The painting has become an iconic representation of human anxiety and has been widely referenced in pop culture."),
111
-
112
- # The Son of Man
113
- ("René Magritte’s 'The Son of Man' is a quintessential example of surrealism, blending reality and fantasy. "
114
- "The painting is a self-portrait with Magritte’s face hidden by a hovering green apple, symbolizing the tension between what is visible "
115
- "and what is hidden. The painting has been widely interpreted as a statement on identity and the nature of perception."),
116
-
117
- # The Bedroom
118
- ("'The Bedroom' by Vincent van Gogh is a reflection of the artist’s longing for stability and tranquility. "
119
- "The painting was created during one of the few peaceful periods in van Gogh’s turbulent life, and the vibrant colors convey "
120
- "his emotions at the time. The bold, contrasting colors and exaggerated perspective make the simple room appear almost alive."),
121
-
122
- # Girl with a Pearl Earring
123
- ("'Girl with a Pearl Earring' by Johannes Vermeer is one of the most enigmatic portraits in Western art. Known for its simplicity and elegance, "
124
- "the painting captures a fleeting moment of connection between the viewer and the subject. The girl’s mysterious gaze and the radiant light "
125
- "on her face have captivated audiences for centuries."),
126
-
127
- # Whistler’s Mother
128
- ("James McNeill Whistler’s 'Arrangement in Grey and Black No. 1,' more commonly known as 'Whistler’s Mother,' is a stark, dignified portrait "
129
- "of the artist’s mother. The painting is renowned for its minimalist composition and restrained use of color. Its iconic status grew after "
130
- "its display at the Musée d'Orsay in Paris, becoming a symbol of maternal devotion and calm."),
131
-
132
- # The Basket of Apples
133
- ("Paul Cézanne’s 'The Basket of Apples' is a revolutionary work that defies the traditional rules of perspective. By tilting objects at different angles, "
134
- "Cézanne challenges the viewer’s perception of space and reality. This still life is often cited as a precursor to Cubism, and its soft color palette "
135
- "creates a serene yet dynamic composition.")
136
- ]
137
- }
138
-
139
-
140
- df = pd.DataFrame(data)
141
-
142
- # Load models
143
-
144
- # Determine if a GPU (CUDA) is available
145
- device = "cuda" if torch.cuda.is_available() else "cpu"
146
-
147
- # TTS model
148
- narrator = pipeline("text-to-speech", model="kakao-enterprise/vits-ljs", device=device)
149
-
150
- # Load the CLIP model and processor
151
- model = CLIPModel.from_pretrained("openai/clip-vit-base-patch32").to(device)
152
- processor = CLIPProcessor.from_pretrained("openai/clip-vit-base-patch32")
153
-
154
- # Load the semantic similarity model for description search
155
- semantic_model = SentenceTransformer('sentence-transformers/all-MiniLM-L6-v2', device=device)
156
-
157
- # Load the translation models for Arabic to English and English to Arabic translations
158
- translator_ar_to_en = pipeline("translation_ar_to_en", model="Helsinki-NLP/opus-mt-ar-en", device=0 if device == "cuda" else -1)
159
- translator_en_to_ar = pipeline("translation_en_to_arabic", model="Helsinki-NLP/opus-mt-en-ar", device=0 if device == "cuda" else -1)
160
-
161
- # Function to Convert the text to Speech in Ensglish
162
- def text_to_speech_english(story_text):
163
-
164
- audio_output = narrator(story_text)
165
-
166
- # Extract audio and sampling rate from the output
167
- audio = np.squeeze(audio_output['audio'])
168
- sampling_rate = audio_output['sampling_rate']
169
-
170
- # Save the output as a WAV file using soundfile
171
- sf.write("story_english.wav", audio, sampling_rate)
172
-
173
- return "story_english.wav"
174
-
175
- # Function to Convert the text to Speech in Arabic using gTTS
176
- def text_to_speech_arabic(story_text):
177
- tts = gTTS(text=story_text, lang='ar')
178
- tts.save("story_arabic.mp3")
179
- return "story_arabic.mp3"
180
-
181
- # Function to translate the full story in chunks
182
- def translate_story_to_arabic(story_text):
183
- sentences = re.split(r'(?<=[.!؟])\s+', story_text) # ٍSplit the story to list of sentences to translate
184
- translated_sentences = []
185
-
186
- for sentence in sentences: # For each sentence translate to arabic and append to the list
187
- translation = translator_en_to_ar(sentence)[0]['translation_text']
188
- translated_sentences.append(translation)
189
-
190
- return ' '.join(translated_sentences) # Return the translated sentences list elements as one String
191
-
192
- # Function to check if the image URL is valid and fetches the image
193
- def fetch_image_from_url(url):
194
- try:
195
- response = requests.get(url, stream=True)
196
- response.raise_for_status() # Check if the request was successful
197
- return Image.open(BytesIO(response.content)) # Return the image if valid
198
- except Exception as e:
199
- print(f"Error fetching image from {url}: {str(e)}")
200
- return None
201
-
202
- # Process the result where result is shown base on selected language
203
- def process_best_match(best_match, language):
204
- best_image_url = best_match["image_url"]
205
- best_story = best_match["Story"]
206
-
207
- # Translate to Arabic if the language is Arabic
208
- if language == "Arabic":
209
- best_story_translated = translate_story_to_arabic(best_story)
210
- info_html = f"<div dir='rtl' style='font-size: 18px; color: white; font-family: Arial, sans-serif;'>{best_story_translated}</div>"
211
- audio_file = text_to_speech_arabic(best_story_translated)
212
- return best_image_url, info_html, audio_file
213
-
214
- # Otherwise, use English
215
- info_html = f"<div style='font-size: 18px; color: white;'>{best_story}</div>"
216
- audio_file = text_to_speech_english(best_story)
217
- return best_image_url, info_html, audio_file
218
-
219
- # Function to match the uploaded image with the DataFrame to retrive the image of painting from the Datafram and it story in text and audio
220
- def compare_images(image, language):
221
- try:
222
- inputs = processor(images=image, return_tensors="pt")
223
- image_features = model.get_image_features(**inputs)
224
-
225
- best_score = -2.0
226
- best_match_idx = None
227
-
228
- for idx, image_url in enumerate(df['image_url']):
229
- db_image = fetch_image_from_url(image_url)
230
- if db_image is None:
231
- continue
232
-
233
- try:
234
- db_inputs = processor(images=db_image, return_tensors="pt")
235
- db_image_features = model.get_image_features(**db_inputs)
236
-
237
- similarity = torch.nn.functional.cosine_similarity(image_features, db_image_features).item()
238
- if similarity > best_score:
239
- best_score = similarity
240
- best_match_idx = idx
241
- except UnidentifiedImageError:
242
- continue
243
-
244
- if best_match_idx is None:
245
- return None, "Error: No valid image match found in the database.", None
246
-
247
- best_match = df.iloc[best_match_idx]
248
- return process_best_match(best_match, language)
249
-
250
- except UnidentifiedImageError:
251
- return None, "Error: The uploaded file is not a valid image.", None
252
- except Exception as e:
253
- return None, f"Error: {str(e)}", None
254
-
255
- # Function to compare user input with descriptions in the DataFrame and return the best match Painting as image of painting with text and audio story of painting
256
- def compare_description(input_text):
257
- try:
258
- language = detect(input_text) #detect the langauge of input
259
- if language == 'ar':
260
- input_text = translator_ar_to_en(input_text)[0]['translation_text']
261
-
262
- input_embedding = semantic_model.encode(input_text, convert_to_tensor=True)
263
- df_embeddings = semantic_model.encode(df["Description"].tolist(), convert_to_tensor=True)
264
-
265
- similarities = util.pytorch_cos_sim(input_embedding, df_embeddings).squeeze()
266
- best_match_idx = torch.argmax(similarities).item()
267
- best_match = df.iloc[best_match_idx]
268
-
269
- return process_best_match(best_match, language)
270
-
271
- except Exception as e:
272
- return None, f"Error: {str(e)}", None
273
-
274
- # Custom CSS for Styling the Gradio
275
-
276
- custom_css = """
277
- .gradio-container {
278
- background-image: url('https://images.squarespace-cdn.com/content/v1/587ee1eab3db2b428f68d221/1626734192415-LI75A3LVVFMJD5TVZ3HR/Gallery+2.jpg');
279
- background-size: cover;
280
- background-position: center;
281
- background-repeat: no-repeat;
282
- color: #333333;
283
- font-family: 'Arial', sans-serif;
284
- }
285
-
286
- h1, #title, #description {
287
- color: white !important;
288
- }
289
-
290
- #upload-text, #description-search-text {
291
- color: white !important;
292
- }
293
-
294
- label, .gr-label {
295
- color: #333333 !important;
296
- }
297
-
298
- button.primary {
299
- background-color: #6A5ACD;
300
- color: black;
301
- border-radius: 10px;
302
- padding: 10px;
303
- margin: 5px;
304
- font-size: 18px;
305
- border: none;
306
- transition: background-color 0.3s;
307
- }
308
-
309
- button.primary:hover {
310
- background-color: #836FFF;
311
- }
312
-
313
- #image_output, #search_image_output {
314
- border: 3px solid white;
315
- border-radius: 10px;
316
- }
317
-
318
- /* Specifically targeting the example buttons */
319
- .gr-examples button {
320
- color: white !important;
321
- background-color: transparent !important; /* Make the background blend in with the overall theme */
322
- border: 1px solid white; /* Add a border if you want to highlight it */
323
- }
324
- """
325
-
326
- image_upload_examples = [
327
- ["https://pbs.twimg.com/media/DgAnD-FUcAAr3NT?format=jpg", "English"],
328
- ["https://pbs.twimg.com/media/DgAnD-FUcAAr3NT?format=jpg", "Arabic"]
329
- ]
330
-
331
- # Sample Examples for the "Description Search" tab
332
- description_search_examples = [
333
- ["A painting of a swirling night sky over a town.", "English"],
334
- ["امرأة بابتسامة غامضة.", "Arabic"]
335
- ]
336
-
337
- # Gradio interface with two tabs: "Image Upload" and "Description Search"
338
- # Image Upload tab to get the Painting story by uploding an image
339
- # Description Search tab is by getting Painting stroy by descriping the painting
340
-
341
- with gr.Blocks(css=custom_css) as demo:
342
- gr.Markdown("<h1 id='title'>Welcome to the Virtual Art Museum</h1>")
343
- gr.Markdown("<p id='description'>Explore the most famous artworks. Upload an image or enter a description to learn about the story behind each piece.</p>")
344
-
345
- with gr.Tab("Image Search"):
346
- gr.Markdown("<h2 id='upload-text'>Upload Art to Recognize and Hear the Story Behind It</h2>")
347
-
348
- image_input = gr.Image(type="pil", label="Upload an image of an art piece")
349
- language_selector = gr.Radio(choices=["English", "Arabic"], label="Select Language for Story Narration", value="English")
350
- recognize_button = gr.Button("Search")
351
-
352
- image_output = gr.Image(label="Matched Art Piece", elem_id="image_output")
353
- description_output = gr.HTML(label="Art Piece Information")
354
- audio_output = gr.Audio(label="Narration of the Story")
355
-
356
- recognize_button.click(compare_images, inputs=[image_input, language_selector], outputs=[image_output, description_output, audio_output])
357
-
358
- gr.Examples(examples=image_upload_examples, inputs=[image_input, language_selector])
359
- with gr.Tab("Description Search"):
360
- gr.Markdown("<h2 id='description-search-text'>Description Search</h2>")
361
-
362
- description_input = gr.Textbox(label="Enter a description (in English or Arabic)")
363
- search_button = gr.Button("Search")
364
-
365
- search_image_output = gr.Image(label="Matched Art Piece", elem_id="search_image_output")
366
- search_description_output = gr.HTML(label="Art Piece Information")
367
- search_audio_output = gr.Audio(label="Narration of the Story")
368
-
369
- search_button.click(compare_description, inputs=description_input, outputs=[search_image_output, search_description_output, search_audio_output])
370
-
371
- gr.Examples(examples=description_search_examples, inputs=description_input)
372
-
373
- demo.launch()