Neurolingua commited on
Commit
a136ebd
1 Parent(s): 14536ce

Update other_function.py

Browse files
Files changed (1) hide show
  1. other_function.py +82 -63
other_function.py CHANGED
@@ -1,64 +1,83 @@
1
- import os
2
- import requests
3
- from requests.auth import HTTPBasicAuth
4
- from PIL import Image
5
- from io import BytesIO
6
- from urllib.parse import urlparse
7
- import os
8
-
9
-
10
- from inference_sdk import InferenceHTTPClient
11
- import base64
12
- UPLOAD_FOLDER = '/code/uploads'
13
- if not os.path.exists(UPLOAD_FOLDER):
14
- os.makedirs(UPLOAD_FOLDER)
15
-
16
- def predict_pest(filepath):
17
- CLIENT = InferenceHTTPClient(
18
- api_url="https://detect.roboflow.com",
19
- api_key="oF1aC4b1FBCDtK8CoKx7"
20
- )
21
- result = CLIENT.infer(filepath, model_id="pest-detection-ueoco/1")
22
- return result['predicted_classes'][0]
23
-
24
-
25
- def predict_disease(filepath):
26
- CLIENT = InferenceHTTPClient(
27
- api_url="https://classify.roboflow.com",
28
- api_key="oF1aC4b1FBCDtK8CoKx7"
29
- )
30
- result = CLIENT.infer(filepath, model_id="plant-disease-detection-iefbi/1")
31
- return result['predicted_classes'][0]
32
-
33
- def convert_img(url, account_sid, auth_token):
34
- try:
35
- # Make the request to the media URL with authentication
36
- response = requests.get(url, auth=HTTPBasicAuth(account_sid, auth_token))
37
- response.raise_for_status() # Raise an error for bad responses
38
-
39
- # Determine a filename from the URL
40
- parsed_url = urlparse(url)
41
- media_id = parsed_url.path.split('/')[-1] # Get the last part of the URL path
42
- filename = f"downloaded_media_{media_id}"
43
-
44
- # Save the media content to a file
45
- media_filepath = os.path.join(UPLOAD_FOLDER, filename)
46
- with open(media_filepath, 'wb') as file:
47
- file.write(response.content)
48
-
49
- print(f"Media downloaded successfully and saved as {media_filepath}")
50
-
51
- # Convert the saved media file to an image
52
- with open(media_filepath, 'rb') as img_file:
53
- image = Image.open(img_file)
54
-
55
- # Optionally, convert the image to JPG and save in UPLOAD_FOLDER
56
- converted_filename = f"image.jpg"
57
- converted_filepath = os.path.join(UPLOAD_FOLDER, converted_filename)
58
- image.convert('RGB').save(converted_filepath, 'JPEG')
59
- return converted_filepath
60
-
61
- except requests.exceptions.HTTPError as err:
62
- print(f"HTTP error occurred: {err}")
63
- except Exception as err:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
64
  print(f"An error occurred: {err}")
 
1
+ import os
2
+ import requests
3
+ from requests.auth import HTTPBasicAuth
4
+ from PIL import Image
5
+ from io import BytesIO
6
+ from urllib.parse import urlparse
7
+ import os
8
+ from pypdf import PdfReader
9
+ from ai71 import AI71
10
+ import os
11
+
12
+ from inference_sdk import InferenceHTTPClient
13
+ import base64
14
+ UPLOAD_FOLDER = '/code/uploads'
15
+ if not os.path.exists(UPLOAD_FOLDER):
16
+ os.makedirs(UPLOAD_FOLDER)
17
+
18
+ AI71_API_KEY = os.environ.get('AI71_API_KEY')
19
+ def generate_response(query):
20
+ response = ''
21
+ for chunk in AI71(AI71_API_KEY).chat.completions.create(
22
+ model="tiiuae/falcon-180b-chat",
23
+ messages=[
24
+ {"role": "system", "content": "You are a best agricultural assistant.Remember to give response not more than 2 sentence"},
25
+ {"role": "user",
26
+ "content": f'''Answer the query:{query}'''},
27
+ ],
28
+ stream=True,
29
+ ):
30
+ if chunk.choices[0].delta.content:
31
+ response += chunk.choices[0].delta.content
32
+ return response.replace("###", '').replace('\nUser:','')
33
+
34
+
35
+ def predict_pest(filepath):
36
+ CLIENT = InferenceHTTPClient(
37
+ api_url="https://detect.roboflow.com",
38
+ api_key="oF1aC4b1FBCDtK8CoKx7"
39
+ )
40
+ result = CLIENT.infer(filepath, model_id="pest-detection-ueoco/1")
41
+ return result['predicted_classes'][0]
42
+
43
+
44
+ def predict_disease(filepath):
45
+ CLIENT = InferenceHTTPClient(
46
+ api_url="https://classify.roboflow.com",
47
+ api_key="oF1aC4b1FBCDtK8CoKx7"
48
+ )
49
+ result = CLIENT.infer(filepath, model_id="plant-disease-detection-iefbi/1")
50
+ return result['predicted_classes'][0]
51
+
52
+ def convert_img(url, account_sid, auth_token):
53
+ try:
54
+ # Make the request to the media URL with authentication
55
+ response = requests.get(url, auth=HTTPBasicAuth(account_sid, auth_token))
56
+ response.raise_for_status() # Raise an error for bad responses
57
+
58
+ # Determine a filename from the URL
59
+ parsed_url = urlparse(url)
60
+ media_id = parsed_url.path.split('/')[-1] # Get the last part of the URL path
61
+ filename = f"downloaded_media_{media_id}"
62
+
63
+ # Save the media content to a file
64
+ media_filepath = os.path.join(UPLOAD_FOLDER, filename)
65
+ with open(media_filepath, 'wb') as file:
66
+ file.write(response.content)
67
+
68
+ print(f"Media downloaded successfully and saved as {media_filepath}")
69
+
70
+ # Convert the saved media file to an image
71
+ with open(media_filepath, 'rb') as img_file:
72
+ image = Image.open(img_file)
73
+
74
+ # Optionally, convert the image to JPG and save in UPLOAD_FOLDER
75
+ converted_filename = f"image.jpg"
76
+ converted_filepath = os.path.join(UPLOAD_FOLDER, converted_filename)
77
+ image.convert('RGB').save(converted_filepath, 'JPEG')
78
+ return converted_filepath
79
+
80
+ except requests.exceptions.HTTPError as err:
81
+ print(f"HTTP error occurred: {err}")
82
+ except Exception as err:
83
  print(f"An error occurred: {err}")