SarowarSaurav commited on
Commit
86886cf
·
verified ·
1 Parent(s): f28ee54

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -48
app.py CHANGED
@@ -1,5 +1,4 @@
1
- from flask import Flask, render_template, request, jsonify
2
- import os
3
  from azure.ai.inference import ChatCompletionsClient
4
  from azure.ai.inference.models import (
5
  SystemMessage,
@@ -10,14 +9,11 @@ from azure.ai.inference.models import (
10
  ImageDetailLevel,
11
  )
12
  from azure.core.credentials import AzureKeyCredential
13
- from googletrans import Translator
14
-
15
- app = Flask(__name__)
16
 
17
  # Azure API credentials
18
  token = "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"
19
  endpoint = "https://models.inference.ai.azure.com"
20
- model_name = "Llama-3.2-90B-Vision-Instruct"
21
 
22
  # Initialize the ChatCompletionsClient
23
  client = ChatCompletionsClient(
@@ -25,20 +21,18 @@ client = ChatCompletionsClient(
25
  credential=AzureKeyCredential(token),
26
  )
27
 
28
- translator = Translator()
29
-
30
- # Analyze leaf disease
31
- def analyze_leaf_disease(image_path):
32
  try:
33
  # Prepare and send the request to the Azure API
34
  response = client.complete(
35
  messages=[
36
  SystemMessage(
37
- content="You are a helpful assistant that describes leaf disease in detail."
38
  ),
39
  UserMessage(
40
  content=[
41
- TextContentItem(text="What's the leaf disease in this image?"),
42
  ImageContentItem(
43
  image_url=ImageUrl.load(
44
  image_file=image_path,
@@ -58,42 +52,35 @@ def analyze_leaf_disease(image_path):
58
  except Exception as e:
59
  return f"An error occurred: {e}"
60
 
61
- @app.route('/')
62
- def index():
63
- return render_template('index.html')
64
-
65
- @app.route('/analyze', methods=['POST'])
66
- def analyze():
67
- if 'image' not in request.files:
68
- return jsonify({"error": "No file uploaded"}), 400
69
-
70
- file = request.files['image']
71
- leaf_type = request.form.get('leaf_type')
72
-
73
- if file:
74
- file_path = os.path.join('uploads', file.filename)
75
- file.save(file_path)
76
-
77
- # Analyze the image
78
- result = analyze_leaf_disease(file_path)
79
-
80
- # Remove the uploaded file
81
- os.remove(file_path)
82
-
83
- return jsonify({"result": result})
84
-
85
- @app.route('/translate', methods=['POST'])
86
- def translate():
87
- data = request.get_json()
88
- text = data.get('text')
89
 
90
- if not text:
91
- return jsonify({"error": "No text provided"}), 400
 
92
 
93
- translated = translator.translate(text, src='en', dest='bn')
94
- return jsonify({"translated_text": translated.text})
95
 
96
- if __name__ == '__main__':
97
- if not os.path.exists('uploads'):
98
- os.makedirs('uploads')
99
- app.run(debug=True)
 
1
+ import gradio as gr
 
2
  from azure.ai.inference import ChatCompletionsClient
3
  from azure.ai.inference.models import (
4
  SystemMessage,
 
9
  ImageDetailLevel,
10
  )
11
  from azure.core.credentials import AzureKeyCredential
 
 
 
12
 
13
  # Azure API credentials
14
  token = "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"
15
  endpoint = "https://models.inference.ai.azure.com"
16
+ model_name = "gpt-4o"
17
 
18
  # Initialize the ChatCompletionsClient
19
  client = ChatCompletionsClient(
 
21
  credential=AzureKeyCredential(token),
22
  )
23
 
24
+ # Define the function to handle the image and get predictions
25
+ def analyze_leaf_disease(image_path, leaf_type):
 
 
26
  try:
27
  # Prepare and send the request to the Azure API
28
  response = client.complete(
29
  messages=[
30
  SystemMessage(
31
+ content=f"You are a subject matter expert that describes leaf disease in detail for {leaf_type} leaves."
32
  ),
33
  UserMessage(
34
  content=[
35
+ TextContentItem(text="What's the name of the leaf disease in this image and what is the confidence score? What is the probable reason? What are the medicine or stops to prevent the disease"),
36
  ImageContentItem(
37
  image_url=ImageUrl.load(
38
  image_file=image_path,
 
52
  except Exception as e:
53
  return f"An error occurred: {e}"
54
 
55
+ # Define the Gradio interface
56
+ def handle_proceed(image_path, leaf_type):
57
+ # Display detecting status
58
+ detecting_status = "Detecting..."
59
+ result = analyze_leaf_disease(image_path, leaf_type)
60
+ # Clear detecting status after processing
61
+ return "", result
62
+
63
+ with gr.Blocks() as interface:
64
+ with gr.Row():
65
+ gr.Markdown("""
66
+ # Leaf Disease Detector
67
+ Upload a leaf image, select the leaf type, and let the AI analyze the disease.
68
+ """)
69
+
70
+ with gr.Row():
71
+ image_input = gr.Image(type="filepath", label="Upload an Image or Take a Photo")
72
+ leaf_type = gr.Dropdown(
73
+ choices=["Tomato", "Tobacco", "Corn", "Paddy", "Maze", "Potato", "Wheat"],
74
+ label="Select Leaf Type",
75
+ )
76
+ proceed_button = gr.Button("Proceed")
 
 
 
 
 
 
77
 
78
+ with gr.Row():
79
+ detecting_label = gr.Label("Detecting...", visible=False)
80
+ output_box = gr.Textbox(label="Results", placeholder="Results will appear here.")
81
 
82
+ # Update the detecting_label and result in outputs
83
+ proceed_button.click(handle_proceed, inputs=[image_input, leaf_type], outputs=[detecting_label, output_box])
84
 
85
+ if __name__ == "__main__":
86
+ interface.launch()