fadiyahalanazi commited on
Commit
acecd95
ยท
verified ยท
1 Parent(s): d90ce3c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +76 -35
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() # Execute installation before running the app
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 from Hugging Face
21
- classifier = pipeline("zero-shot-image-classification", model="openai/clip-vit-base-patch32")
22
 
23
- # API URL for retrieving plant information (Replace with the correct API endpoint)
24
- API_URL = "https://your-new-space-url.hf.space/api"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25
 
26
  # Function to classify plant images and fetch plant information
27
- def classify_plant(image):
28
- labels = ["Rose", "Sunflower", "Tulip", "Daisy", "Dandelion"] # List of plant labels
29
- result = classifier(image, candidate_labels=labels)
30
- plant_name = result["labels"][0] # Get the highest predicted label
31
-
32
- # API request to fetch plant information based on the predicted plant name
33
- try:
34
- response = requests.post(API_URL, json={"data": [plant_name]})
35
- if response.status_code == 200:
36
- plant_info = response.json()["data"][0] # Extract information from API response
37
- else:
38
- plant_info = "โš  No information available for this plant."
39
- except:
40
- plant_info = "โŒ Error connecting to the Plant Info API."
 
 
 
 
 
 
 
 
 
 
41
 
42
  return plant_name, plant_info
43
 
44
- # Gradio interface with a navy blue background
45
  with gr.Blocks(css="""
46
  .gradio-container {
47
- background-color: #001f3f; /* Navy blue background */
48
  font-family: 'Arial', sans-serif;
49
  color: white;
50
  text-align: center;
51
  }
52
  h1 {
53
- color: #ffffff;
54
  font-size: 32px;
55
  margin-bottom: 10px;
56
  }
57
  p {
58
- color: #d3d3d3;
59
  font-size: 18px;
60
  }
61
  .gradio-container .btn {
62
- background-color: #00A86B !important; /* Deep green button */
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: #003366;
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 Classifier AI</h1>")
86
- gr.Markdown("<p>Upload a plant image to classify it and retrieve detailed information.</p>")
87
 
88
- # Layout with columns for image input and classification results
89
  with gr.Row():
90
  with gr.Column(scale=1):
91
  image_input = gr.Image(type="pil", label="๐Ÿ“ธ Upload a Plant Image")
92
- classify_button = gr.Button("๐Ÿ” Identify Plant & Get Info")
 
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 the classify button to the function
98
- classify_button.click(classify_plant, inputs=[image_input], outputs=[plant_name_output, plant_info_output])
99
 
100
- # Footer text
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)