Spaces:
Sleeping
Sleeping
razanalsulami
commited on
Commit
•
1fbd741
1
Parent(s):
eb96d4f
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,219 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Import required libraries
|
2 |
+
import requests
|
3 |
+
from PIL import Image
|
4 |
+
from io import BytesIO
|
5 |
+
from transformers import pipeline
|
6 |
+
import gradio as gr
|
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}'.")
|
147 |
+
|
148 |
+
# Translate care instructions and facts to Arabic
|
149 |
+
if language == 'arabic':
|
150 |
+
care_instructions = translator_en_to_ar(care_instructions)[0]['translation_text']
|
151 |
+
plant_fact = translator_en_to_ar(plant_fact)[0]['translation_text']
|
152 |
+
plant_name = plant_name_translations.get(plant_name, plant_name) # Translate plant name to Arabic
|
153 |
+
|
154 |
+
# Generate audio for the facts
|
155 |
+
audio_file = text_to_speech(plant_fact, language='ar' if language == 'arabic' else 'en')
|
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}'.")
|
168 |
+
|
169 |
+
# Translate care instructions and facts to Arabic
|
170 |
+
if language == 'arabic':
|
171 |
+
care_instructions = translator_en_to_ar(care_instructions)[0]['translation_text']
|
172 |
+
plant_fact = translator_en_to_ar(plant_fact)[0]['translation_text']
|
173 |
+
plant_name = plant_name_translations.get(plant_name, plant_name) # Translate plant name to Arabic
|
174 |
+
|
175 |
+
# Generate audio for the facts
|
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"):
|
184 |
+
gr.Interface(
|
185 |
+
fn=process_image_input,
|
186 |
+
inputs=[
|
187 |
+
gr.Image(type="pil", label="Upload a Plant Image"), # Image input
|
188 |
+
gr.Dropdown(choices=["english", "arabic"], label="Select Language", value="english") # Language selection
|
189 |
+
],
|
190 |
+
outputs=[
|
191 |
+
gr.Textbox(label="Identified Plant"),
|
192 |
+
gr.Textbox(label="Care Instructions"),
|
193 |
+
gr.Audio(label="Audio Fact") # Output audio file
|
194 |
+
],
|
195 |
+
title="Identify Plant from Image",
|
196 |
+
description="Upload an image of a plant to identify it and receive care instructions in your chosen language."
|
197 |
+
)
|
198 |
+
|
199 |
+
with gr.Tab("Text Input"):
|
200 |
+
gr.Interface(
|
201 |
+
fn=process_text_input,
|
202 |
+
inputs=[
|
203 |
+
gr.Textbox(label="Enter Plant Name"), # Text input
|
204 |
+
gr.Dropdown(choices=["english", "arabic"], label="Select Language", value="english") # Language selection
|
205 |
+
],
|
206 |
+
outputs=[
|
207 |
+
gr.Textbox(label="Identified Plant"),
|
208 |
+
gr.Textbox(label="Care Instructions"),
|
209 |
+
gr.Audio(label="Audio Fact") # Output audio file
|
210 |
+
],
|
211 |
+
title="Identify Plant by Name",
|
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)
|