Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| from azure.ai.inference import ChatCompletionsClient | |
| from azure.ai.inference.models import ( | |
| SystemMessage, | |
| UserMessage, | |
| TextContentItem, | |
| ImageContentItem, | |
| ImageUrl, | |
| ImageDetailLevel, | |
| ) | |
| from azure.core.credentials import AzureKeyCredential | |
| from gtts import gTTS | |
| from deep_translator import GoogleTranslator | |
| import os | |
| # β Securely load Azure credentials from environment | |
| # Azure API credentials | |
| token = "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" | |
| endpoint = "https://models.inference.ai.azure.com" | |
| model_name = "gpt-4o" # Optional: use secret or default to gpt-4o | |
| # β Validate credentials | |
| if not (isinstance(token, str) and token.strip()) or not (isinstance(endpoint, str) and endpoint.strip()): | |
| raise ValueError("Azure API credentials are missing. Please set AZURE_API_KEY and AZURE_ENDPOINT in Hugging Face secrets.") | |
| # β Azure Client | |
| client = ChatCompletionsClient( | |
| endpoint=endpoint, | |
| credential=AzureKeyCredential(token), | |
| ) | |
| # π Analyze disease | |
| def analyze_leaf_disease(image_path, leaf_type): | |
| try: | |
| response = client.complete( | |
| messages=[ | |
| SystemMessage( | |
| content=f"You are a subject matter expert that describes leaf disease in detail for {leaf_type} leaves." | |
| ), | |
| UserMessage( | |
| content=[ | |
| TextContentItem(text="What's the name of the leaf disease in this image? what is the confidence Score only?. What is the probable reason? What are the medicine or stops to prevent the disease"), | |
| ImageContentItem( | |
| image_url=ImageUrl.load( | |
| image_file=image_path, | |
| image_format="jpg", | |
| detail=ImageDetailLevel.LOW, | |
| ) | |
| ), | |
| ], | |
| ), | |
| ], | |
| model=model_name, | |
| ) | |
| return response.choices[0].message.content | |
| except Exception as e: | |
| return f"β Error: {e}" | |
| # π Translate to Bangla | |
| def translate_to_bangla(text): | |
| try: | |
| return GoogleTranslator(source="auto", target="bn").translate(text) | |
| except Exception as e: | |
| return f"β Translation error: {e}" | |
| # π Text to Speech | |
| def text_to_speech(text): | |
| try: | |
| tts = gTTS(text) | |
| audio_file = "tts_output.mp3" | |
| tts.save(audio_file) | |
| return audio_file | |
| except Exception as e: | |
| return f"β TTS error: {e}" | |
| # π Main Action | |
| def handle_proceed(image_path, leaf_type): | |
| return "", analyze_leaf_disease(image_path, leaf_type) | |
| # πΏ Gradio App | |
| with gr.Blocks() as interface: | |
| gr.Markdown("# π Leaf Disease Detector\nUpload an image, select the leaf type, and analyze the disease. Listen or translate the result.") | |
| with gr.Row(): | |
| image_input = gr.Image(type="filepath", label="πΈ Upload Leaf Image") | |
| leaf_type = gr.Dropdown( | |
| choices=["Tomato", "Tobacco", "Corn", "Paddy", "Maze", "Potato", "Wheat"], | |
| label="πΏ Select Leaf Type", | |
| ) | |
| proceed_button = gr.Button("π Analyze") | |
| with gr.Row(): | |
| detecting_label = gr.Label("Detecting...", visible=False) | |
| output_box = gr.Textbox(label="π Result", placeholder="Analysis will appear here", lines=10) | |
| with gr.Row(): | |
| tts_button = gr.Button("π Read Aloud") | |
| translate_button = gr.Button("π Translate to Bangla") | |
| with gr.Row(): | |
| tts_audio = gr.Audio(label="π§ Audio", autoplay=True) | |
| translated_output = gr.Textbox(label="π Bangla Translation", placeholder="Translation will appear here", lines=10) | |
| # Button logic | |
| proceed_button.click(handle_proceed, inputs=[image_input, leaf_type], outputs=[detecting_label, output_box]) | |
| tts_button.click(text_to_speech, inputs=[output_box], outputs=[tts_audio]) | |
| translate_button.click(translate_to_bangla, inputs=[output_box], outputs=[translated_output]) | |
| if __name__ == "__main__": | |
| interface.launch() | |