straubchristine commited on
Commit
fd5c7c6
β€’
1 Parent(s): 8c12c83

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +91 -0
app.py ADDED
@@ -0,0 +1,91 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import google.generativeai as generativeai
2
+ from pathlib import Path
3
+ import gradio as gr
4
+ from dotenv import load_dotenv
5
+ import os
6
+
7
+ load_dotenv()
8
+
9
+ generativeai.configure(api_key=os.getenv("GENERATIVEAI_API_KEY"))
10
+
11
+ generation_config = {
12
+ "temperature":0.4,
13
+ "top_p":1,
14
+ "top_k":32,
15
+ "max_output_tokens":4096,
16
+ }
17
+ # Define safety settings for content generation
18
+ safety_settings = [
19
+ {"category": f"HARM_CATEGORY_{category}", "threshold": "BLOCK_MEDIUM_AND_ABOVE"}
20
+ for category in ["HARASSMENT", "HATE_SPEECH", "SEXUALLY_EXPLICIT", "DANGEROUS_CONTENT"]
21
+ ]
22
+
23
+ # Initialize the GenerativeModel with the specified model name, configuration, and safety settings
24
+ model = generativeai.GenerativeModel(
25
+ model_name="gemini-pro-vision",
26
+ generation_config=generation_config,
27
+ safety_settings=safety_settings,
28
+ )
29
+ # Function to read image data from a file path
30
+ def read_image_data(file_path):
31
+ image_path = Path(file_path)
32
+ if not image_path.exists():
33
+ raise FileNotFoundError(f"Could not find image: {image_path}")
34
+ return {"mime_type": "image/jpeg", "data": image_path.read_bytes()}
35
+
36
+ # Function to generate a response based on a prompt and an image path
37
+ def generate_gemini_response(prompt, image_path):
38
+ image_data = read_image_data(image_path)
39
+ response = model.generate_content([prompt, image_data])
40
+ return response.text
41
+
42
+ # Initial input prompt for the plant pathologist
43
+ input_prompt = """
44
+ As a highly skilled plant pathologist, your expertise is indispensable in our pursuit of maintaining optimal plant health. You will be provided with information or samples related to plant diseases, and your role involves conducting a detailed analysis to identify the specific issues, propose solutions, and offer recommendations.
45
+
46
+ **Analysis Guidelines:**
47
+
48
+ 1. **Disease Identification:** Examine the provided information or samples to identify and characterize plant diseases accurately.
49
+
50
+ 2. **Detailed Findings:** Provide in-depth findings on the nature and extent of the identified plant diseases, including affected plant parts, symptoms, and potential causes.
51
+
52
+ 3. **Next Steps:** Outline the recommended course of action for managing and controlling the identified plant diseases. This may involve treatment options, preventive measures, or further investigations.
53
+
54
+ 4. **Recommendations:** Offer informed recommendations for maintaining plant health, preventing disease spread, and optimizing overall plant well-being.
55
+
56
+ 5. **Important Note:** As a plant pathologist, your insights are vital for informed decision-making in agriculture and plant management. Your response should be thorough, concise, and focused on plant health.
57
+
58
+ **Disclaimer:**
59
+ *"Please note that the information provided is based on plant pathology analysis and should not replace professional agricultural advice. Consult with qualified agricultural experts before implementing any strategies or treatments."*
60
+
61
+ Your role is pivotal in ensuring the health and productivity of plants. Proceed to analyze the provided information or samples, adhering to the structured. Do not mention any Plant Pathologist Name, Date or References Id etc. Try to mention the disease name.
62
+ """
63
+
64
+ # Function to process uploaded files and generate a response
65
+ def process_uploaded_files(files):
66
+ file_path = files[0].name if files else None
67
+ response = generate_gemini_response(input_prompt, file_path) if file_path else None
68
+ return file_path, response
69
+
70
+ # Gradio interface setup
71
+ with gr.Blocks() as demo:
72
+ gr.Markdown(
73
+ """
74
+ # Plant Disease Detection & Deep Analyzer App πŸŒ±πŸŒΎπŸ€–πŸ§ 
75
+ The Plant Disease Detection & Analyzer App revolutionizes agriculture by harnessing advanced technology for early detection of plant diseases. This innovative application employs image recognition and machine learning algorithms to analyze photos of plant leaves, identifying potential ailments swiftly and accurately. Farmers & Plant Pathologists can simply capture images using their smartphones, and the app provides instant insights into the health of their crops. Real-time data and trend analysis further empower users to make informed decisions about crop management and disease prevention. By facilitating proactive measures, this app contributes to sustainable farming practices, ensuring food security for a growing global population.
76
+ """)
77
+ file_output = gr.Textbox()
78
+ image_output = gr.Image()
79
+ combined_output = [image_output, file_output]
80
+
81
+ # Upload button for user to provide images
82
+ upload_button = gr.UploadButton(
83
+ "Click to Upload an Image",
84
+ file_types=["image"],
85
+ file_count="multiple",
86
+ )
87
+ # Set up the upload button to trigger the processing function
88
+ upload_button.upload(process_uploaded_files, upload_button, combined_output)
89
+
90
+ # Launch the Gradio interface with debug mode enabled
91
+ demo.launch(debug=True)