File size: 8,500 Bytes
a136ebd
bebbf0f
bbe5a2f
 
a136ebd
 
 
 
393577d
a136ebd
 
29191e5
 
 
 
 
a136ebd
 
 
dc30935
09c50ed
bbe5a2f
 
 
 
 
29191e5
 
 
 
a136ebd
29191e5
 
 
 
a136ebd
 
 
 
 
 
dc30935
a136ebd
6fa1c6b
bbe5a2f
 
 
 
 
 
 
 
 
 
 
7d56735
b789611
e6d5541
 
7d56735
 
e6d5541
 
 
7d56735
 
e6d5541
a136ebd
8cc48a4
 
 
 
 
 
a4684f0
 
 
 
8cc48a4
 
a136ebd
 
 
8cc48a4
 
a136ebd
 
 
8cc48a4
0fca847
 
 
 
8cc48a4
 
a136ebd
4746643
a136ebd
bcba21f
a136ebd
439aaf4
a136ebd
 
 
439aaf4
a136ebd
4746643
a136ebd
4746643
 
 
a136ebd
 
4746643
 
a136ebd
bcba21f
595a477
a136ebd
bebbf0f
 
 
 
 
 
880b9c8
e5ecf3c
880b9c8
4d5b15d
 
e5ecf3c
 
 
 
 
4d5b15d
e5ecf3c
 
4d5b15d
e5ecf3c
 
c74b0db
e5ecf3c
 
 
 
c74b0db
e5ecf3c
 
28bae9c
 
e5ecf3c
 
 
 
 
 
 
 
 
 
 
4d5b15d
d56ec24
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
65a48f9
 
 
 
 
1ad5761
65a48f9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1ad5761
 
b789611
 
 
 
 
dc30935
 
 
 
 
 
 
 
 
 
 
 
 
2b0bff4
 
dc30935
 
 
 
3a352e4
dc30935
 
 
 
 
 
760dcb4
 
 
 
 
 
 
 
8b28c8c
8d9e442
760dcb4
 
 
 
 
 
 
 
 
 
 
 
 
 
8b28c8c
4f00765
760dcb4
 
 
 
 
dc30935
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
import os
from bs4 import BeautifulSoup
import os
from mistralai import Mistral
import requests
from requests.auth import HTTPBasicAuth
from PIL import Image
from io import BytesIO
import pandas as pd
from urllib.parse import urlparse
import os
import cv2
import numpy as np
import pytesseract
import subprocess
from PIL import Image
from pypdf import PdfReader
from ai71 import AI71
import os
import PyPDF2
import pandas as pd
model = "mistral-large-latest"
api_key='xQ2Zhfsp4cLar4lvBRDWZKljvp0Ej427'

client = Mistral(api_key=api_key)

def extract_text_from_image(image_path):
    img = cv2.imread(image_path)
    if img is None:
        raise ValueError("Image not found or unable to load")

    img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    text = pytesseract.image_to_string(img_rgb)
    
    return text
from inference_sdk import InferenceHTTPClient
import base64
UPLOAD_FOLDER = '/code/uploads'
if not os.path.exists(UPLOAD_FOLDER):
    os.makedirs(UPLOAD_FOLDER)

pdf_text=''
AI71_API_KEY = os.environ.get('AI71_API_KEY')
def generate_response(query,chat_history):
    
    chat_response = client.chat.complete(
        model= model,
        messages = [
            {
                "role": "user",
                "content": f"{User_querry}? provide response within 2 sentence",
            },
        ]
    )
    return chat_response.choices[0].message.content
class ConversationBufferMemory:
    def __init__(self, max_size):
        self.memory = []
        self.max_size = max_size

    def add_to_memory(self, interaction):
        self.memory.append(interaction)
        if len(self.memory) > self.max_size:
            self.memory.pop(0)  # Remove the oldest interaction

    def get_memory(self):
        return self.memory
def predict_pest(filepath):
    try:
        CLIENT = InferenceHTTPClient(
            api_url="https://detect.roboflow.com",
            api_key="oF1aC4b1FBCDtK8CoKx7"
        )
        result = CLIENT.infer(filepath, model_id="pest-detection-ueoco/1")
        a= result['predictions'][0]
        if a=='x':
            return 'APHIDS'
        return a
    except:
        return None
    

def predict_disease(filepath):
    try:
        CLIENT = InferenceHTTPClient(
        api_url="https://classify.roboflow.com",
        api_key="oF1aC4b1FBCDtK8CoKx7"
    )
        result = CLIENT.infer(filepath, model_id="plant-disease-detection-iefbi/1")
        a= result['predicted_classes'][0]
        if a=='x':
            return 'APHIDS'
        return a
    except:
        return None


def convert_img(url, account_sid, auth_token):
    if 1==1:
        # Make the request to the media URL with authentication
        response = requests.get(url.replace(' ',''), auth=HTTPBasicAuth(account_sid, auth_token))
        response.raise_for_status()  # Raise an error for bad responses

        # Determine a filename from the URL
        parsed_url = urlparse(url.replace(' ',''))
        media_id = parsed_url.path.split('/')[-1]  # Get the last part of the URL path
        filename = f"image.jpg"

        # Save the media content to a .txt file
        txt_filepath = os.path.join(UPLOAD_FOLDER, filename)
        with open(txt_filepath, 'wb') as file:
            file.write(response.content)
        
        print(f"Media downloaded successfully and saved as {txt_filepath}")
        return txt_filepath

    else :
        return 'errir in process none'

def get_weather(city):
  city=city.strip()
  city=city.replace(' ',"+")
  r = requests.get(f'https://www.google.com/search?q=weather+in+{city}')

  soup=BeautifulSoup(r.text,'html.parser')
  temp = soup.find('div', class_='BNeawe iBp4i AP7Wnd').text
  
  return (temp)


from zenrows import ZenRowsClient
from bs4 import BeautifulSoup
Zenrow_api=os.environ.get('Zenrow_api')
# Initialize ZenRows client with your API key
client = ZenRowsClient(str(Zenrow_api))

def get_rates():    # URL to scrape
    url = "https://www.kisandeals.com/mandiprices/ALL/TAMIL-NADU/ALL"

    # Fetch the webpage content using ZenRows
    response = client.get(url)

    # Check if the request was successful
    if response.status_code == 200:
        # Parse the raw HTML content with BeautifulSoup
        soup = BeautifulSoup(response.content, 'html.parser')

        # Find the table rows containing the data
        rows = soup.select('table tbody tr')
        data = {}
        for row in rows:
            # Extract commodity and price using BeautifulSoup
            columns = row.find_all('td')
            if len(columns) >= 2:
                commodity = columns[0].get_text(strip=True)
                price = columns[1].get_text(strip=True)
                if '₹' in price:
                    data[commodity] = price
    return str(data)+" This are the prices for 1 kg"




def get_news(): 
    news=[]   # URL to scrape
    url = "https://economictimes.indiatimes.com/news/economy/agriculture?from=mdr"

    # Fetch the webpage content using ZenRows
    response = client.get(url)

    # Check if the request was successful
    if response.status_code == 200:
        # Parse the raw HTML content with BeautifulSoup
        soup = BeautifulSoup(response.content, 'html.parser')

        # Find the table rows containing the data
        headlines = soup.find_all("div", class_="eachStory")
        for story in headlines:
    # Extract the headline
            headline = story.find('h3').text.strip()
            news.append(headline)
    return news



def download_and_save_as_txt(url, account_sid, auth_token):
    global pdf_text
    try:
        # Make the request to the media URL with authentication
        response = requests.get(url, auth=HTTPBasicAuth(account_sid, auth_token))
        response.raise_for_status()  # Raise an error for bad responses

        # Determine a filename from the URL
        parsed_url = urlparse(url)
        media_id = parsed_url.path.split('/')[-1]  # Get the last part of the URL path
        filename = f"pdf_file.pdf"

        # Save the media content to a .txt file
        txt_filepath = os.path.join(UPLOAD_FOLDER, filename)
        with open(txt_filepath, 'wb') as file:
            file.write(response.content)
        
        print(f"Media downloaded successfully and saved as {txt_filepath}")
        pdf_text=extract_text_from_pdf(txt_filepath)
        return txt_filepath

    except requests.exceptions.HTTPError as err:
        print(f"HTTP error occurred: {err}")
    except Exception as err:
        print(f"An error occurred: {err}")


def extract_text_from_pdf(pdf_path):
    global pdf_text
    with open(pdf_path, 'rb') as file:
        reader = PyPDF2.PdfReader(file)
        pdf_text = ''
        for page_num in range(len(reader.pages)):
            page = reader.pages[page_num]
            pdf_text += page.extract_text()
        return pdf_text


def respond_pdf(query):
    extracted_text=pdf_text
    res = ''
    for chunk in AI71(AI71_API_KEY).chat.completions.create(
        model="tiiuae/falcon-11b",
        messages=[
            {"role": "system", "content": "You are a  pdf answering assistant and you have a pdf as a data."},
            {"role": "user", "content": f"Content:{extracted_text},Query:{query}"},
        ],
        stream=True,
    ):
        if chunk.choices[0].delta.content:
            res += chunk.choices[0].delta.content
    return ( res.replace("User:",'').strip())


def booktask(data):
    res = ''
    for chunk in AI71(AI71_API_KEY).chat.completions.create(
        model="tiiuae/falcon-11b",
        messages=[
            {"role": "system", "content": "You are an assistant."},
            {"role": "user", "content": f"My bookkeeping data is {data}.Provide the data in points."},
        ],
        stream=True,
    ):
        if chunk.choices[0].delta.content:
            res += chunk.choices[0].delta.content
    return ( res.replace("User:",'').strip())



def return_bookdata(querry,data):
    res = ''
    for chunk in AI71(AI71_API_KEY).chat.completions.create(
        model="tiiuae/falcon-11b",
        messages=[
            {"role": "system", "content": "You are an assistant."},
            {"role": "user", "content": f"My notes data is {data}.user:{querry.replace('bookkeeping','data')}.Give the format of bookkeeping data in points.Make your response very concise to maximum of 10 points"},
        ],
        stream=True,
    ):
        if chunk.choices[0].delta.content:
            res += chunk.choices[0].delta.content
    return ( res.replace("User:",'').strip())