Spaces:
Sleeping
Sleeping
SarowarSaurav
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -1,4 +1,5 @@
|
|
1 |
-
import
|
|
|
2 |
from azure.ai.inference import ChatCompletionsClient
|
3 |
from azure.ai.inference.models import (
|
4 |
SystemMessage,
|
@@ -9,6 +10,9 @@ from azure.ai.inference.models import (
|
|
9 |
ImageDetailLevel,
|
10 |
)
|
11 |
from azure.core.credentials import AzureKeyCredential
|
|
|
|
|
|
|
12 |
|
13 |
# Azure API credentials
|
14 |
token = "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"
|
@@ -21,7 +25,9 @@ client = ChatCompletionsClient(
|
|
21 |
credential=AzureKeyCredential(token),
|
22 |
)
|
23 |
|
24 |
-
|
|
|
|
|
25 |
def analyze_leaf_disease(image_path):
|
26 |
try:
|
27 |
# Prepare and send the request to the Azure API
|
@@ -52,15 +58,42 @@ def analyze_leaf_disease(image_path):
|
|
52 |
except Exception as e:
|
53 |
return f"An error occurred: {e}"
|
54 |
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
63 |
|
64 |
-
|
65 |
-
if
|
66 |
-
|
|
|
|
1 |
+
from flask import Flask, render_template, request, jsonify
|
2 |
+
import os
|
3 |
from azure.ai.inference import ChatCompletionsClient
|
4 |
from azure.ai.inference.models import (
|
5 |
SystemMessage,
|
|
|
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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"
|
|
|
25 |
credential=AzureKeyCredential(token),
|
26 |
)
|
27 |
|
28 |
+
translator = Translator()
|
29 |
+
|
30 |
+
# Analyze leaf disease
|
31 |
def analyze_leaf_disease(image_path):
|
32 |
try:
|
33 |
# Prepare and send the request to the Azure API
|
|
|
58 |
except Exception as e:
|
59 |
return f"An error occurred: {e}"
|
60 |
|
61 |
+
@app.route('/')
|
62 |
+
def index():
|
63 |
+
return render_template('index.html')
|
64 |
+
|
65 |
+
@app.route('/analyze', methods=['POST'])
|
66 |
+
def analyze():
|
67 |
+
if 'image' not in request.files:
|
68 |
+
return jsonify({"error": "No file uploaded"}), 400
|
69 |
+
|
70 |
+
file = request.files['image']
|
71 |
+
leaf_type = request.form.get('leaf_type')
|
72 |
+
|
73 |
+
if file:
|
74 |
+
file_path = os.path.join('uploads', file.filename)
|
75 |
+
file.save(file_path)
|
76 |
+
|
77 |
+
# Analyze the image
|
78 |
+
result = analyze_leaf_disease(file_path)
|
79 |
+
|
80 |
+
# Remove the uploaded file
|
81 |
+
os.remove(file_path)
|
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 |
+
if not text:
|
91 |
+
return jsonify({"error": "No text provided"}), 400
|
92 |
+
|
93 |
+
translated = translator.translate(text, src='en', dest='bn')
|
94 |
+
return jsonify({"translated_text": translated.text})
|
95 |
|
96 |
+
if __name__ == '__main__':
|
97 |
+
if not os.path.exists('uploads'):
|
98 |
+
os.makedirs('uploads')
|
99 |
+
app.run(debug=True)
|