razanalsulami commited on
Commit
1c0714f
1 Parent(s): 7f8d0d5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +124 -51
app.py CHANGED
@@ -7,33 +7,140 @@ import gradio as gr
7
  import torch
8
  from gtts import gTTS
9
  import IPython.display as ipd
10
-
11
  device = "cuda" if torch.cuda.is_available() else "cpu"
12
  translator_ar_to_en = pipeline("translation_ar_to_en", model="Helsinki-NLP/opus-mt-ar-en", device=0 if device == "cuda" else -1)
13
  translator_en_to_ar = pipeline("translation_en_to_arabic", model="Helsinki-NLP/opus-mt-en-ar", device=0 if device == "cuda" else -1)
14
-
15
  # Load the plant identification pipeline from Hugging Face
16
  pipe = pipeline("image-classification", model="umutbozdag/plant-identity")
17
-
18
- # Define the dictionaries for plant care and facts (unchanged)
19
- plant_care_dict = { ... }
20
- plant_facts_dict = { ... }
21
- plant_name_translations = { ... }
22
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23
  def classify_plant_image(image):
24
  image.save('temp_image.jpg') # Save the image locally for pipeline processing
25
  predictions = pipe('temp_image.jpg')
26
  top_prediction = predictions[0] # Get the top prediction
27
  return top_prediction['label']
28
-
29
  def text_to_speech(instructions, language='en'):
30
  tts = gTTS(text=instructions, lang=language)
31
  tts.save('temp_audio.mp3') # Save the audio file
32
  return 'temp_audio.mp3'
33
-
34
  def process_image_input(image=None, language='english'):
35
  plant_name = classify_plant_image(image) # Identify plant from image
36
 
 
 
 
37
  # Get care instructions in English
38
  care_instructions = plant_care_dict.get(plant_name, f"Care instructions are not available for '{plant_name}'.")
39
  plant_fact = plant_facts_dict.get(plant_name, f"Facts are not available for '{plant_name}'.")
@@ -49,8 +156,12 @@ def process_image_input(image=None, language='english'):
49
 
50
  return plant_name, care_instructions, audio_file
51
 
 
52
  def process_text_input(plant_name=None, language='english'):
53
  if plant_name:
 
 
 
54
  # Get care instructions in English
55
  care_instructions = plant_care_dict.get(plant_name, f"Care instructions are not available for '{plant_name}'.")
56
  plant_fact = plant_facts_dict.get(plant_name, f"Facts are not available for '{plant_name}'.")
@@ -65,43 +176,8 @@ def process_text_input(plant_name=None, language='english'):
65
  audio_file = text_to_speech(plant_fact, language='ar' if language == 'arabic' else 'en')
66
 
67
  return plant_name, care_instructions, audio_file
68
-
69
- # Define custom CSS
70
- css_style = """
71
- body {
72
- background-color: #FFC0CB; /* Baby Pink Background */
73
- }
74
-
75
- .gradio-container {
76
- border-radius: 10px;
77
- padding: 20px;
78
- background-color: white;
79
- box-shadow: 2px 2px 8px rgba(0, 0, 0, 0.1);
80
- }
81
- """
82
-
83
- # Adding custom JavaScript to dynamically change button and audio styles
84
- js_script = """
85
- <script>
86
- // Wait for Gradio components to load before applying styles
87
- window.onload = function() {
88
- // Change button color to green
89
- document.querySelectorAll("button").forEach(function(button) {
90
- button.style.backgroundColor = '#4CAF50';
91
- button.style.color = 'white';
92
- });
93
-
94
- // Change audio player background color to green
95
- document.querySelectorAll("audio").forEach(function(audio) {
96
- audio.style.backgroundColor = '#4CAF50';
97
- audio.style.border = '2px solid #45a049';
98
- });
99
- }
100
- </script>
101
- """
102
-
103
- # Creating Gradio app with tabs and custom CSS
104
- with gr.Blocks(css=css_style) as demo:
105
  gr.Markdown("## Plant Identification and Care Instructions")
106
 
107
  with gr.Tab("Image Input"):
@@ -136,11 +212,8 @@ with gr.Blocks(css=css_style) as demo:
136
  description="Enter the name of a plant to receive care instructions in your chosen language."
137
  )
138
 
139
- # Insert JavaScript after Gradio components are rendered
140
- gr.HTML(js_script)
141
-
142
  # Launch the Gradio app with Tabs
143
  demo.launch()
144
 
145
  if __name__ == "__main__":
146
- app.run(host="0.0.0.0", port=7860)
 
7
  import torch
8
  from gtts import gTTS
9
  import IPython.display as ipd
 
10
  device = "cuda" if torch.cuda.is_available() else "cpu"
11
  translator_ar_to_en = pipeline("translation_ar_to_en", model="Helsinki-NLP/opus-mt-ar-en", device=0 if device == "cuda" else -1)
12
  translator_en_to_ar = pipeline("translation_en_to_arabic", model="Helsinki-NLP/opus-mt-en-ar", device=0 if device == "cuda" else -1)
 
13
  # Load the plant identification pipeline from Hugging Face
14
  pipe = pipeline("image-classification", model="umutbozdag/plant-identity")
15
+ plant_care_dict = {
16
+ "Aloe Vera": "Water every 2-3 weeks, allowing the soil to dry out between waterings. Place in bright, indirect sunlight.",
17
+ "Bamboo": "Keep the soil moist but not soggy. Place in indirect sunlight, and avoid direct exposure to sunlight.",
18
+ "Basil": "Water regularly to keep the soil moist. Place in full sun for at least 6 hours a day.",
19
+ "Boston fern": "Water regularly to maintain humidity. Mist the leaves often and keep in indirect sunlight.",
20
+ "English ivy": "Water when the top inch of soil is dry. Place in moderate sunlight and maintain humidity.",
21
+ "Ficus": "Water when the top inch of soil is dry. Place in bright, indirect light, and rotate the plant occasionally.",
22
+ "Fiddle leaf fig": "Water when the top inch of soil is dry. Place in bright, indirect light, and avoid drafts.",
23
+ "Lavender": "Water when the soil is dry to the touch. Place in full sun and ensure good drainage.",
24
+ "Mint": "Water regularly to keep the soil moist. Place in partial shade, and pinch back to encourage bushiness.",
25
+ "Monstera deliciosa": "Water when the top inch of soil is dry. Place in indirect sunlight and provide support for climbing.",
26
+ "Orchids": "Water every 1-2 weeks. Provide humidity and place in indirect light.",
27
+ "Peace Lily": "Water when the top inch of soil is dry. Place in low light and keep out of direct sunlight.",
28
+ "Philodendron": "Water when the top inch of soil is dry. Place in indirect sunlight, and prune as needed.",
29
+ "Pothos": "Water when the top inch of soil is dry. Tolerates low light but grows best in bright, indirect light.",
30
+ "Rosemary": "Water when the top inch of soil is dry. Place in full sun and ensure good drainage.",
31
+ "Rubber plant": "Water when the top inch of soil is dry. Place in bright, indirect light, and dust leaves regularly.",
32
+ "Snake plant": "Water every 2-6 weeks, depending on light conditions. Place in low to bright indirect light.",
33
+ "Spider plant": "Water when the top inch of soil is dry. Place in bright, indirect light, and avoid soggy soil.",
34
+ "Succulents": "Water every 1-3 weeks, allowing the soil to dry completely between waterings. Place in bright sunlight.",
35
+ "ZZ plant": "Water when the soil is completely dry, usually every 2-3 weeks. Tolerates low light conditions.",
36
+ "Jasmine": "Water when the top inch of soil is dry. Place in full sun and prune regularly to encourage growth.",
37
+ "Rose": "Water regularly, keeping the soil moist but not soggy. Place in full sun and fertilize during the growing season.",
38
+ "Chamomile": "Water when the top inch of soil is dry. Place in full sun, and harvest flowers regularly.",
39
+ "Marigold": "Water regularly, especially in dry conditions. Place in full sun and deadhead flowers to promote blooming.",
40
+ "Daffodil": "Water when the soil is dry to the touch. Place in full sun and avoid overwatering.",
41
+ "Geranium": "Water when the top inch of soil is dry. Place in full sun and pinch back to promote bushiness.",
42
+ "Thyme": "Water when the soil is dry. Place in full sun and ensure good drainage.",
43
+ "Coriander": "Water regularly to keep the soil moist. Place in full sun and harvest leaves often.",
44
+ "Lemon Balm": "Water when the soil is dry. Place in full sun and prune regularly to encourage growth.",
45
+ "Sage": "Water when the soil is dry. Place in full sun and provide well-drained soil.",
46
+ "Peppermint": "Water regularly to keep the soil moist. Place in partial shade and pinch back to promote bushiness.",
47
+ "Bougainvillea": "Water when the soil is dry. Place in full sun and prune regularly to encourage flowering.",
48
+ "Desert Rose": "Water when the soil is dry. Place in full sun and ensure good drainage.",
49
+ "Palm Tree": "Water when the top inch of soil is dry. Place in bright sunlight and provide adequate space for growth.",
50
+ "Oleander": "Water when the top inch of soil is dry. Place in full sun and fertilize regularly.",
51
+ "Cactus": "Water every few weeks, allowing the soil to dry completely between waterings. Place in bright sunlight."
52
+ }
53
+ # Plant facts dictionary
54
+ plant_facts_dict = {
55
+ "Aloe Vera": "Aloe Vera is known for its soothing properties and is commonly used in skincare products. It thrives in bright, indirect sunlight.",
56
+ "Bamboo": "Bamboo is one of the fastest-growing plants in the world. It prefers moist soil and indirect sunlight.",
57
+ "Basil": "Basil is a fragrant herb that is often used in cooking. It needs plenty of sunlight and regular watering to thrive.",
58
+ "Boston fern": "Boston ferns are popular houseplants known for their lush, green fronds. They prefer high humidity and indirect sunlight.",
59
+ "English ivy": "English ivy is a climbing plant that can adapt to various conditions. It likes well-drained soil and moderate sunlight.",
60
+ "Ficus": "Ficus plants are versatile and can be grown indoors or outdoors. They prefer bright, indirect light and moderate watering.",
61
+ "Fiddle leaf fig": "Fiddle leaf figs are known for their large, glossy leaves. They thrive in bright, indirect light and require regular watering.",
62
+ "Lavender": "Lavender is a fragrant herb often used in aromatherapy. It prefers full sun and well-drained soil.",
63
+ "Mint": "Mint is a fast-growing herb that can easily spread. It prefers partial shade and moist soil.",
64
+ "Monstera deliciosa": "Monstera is known for its unique leaf holes. It thrives in indirect sunlight and requires regular watering.",
65
+ "Orchids": "Orchids are exotic flowers that require special care. They prefer indirect light and humidity.",
66
+ "Peace Lily": "Peace Lilies are known for their elegant white flowers. They thrive in low light and require regular watering.",
67
+ "Philodendron": "Philodendrons are popular houseplants with heart-shaped leaves. They prefer indirect sunlight and moderate watering.",
68
+ "Pothos": "Pothos are resilient plants that can survive in low light. They prefer to dry out between waterings.",
69
+ "Rosemary": "Rosemary is an aromatic herb that is commonly used in cooking. It prefers full sun and well-drained soil.",
70
+ "Rubber plant": "Rubber plants are known for their large, glossy leaves. They thrive in bright, indirect light and moderate watering.",
71
+ "Snake plant": "Snake plants are hardy and can tolerate low light. They require infrequent watering.",
72
+ "Spider plant": "Spider plants are known for their air-purifying properties. They prefer bright, indirect light and regular watering.",
73
+ "Succulents": "Succulents are drought-tolerant plants that store water in their leaves. They prefer bright sunlight and well-drained soil.",
74
+ "ZZ plant": "ZZ plants are known for their glossy leaves and resilience. They thrive in low light and require infrequent watering.",
75
+ "Jasmine": "Jasmine is a fragrant flower often used in perfumes. It prefers full sun and well-drained soil.",
76
+ "Rose": "Roses are popular garden plants known for their beautiful flowers. They prefer full sun and regular watering.",
77
+ "Chamomile": "Chamomile is an aromatic herb used in teas. It prefers full sun and well-drained soil.",
78
+ "Marigold": "Marigolds are bright flowers that repel pests. They thrive in full sun and require regular watering.",
79
+ "Daffodil": "Daffodils are spring flowers known for their trumpet shape. They prefer well-drained soil and full sun.",
80
+ "Geranium": "Geraniums are popular bedding plants. They prefer full sun and regular watering.",
81
+ "Thyme": "Thyme is an aromatic herb commonly used in cooking. It prefers full sun and well-drained soil.",
82
+ "Coriander": "Coriander is an herb known for its distinct flavor. It prefers full sun and well-drained soil.",
83
+ "Lemon Balm": "Lemon balm is a fragrant herb often used in teas. It prefers full sun and regular watering.",
84
+ "Sage": "Sage is a hardy herb commonly used in cooking. It prefers full sun and well-drained soil.",
85
+ "Peppermint": "Peppermint is a popular herb known for its refreshing flavor. It prefers partial shade and moist soil.",
86
+ "Bougainvillea": "Bougainvillea is a colorful flowering plant. It prefers full sun and well-drained soil.",
87
+ "Desert Rose": "Desert roses are succulent plants known for their beautiful flowers. They prefer full sun and well-drained soil.",
88
+ "Palm Tree": "Palm trees are iconic tropical plants. They prefer bright sunlight and well-drained soil.",
89
+ "Oleander": "Oleander is a hardy shrub known for its flowers. It prefers full sun and well-drained soil.",
90
+ "Cactus": "Cacti are drought-resistant plants that thrive in dry conditions. They prefer bright sunlight and minimal watering."
91
+ }
92
+ plant_name_translations = {
93
+ "Aloe Vera": "Aloe Vera - صبار الألوفيرا",
94
+ "Bamboo": "Bamboo - الخيزران",
95
+ "Basil": "Basil - الريحان",
96
+ "Boston fern": "Boston Fern - سرخس بوسطن",
97
+ "English ivy": "English Ivy - اللبلاب الإنجليزي",
98
+ "Ficus": "Ficus - نبات الفيكس",
99
+ "Fiddle leaf fig": "Fiddle Leaf Fig - التين ورقي الكمان",
100
+ "Lavender": "Lavender - الخزامى",
101
+ "Mint": "Mint - النعناع",
102
+ "Monstera deliciosa": "Monstera Deliciosa - مونستيرا دليسيوسا",
103
+ "Orchids": "Orchids - الأوركيد",
104
+ "Peace Lily": "Peace Lily - زنبق السلام",
105
+ "Philodendron": "Philodendron - الفيلوديندرون",
106
+ "Pothos": "Pothos - البوتس",
107
+ "Rosemary": "Rosemary - إكليل الجبل",
108
+ "Rubber plant": "Rubber Plant - نبات المطاط",
109
+ "Snake plant": "Snake Plant - نبات الثعبان",
110
+ "Spider plant": "Spider Plant - نبات العنكبوت",
111
+ "Succulents": "Succulents - النباتات العصارية",
112
+ "ZZ plant": "ZZ Plant - نبات زيزي",
113
+ "Jasmine": "Jasmine - الياسمين",
114
+ "Rose": "Rose - الورد",
115
+ "Chamomile": "Chamomile - البابونج",
116
+ "Marigold": "Marigold - القطيفة",
117
+ "Daffodil": "Daffodil - النرجس",
118
+ "Geranium": "Geranium - إبرة الراعي",
119
+ "Thyme": "Thyme - الزعتر",
120
+ "Coriander": "Coriander - الكزبرة",
121
+ "Lemon Balm": "Lemon Balm - بلسم الليمون",
122
+ "Sage": "Sage - الميرمية",
123
+ "Peppermint": "Peppermint - النعناع الفلفلي",
124
+ "Bougainvillea": "Bougainvillea - الجهنمية",
125
+ "Desert Rose": "Desert Rose - وردة الصحراء",
126
+ "Palm Tree": "Palm Tree - النخيل",
127
+ "Oleander": "Oleander - دفلى",
128
+ "Cactus": "Cactus - الصبار"}
129
  def classify_plant_image(image):
130
  image.save('temp_image.jpg') # Save the image locally for pipeline processing
131
  predictions = pipe('temp_image.jpg')
132
  top_prediction = predictions[0] # Get the top prediction
133
  return top_prediction['label']
 
134
  def text_to_speech(instructions, language='en'):
135
  tts = gTTS(text=instructions, lang=language)
136
  tts.save('temp_audio.mp3') # Save the audio file
137
  return 'temp_audio.mp3'
 
138
  def process_image_input(image=None, language='english'):
139
  plant_name = classify_plant_image(image) # Identify plant from image
140
 
141
+ # Debugging: Log the identified plant name
142
+ print(f"Identified Plant Name: {plant_name}")
143
+
144
  # Get care instructions in English
145
  care_instructions = plant_care_dict.get(plant_name, f"Care instructions are not available for '{plant_name}'.")
146
  plant_fact = plant_facts_dict.get(plant_name, f"Facts are not available for '{plant_name}'.")
 
156
 
157
  return plant_name, care_instructions, audio_file
158
 
159
+ # Main processing function for text input
160
  def process_text_input(plant_name=None, language='english'):
161
  if plant_name:
162
+ # Debugging: Log the requested plant name
163
+ print(f"Requested Plant Name: {plant_name}")
164
+
165
  # Get care instructions in English
166
  care_instructions = plant_care_dict.get(plant_name, f"Care instructions are not available for '{plant_name}'.")
167
  plant_fact = plant_facts_dict.get(plant_name, f"Facts are not available for '{plant_name}'.")
 
176
  audio_file = text_to_speech(plant_fact, language='ar' if language == 'arabic' else 'en')
177
 
178
  return plant_name, care_instructions, audio_file
179
+ # Creating Gradio app with tabs
180
+ with gr.Blocks() as demo:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
181
  gr.Markdown("## Plant Identification and Care Instructions")
182
 
183
  with gr.Tab("Image Input"):
 
212
  description="Enter the name of a plant to receive care instructions in your chosen language."
213
  )
214
 
 
 
 
215
  # Launch the Gradio app with Tabs
216
  demo.launch()
217
 
218
  if __name__ == "__main__":
219
+ app.run(host="0.0.0.0", port=7860)