from flask import Flask, render_template, request, jsonify import os 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 googletrans import Translator app = Flask(__name__) # Azure API credentials token = "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" endpoint = "https://models.inference.ai.azure.com" model_name = "Llama-3.2-90B-Vision-Instruct" # Initialize the ChatCompletionsClient client = ChatCompletionsClient( endpoint=endpoint, credential=AzureKeyCredential(token), ) translator = Translator() # Analyze leaf disease def analyze_leaf_disease(image_path): try: # Prepare and send the request to the Azure API response = client.complete( messages=[ SystemMessage( content="You are a helpful assistant that describes leaf disease in detail." ), UserMessage( content=[ TextContentItem(text="What's the leaf disease in this image?"), ImageContentItem( image_url=ImageUrl.load( image_file=image_path, image_format="jpg", detail=ImageDetailLevel.LOW, ) ), ], ), ], model=model_name, ) # Extract and return the response content return response.choices[0].message.content except Exception as e: return f"An error occurred: {e}" @app.route('/') def index(): return render_template('index.html') @app.route('/analyze', methods=['POST']) def analyze(): if 'image' not in request.files: return jsonify({"error": "No file uploaded"}), 400 file = request.files['image'] leaf_type = request.form.get('leaf_type') if file: file_path = os.path.join('uploads', file.filename) file.save(file_path) # Analyze the image result = analyze_leaf_disease(file_path) # Remove the uploaded file os.remove(file_path) return jsonify({"result": result}) @app.route('/translate', methods=['POST']) def translate(): data = request.get_json() text = data.get('text') if not text: return jsonify({"error": "No text provided"}), 400 translated = translator.translate(text, src='en', dest='bn') return jsonify({"translated_text": translated.text}) if __name__ == '__main__': if not os.path.exists('uploads'): os.makedirs('uploads') app.run(debug=True)