File size: 2,830 Bytes
1db8eb2
 
95f5acf
 
 
 
 
 
 
 
 
 
1db8eb2
 
 
3051b4e
95f5acf
 
 
 
a933af8
95f5acf
 
 
 
 
3051b4e
1db8eb2
 
 
26043fa
3051b4e
95f5acf
 
3051b4e
95f5acf
 
 
 
 
 
 
 
26043fa
95f5acf
 
 
 
 
 
3051b4e
95f5acf
3051b4e
 
95f5acf
 
a933af8
 
95f5acf
3051b4e
1db8eb2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3051b4e
1db8eb2
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
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)