SarowarSaurav commited on
Commit
3a33882
Β·
verified Β·
1 Parent(s): 4103ef6

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +108 -0
app.py ADDED
@@ -0,0 +1,108 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from azure.ai.inference import ChatCompletionsClient
3
+ from azure.ai.inference.models import (
4
+ SystemMessage,
5
+ UserMessage,
6
+ TextContentItem,
7
+ ImageContentItem,
8
+ ImageUrl,
9
+ ImageDetailLevel,
10
+ )
11
+ from azure.core.credentials import AzureKeyCredential
12
+ from gtts import gTTS
13
+ from deep_translator import GoogleTranslator
14
+ import os
15
+
16
+ # βœ… Securely load Azure credentials from environment
17
+ token = os.getenv("AZURE_API_KEY")
18
+ endpoint = os.getenv("AZURE_ENDPOINT")
19
+ model_name = os.getenv("AZURE_MODEL_NAME", "gpt-4o") # Optional: use secret or default to gpt-4o
20
+
21
+ # βœ… Validate credentials
22
+ if not (isinstance(token, str) and token.strip()) or not (isinstance(endpoint, str) and endpoint.strip()):
23
+ raise ValueError("Azure API credentials are missing. Please set AZURE_API_KEY and AZURE_ENDPOINT in Hugging Face secrets.")
24
+
25
+ # βœ… Azure Client
26
+ client = ChatCompletionsClient(
27
+ endpoint=endpoint,
28
+ credential=AzureKeyCredential(token),
29
+ )
30
+
31
+ # πŸ” Analyze disease
32
+ def analyze_leaf_disease(image_path, leaf_type):
33
+ try:
34
+ response = client.complete(
35
+ messages=[
36
+ SystemMessage(
37
+ content=f"You are a subject matter expert that describes leaf disease in detail for {leaf_type} leaves."
38
+ ),
39
+ UserMessage(
40
+ content=[
41
+ 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"),
42
+ ImageContentItem(
43
+ image_url=ImageUrl.load(
44
+ image_file=image_path,
45
+ image_format="jpg",
46
+ detail=ImageDetailLevel.LOW,
47
+ )
48
+ ),
49
+ ],
50
+ ),
51
+ ],
52
+ model=model_name,
53
+ )
54
+ return response.choices[0].message.content
55
+ except Exception as e:
56
+ return f"❌ Error: {e}"
57
+
58
+ # 🌐 Translate to Bangla
59
+ def translate_to_bangla(text):
60
+ try:
61
+ return GoogleTranslator(source="auto", target="bn").translate(text)
62
+ except Exception as e:
63
+ return f"❌ Translation error: {e}"
64
+
65
+ # πŸ”Š Text to Speech
66
+ def text_to_speech(text):
67
+ try:
68
+ tts = gTTS(text)
69
+ audio_file = "tts_output.mp3"
70
+ tts.save(audio_file)
71
+ return audio_file
72
+ except Exception as e:
73
+ return f"❌ TTS error: {e}"
74
+
75
+ # πŸš€ Main Action
76
+ def handle_proceed(image_path, leaf_type):
77
+ return "", analyze_leaf_disease(image_path, leaf_type)
78
+
79
+ # 🌿 Gradio App
80
+ with gr.Blocks() as interface:
81
+ gr.Markdown("# πŸƒ Leaf Disease Detector\nUpload an image, select the leaf type, and analyze the disease. Listen or translate the result.")
82
+
83
+ with gr.Row():
84
+ image_input = gr.Image(type="filepath", label="πŸ“Έ Upload Leaf Image")
85
+ leaf_type = gr.Dropdown(
86
+ choices=["Tomato", "Tobacco", "Corn", "Paddy", "Maze", "Potato", "Wheat"],
87
+ label="🌿 Select Leaf Type",
88
+ )
89
+ proceed_button = gr.Button("πŸ” Analyze")
90
+
91
+ with gr.Row():
92
+ detecting_label = gr.Label("Detecting...", visible=False)
93
+ output_box = gr.Textbox(label="πŸ“‹ Result", placeholder="Analysis will appear here", lines=10)
94
+
95
+ with gr.Row():
96
+ tts_button = gr.Button("πŸ”Š Read Aloud")
97
+ tts_audio = gr.Audio(label="🎧 Audio", autoplay=True)
98
+
99
+ translate_button = gr.Button("🌐 Translate to Bangla")
100
+ translated_output = gr.Textbox(label="πŸ“˜ Bangla Translation", placeholder="Translation will appear here", lines=10)
101
+
102
+ # Button logic
103
+ proceed_button.click(handle_proceed, inputs=[image_input, leaf_type], outputs=[detecting_label, output_box])
104
+ tts_button.click(text_to_speech, inputs=[output_box], outputs=[tts_audio])
105
+ translate_button.click(translate_to_bangla, inputs=[output_box], outputs=[translated_output])
106
+
107
+ if __name__ == "__main__":
108
+ interface.launch()