File size: 6,915 Bytes
c2ed2b0
 
 
 
 
 
 
 
 
 
 
e65c47f
 
c2ed2b0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c7a0f34
c2ed2b0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
from flask import Flask, render_template, request, jsonify, send_from_directory
import pymysql
import matplotlib.pyplot as plt
import io
import base64
from gtts import gTTS
import os
import random
from ai71 import AI71

app = Flask(__name__)
'''db = pymysql.connect(host="localhost", user="root", password="1234", database="tea_farm")
cursor = db.cursor()'''

UPLOAD_FOLDER = 'uploads'
OUTPUT_DIRECTORY = 'audio'
if not os.path.exists(UPLOAD_FOLDER):
    os.makedirs(UPLOAD_FOLDER)
if not os.path.exists(OUTPUT_DIRECTORY):
    os.makedirs(OUTPUT_DIRECTORY)

# Text for pest and disease
pest_text = '''tea mosquito bugs,to get rid,spray with neem oil and garlic,use sticky traps, and trap crops like castor . Expect results in 2 to 4 weeks.চা মশার বাগ, পরিত্রাণ পেতে, নিম তেল এবং রসুন দিয়ে স্প্রে করুন, আঠালো ফাঁদ ব্যবহার করুন এবং ক্যাস্টরের মতো ফসল ফাঁদ করুন। 2-4 সপ্তাহের মধ্যে ফলাফল আশা করুন।'''
disease_text = '''Detected Disease: Brown Blight is a fungal disease To treat it spray a mix of baking soda, water and apply organic mulch to keep soil healthy.ব্রাউন ব্লাইট একটি ছত্রাকজনিত রোগের চিকিৎসার জন্য বেকিং সোডা, পানির মিশ্রণ স্প্রে করুন এবং মাটি সুস্থ রাখতে জৈব মালচ প্রয়োগ করুন'''

@app.route('/home')
def home():
    return render_template('index.html')

@app.route('/notification')
def notification():
    return render_template('notification.html')

@app.route('/', methods=['GET', 'POST'])
def upload_file():
    pest_filename = None
    disease_filename = None
    pest_audio_file = "pest.mp3"
    disease_audio_file = "disease.mp3"

    if request.method == 'POST':
        if 'pest_file' in request.files:
            pest_file = request.files['pest_file']
            if pest_file.filename != '':
                pest_filename = pest_file.filename
                pest_file.save(os.path.join(UPLOAD_FOLDER, pest_filename))
                tts_pest = gTTS(text=pest_text, lang='en', slow=False)
                tts_pest.save(os.path.join(OUTPUT_DIRECTORY, pest_audio_file))

        if 'disease_file' in request.files:
            disease_file = request.files['disease_file']
            if disease_file.filename != '':
                disease_filename = disease_file.filename
                disease_file.save(os.path.join(UPLOAD_FOLDER, disease_filename))
                tts_disease = gTTS(text=disease_text, lang='en', slow=False)
                tts_disease.save(os.path.join(OUTPUT_DIRECTORY, disease_audio_file))

    return render_template('index.html', pest_filename=pest_filename, disease_filename=disease_filename,
                           pest_audio_file=pest_audio_file, disease_audio_file=disease_audio_file)

AI71_API_KEY = "api71-api-20725a9d-46d6-4baf-9e26-abfca35ab242"
@app.route('/chat', methods=['POST'])
def chat():
    message = request.json['message']
    ai71 = AI71(AI71_API_KEY)
    response = ""
    for chunk in ai71.chat.completions.create(
        model="tiiuae/falcon-180b-chat",
        messages=[
            {"role": "system", "content": "You are a helpful assistant for a farming system.Guide the user properly to increase the yield."},
            {"role": "user", "content": message},
        ],
        stream=True,
    ):
        if chunk.choices[0].delta.content:
            response += chunk.choices[0].delta.content
    return jsonify({'response': response.replace('\n','<br>')})
@app.route('/uploads/<filename>')
def uploaded_file(filename):
    return send_from_directory(UPLOAD_FOLDER, filename)

@app.route('/audio/<filename>')
def audio_file(filename):
    return send_from_directory(OUTPUT_DIRECTORY, filename)

@app.route('/sensors')
def sensors():
    # Fetch a random row from the database
    sensor_data=False

    if sensor_data:
        sensor_dict = {
            'id': sensor_data[0],
            'temperature': sensor_data[1],
            'humidity': sensor_data[2],
            'irValue': sensor_data[3],
            'distance': sensor_data[4],
            'timestamp': sensor_data[5]
        }
    else:
        sensor_dict = {
            'temperature': None,
            'humidity': None,
            'irValue': None,
            'distance': None,
            'timestamp': None
        }

    return render_template('sensors.html', sensor_data=sensor_dict)

@app.route('/zones')
def zones():
    return render_template('zones.html')

@app.route('/activity-log')
def activity_log():

    # Fetch data for IR value distribution plot
    cursor.execute("SELECT ir_value FROM sensor_data")
    ir_values = cursor.fetchall()
    count_0 = ir_values.count((0,))
    count_1 = ir_values.count((1,))
    labels = ['Pest Swarm Detected', 'No Pest Swarm Detected']
    sizes = [count_0, count_1]
    plt.figure(figsize=(6, 6))
    plt.pie(sizes, labels=labels, autopct='%1.1f%%')
    plt.title('IR Value Distribution')
    plt.axis('equal')
    ir_buffer = io.BytesIO()
    plt.savefig(ir_buffer, format='png')
    ir_buffer.seek(0)
    ir_plot_data = base64.b64encode(ir_buffer.getvalue()).decode('utf-8')
    plt.close()

    # Fetch data for average closeness of pests plot
    cursor.execute("SELECT timestamp, distance FROM sensor_data WHERE distance < 500")
    data = cursor.fetchall()
    timestamps = [row[0] for row in data]
    distances = [row[1] for row in data]
    average_distance = sum(distances) / len(distances)
    plt.figure(figsize=(10, 6))
    plt.plot(timestamps, distances, marker='o', linestyle='-')
    plt.xlabel('Timestamp')
    plt.ylabel('Distance (cm)')
    plt.title('Average Closeness of Pests over Time')
    plt.xticks(rotation=45)
    plt.grid(True)
    distance_buffer = io.BytesIO()
    plt.savefig(distance_buffer, format='png')
    distance_buffer.seek(0)
    distance_plot_data = base64.b64encode(distance_buffer.getvalue()).decode('utf-8')
    plt.close()

    return render_template('activity-log.html', ir_plot_data=ir_plot_data, distance_plot_data=distance_plot_data)

@app.route('/sensor-data', methods=['POST'])
def update_sensor_data():
    data = request.get_json()
    temperature = data.get('temperature')
    humidity = data.get('humidity')
    ir_value = data.get('irValue')
    distance = data.get('distance')
    sql = "INSERT INTO sensor_data (temperature, humidity, ir_value, distance) VALUES (%s, %s, %s, %s)"
    val = (temperature, humidity, ir_value, distance)
    cursor.execute(sql, val)
    db.commit()
    return jsonify(success=True)

if __name__ == "__main__":

    app.run(host='0.0.0.0', port=7860,debug=1==1)