Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,5 +1,6 @@
|
|
1 |
import os
|
2 |
import subprocess
|
|
|
3 |
|
4 |
# Install necessary packages if not already installed
|
5 |
def install_packages():
|
@@ -10,56 +11,95 @@ def install_packages():
|
|
10 |
except ImportError:
|
11 |
subprocess.call(["pip", "install", package])
|
12 |
|
13 |
-
install_packages() #
|
14 |
|
15 |
# Import required libraries
|
16 |
-
from transformers import pipeline
|
17 |
import gradio as gr
|
18 |
import requests
|
|
|
19 |
|
20 |
-
# Load the image classification model
|
21 |
-
classifier = pipeline("
|
22 |
|
23 |
-
|
24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
|
26 |
# Function to classify plant images and fetch plant information
|
27 |
-
def
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
41 |
|
42 |
return plant_name, plant_info
|
43 |
|
44 |
-
# Gradio interface with a
|
45 |
with gr.Blocks(css="""
|
46 |
.gradio-container {
|
47 |
-
background-color: #
|
48 |
font-family: 'Arial', sans-serif;
|
49 |
color: white;
|
50 |
text-align: center;
|
51 |
}
|
52 |
h1 {
|
53 |
-
color: #
|
54 |
font-size: 32px;
|
55 |
margin-bottom: 10px;
|
56 |
}
|
57 |
p {
|
58 |
-
color: #
|
59 |
font-size: 18px;
|
60 |
}
|
61 |
.gradio-container .btn {
|
62 |
-
background-color: #00A86B !important;
|
63 |
color: white !important;
|
64 |
font-size: 18px;
|
65 |
padding: 10px 20px;
|
@@ -74,7 +114,7 @@ with gr.Blocks(css="""
|
|
74 |
font-size: 16px;
|
75 |
font-weight: bold;
|
76 |
color: #ffffff;
|
77 |
-
background-color: #
|
78 |
border: 2px solid #00A86B;
|
79 |
padding: 10px;
|
80 |
border-radius: 8px;
|
@@ -82,23 +122,24 @@ with gr.Blocks(css="""
|
|
82 |
""") as interface:
|
83 |
|
84 |
# App title and description
|
85 |
-
gr.Markdown("<h1>๐ฟ Plant
|
86 |
-
gr.Markdown("<p>Upload
|
87 |
|
88 |
-
# Layout
|
89 |
with gr.Row():
|
90 |
with gr.Column(scale=1):
|
91 |
image_input = gr.Image(type="pil", label="๐ธ Upload a Plant Image")
|
92 |
-
|
|
|
93 |
with gr.Column(scale=1):
|
94 |
plant_name_output = gr.Textbox(label="๐ฑ Identified Plant Name", interactive=False, elem_classes="textbox")
|
95 |
plant_info_output = gr.Textbox(label="๐ Plant Information", interactive=False, lines=4, elem_classes="textbox")
|
96 |
|
97 |
-
# Connect
|
98 |
-
classify_button.click(
|
99 |
|
100 |
-
# Footer
|
101 |
gr.Markdown("<p style='font-size: 14px; text-align: center;'>Developed with โค๏ธ using Hugging Face & Gradio</p>")
|
102 |
|
103 |
-
# Launch the application
|
104 |
-
interface.launch()
|
|
|
1 |
import os
|
2 |
import subprocess
|
3 |
+
import spaces
|
4 |
|
5 |
# Install necessary packages if not already installed
|
6 |
def install_packages():
|
|
|
11 |
except ImportError:
|
12 |
subprocess.call(["pip", "install", package])
|
13 |
|
14 |
+
install_packages() # Install dependencies before running the app
|
15 |
|
16 |
# Import required libraries
|
17 |
+
from transformers import pipeline, AutoModelForCausalLM, AutoTokenizer
|
18 |
import gradio as gr
|
19 |
import requests
|
20 |
+
import torch
|
21 |
|
22 |
+
# Load the image classification model
|
23 |
+
classifier = pipeline("image-classification", model="umutbozdag/plant-identity")
|
24 |
|
25 |
+
|
26 |
+
# Function to get the appropriate text-generation model
|
27 |
+
def get_model_name(language):
|
28 |
+
model_mapping = {
|
29 |
+
"English": "microsoft/Phi-3-mini-4k-instruct",
|
30 |
+
"Arabic": "ALLaM-AI/ALLaM-7B-Instruct-preview"
|
31 |
+
}
|
32 |
+
return model_mapping.get(language, "ALLaM-AI/ALLaM-7B-Instruct-preview")
|
33 |
+
|
34 |
+
# Function to load the text-generation model
|
35 |
+
def load_text_model(model_name):
|
36 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
37 |
+
model = AutoModelForCausalLM.from_pretrained(
|
38 |
+
model_name,
|
39 |
+
device_map=device,
|
40 |
+
torch_dtype="auto",
|
41 |
+
trust_remote_code=True,
|
42 |
+
)
|
43 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
44 |
+
generator = pipeline(
|
45 |
+
"text-generation",
|
46 |
+
model=model,
|
47 |
+
tokenizer=tokenizer,
|
48 |
+
return_full_text=False,
|
49 |
+
max_new_tokens=500,
|
50 |
+
do_sample=False
|
51 |
+
)
|
52 |
+
return generator
|
53 |
+
|
54 |
+
@spaces.GPU
|
55 |
|
56 |
# Function to classify plant images and fetch plant information
|
57 |
+
def classify_and_get_info(image, language):
|
58 |
+
result = classifier(image)
|
59 |
+
|
60 |
+
if result: # Ensure result is not empty
|
61 |
+
plant_name = result[0]["label"] # Extract the top predicted class
|
62 |
+
else:
|
63 |
+
return "Unknown", "Could not classify the plant."
|
64 |
+
|
65 |
+
|
66 |
+
# Load the appropriate text-generation model based on language
|
67 |
+
model_name = get_model_name(language)
|
68 |
+
text_generator = load_text_model(model_name)
|
69 |
+
|
70 |
+
# Define the prompt for plant information
|
71 |
+
prompt = (
|
72 |
+
f"Provide detailed information about {plant_name}. Include its scientific name, growing conditions, common uses, and care tips."
|
73 |
+
if language == "English"
|
74 |
+
else f"ูุฏู
ู
ุนููู
ุงุช ู
ูุตูุฉ ุนู {plant_name}. ุงุฐูุฑ ุงุณู
ู ุงูุนูู
ูุ ูุธุฑูู ูู
ููุ ูุงุณุชุฎุฏุงู
ุงุชู ุงูุดุงุฆุนุฉุ ููุตุงุฆุญ ุงูุนูุงูุฉ ุจู."
|
75 |
+
)
|
76 |
+
|
77 |
+
messages = [{"role": "user", "content": prompt}]
|
78 |
+
output = text_generator(messages)
|
79 |
+
|
80 |
+
plant_info = output[0]["generated_text"] if output else "No detailed information available."
|
81 |
|
82 |
return plant_name, plant_info
|
83 |
|
84 |
+
# Gradio interface with a styled theme
|
85 |
with gr.Blocks(css="""
|
86 |
.gradio-container {
|
87 |
+
background-color: #d9ccdf;
|
88 |
font-family: 'Arial', sans-serif;
|
89 |
color: white;
|
90 |
text-align: center;
|
91 |
}
|
92 |
h1 {
|
93 |
+
color: #333333;
|
94 |
font-size: 32px;
|
95 |
margin-bottom: 10px;
|
96 |
}
|
97 |
p {
|
98 |
+
color: #181817;
|
99 |
font-size: 18px;
|
100 |
}
|
101 |
.gradio-container .btn {
|
102 |
+
background-color: #00A86B !important;
|
103 |
color: white !important;
|
104 |
font-size: 18px;
|
105 |
padding: 10px 20px;
|
|
|
114 |
font-size: 16px;
|
115 |
font-weight: bold;
|
116 |
color: #ffffff;
|
117 |
+
background-color: #b69dc2;
|
118 |
border: 2px solid #00A86B;
|
119 |
padding: 10px;
|
120 |
border-radius: 8px;
|
|
|
122 |
""") as interface:
|
123 |
|
124 |
# App title and description
|
125 |
+
gr.Markdown("<h1>๐ฟ AI Plant Visiรณn </h1>")
|
126 |
+
gr.Markdown("<p>Upload an image to identify a plant and retrieve detailed information in English or Arabic.</p>")
|
127 |
|
128 |
+
# Layout for user input and results
|
129 |
with gr.Row():
|
130 |
with gr.Column(scale=1):
|
131 |
image_input = gr.Image(type="pil", label="๐ธ Upload a Plant Image")
|
132 |
+
language_selector = gr.Radio(["English", "Arabic"], label="๐ Choose Language", value="English")
|
133 |
+
classify_button = gr.Button("๐ Identify & Get Info")
|
134 |
with gr.Column(scale=1):
|
135 |
plant_name_output = gr.Textbox(label="๐ฑ Identified Plant Name", interactive=False, elem_classes="textbox")
|
136 |
plant_info_output = gr.Textbox(label="๐ Plant Information", interactive=False, lines=4, elem_classes="textbox")
|
137 |
|
138 |
+
# Connect button click to function
|
139 |
+
classify_button.click(classify_and_get_info, inputs=[image_input, language_selector], outputs=[plant_name_output, plant_info_output])
|
140 |
|
141 |
+
# Footer
|
142 |
gr.Markdown("<p style='font-size: 14px; text-align: center;'>Developed with โค๏ธ using Hugging Face & Gradio</p>")
|
143 |
|
144 |
+
# Launch the application with public link
|
145 |
+
interface.launch(share=True)
|