File size: 5,692 Bytes
a136ebd
bebbf0f
a136ebd
 
 
 
393577d
a136ebd
 
e251ddd
a136ebd
 
 
09c50ed
 
 
 
 
 
 
a136ebd
 
 
 
 
 
 
 
6fa1c6b
a136ebd
 
 
 
def2d74
a136ebd
6fa1c6b
a136ebd
 
 
 
 
 
7d56735
e6d5541
 
 
7d56735
 
e6d5541
 
 
7d56735
 
e6d5541
a136ebd
 
 
 
 
 
e9ec5c3
a136ebd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bebbf0f
 
 
 
 
 
 
 
 
 
a85a9a9
c74b0db
5323742
c74b0db
 
5323742
 
c74b0db
 
 
5323742
 
c74b0db
5323742
c74b0db
5323742
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
from bs4 import BeautifulSoup
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

from pypdf import PdfReader
from ai71 import AI71
import os
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import pandas as pd

from inference_sdk import InferenceHTTPClient
import base64
UPLOAD_FOLDER = '/code/uploads'
if not os.path.exists(UPLOAD_FOLDER):
    os.makedirs(UPLOAD_FOLDER)

AI71_API_KEY = os.environ.get('AI71_API_KEY')
def generate_response(query,chat_history):
    response = ''
    for chunk in AI71(AI71_API_KEY).chat.completions.create(
            model="tiiuae/falcon-180b-chat",
            messages=[
                {"role": "system", "content": "You are a best agricultural assistant.Remember to give response not more than 2 sentence.Greet the user if user greets you."},
                {"role": "user",
                 "content": f'''Answer the query based on history {chat_history}:{query}'''},
            ],
            stream=True,
    ):
        if chunk.choices[0].delta.content:
            response += chunk.choices[0].delta.content
    return response.replace("###", '').replace('\nUser:','')
class ConversationBufferMemory:
    def __init__(self, max_size=6):
        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):
    CLIENT = InferenceHTTPClient(
        api_url="https://detect.roboflow.com",
        api_key="oF1aC4b1FBCDtK8CoKx7"
    )
    result = CLIENT.infer(filepath, model_id="pest-detection-ueoco/1")
    return result['predictions'][0]
    

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

def convert_img(url, account_sid, auth_token):
    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"downloaded_media_{media_id}"

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

        # Convert the saved media file to an image
        with open(media_filepath, 'rb') as img_file:
            image = Image.open(img_file)

            # Optionally, convert the image to JPG and save in UPLOAD_FOLDER
            converted_filename = f"image.jpg"
            converted_filepath = os.path.join(UPLOAD_FOLDER, converted_filename)
            image.convert('RGB').save(converted_filepath, 'JPEG')
            return converted_filepath

    except requests.exceptions.HTTPError as err:
        print(f"HTTP error occurred: {err}")
    except Exception as err:
        print(f"An error occurred: {err}")
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')
  temperature=soup.find('div',attrs={'class':'BNeawe iBp4i AP7Wnd'}).text
  degree=temperature[:-2]
  celcius=str(round((int(degree) - 32)* 5/9,1))+temperature[-2]+'C'
  return (celcius)
import scrapy
from scrapy.crawler import CrawlerProcess
import pandas as pd

class RatesSpider(scrapy.Spider):
    name = "rates_spider"
    start_urls = ['https://www.kisandeals.com/mandiprices/ALL/TAMIL-NADU/ALL']

    def parse(self, response):
        # Extract the table data
        table_rows = response.xpath('//table/tbody/tr')

        # Initialize a list to hold the data
        data = []

        for row in table_rows:
            # Extract the commodity name and price per kg
            commodity_name = row.xpath('td[1]//text()').get().strip()
            price_per_kg = row.xpath('td[2]//text()').get().strip()
            
            # Append the data to the list
            data.append((commodity_name, price_per_kg))
        
        # Convert the data to a Pandas DataFrame
        df = pd.DataFrame(data, columns=['Commodity', 'Price per kg'])
        # Convert the DataFrame to a dictionary
        rate_dict = df.set_index('Commodity')['Price per kg'].to_dict()

        # Return the scraped rates
        return rate_dict+' This is prices for 1 kg'

def get_rates():
    process = CrawlerProcess({
        'USER_AGENT': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:89.0) Gecko/20100101 Firefox/89.0',
        'LOG_LEVEL': 'ERROR',  # Suppress logging to keep the output clean
    })

    spider = RatesSpider()
    process.crawl(spider)
    process.start()  # This will block until the spider is done

    # Get the scraped data from the spider
    return spider.parse(None)