Spaces:
Sleeping
Sleeping
Neurolingua
commited on
Commit
•
a136ebd
1
Parent(s):
14536ce
Update other_function.py
Browse files- 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 |
-
|
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 |
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}")
|