Spaces:
Sleeping
Sleeping
SarowarSaurav
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -1,5 +1,4 @@
|
|
1 |
-
|
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 = "
|
21 |
|
22 |
# Initialize the ChatCompletionsClient
|
23 |
client = ChatCompletionsClient(
|
@@ -25,20 +21,18 @@ client = ChatCompletionsClient(
|
|
25 |
credential=AzureKeyCredential(token),
|
26 |
)
|
27 |
|
28 |
-
|
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
|
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 |
-
|
62 |
-
def
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
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 |
-
|
91 |
-
|
|
|
92 |
|
93 |
-
|
94 |
-
|
95 |
|
96 |
-
if __name__ ==
|
97 |
-
|
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()
|
|
|
|