Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,24 +1,83 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
st.image(plant_info["image_url"], caption=plant_info["name"], width=300)
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
st.error("No plant information found.")
|
| 23 |
-
else:
|
| 24 |
-
st.warning("Please enter a plant name.")
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import requests
|
| 3 |
+
from transformers import pipeline
|
| 4 |
+
|
| 5 |
+
# Initialize the AI models
|
| 6 |
+
qa_pipeline = pipeline("question-answering", model="distilbert-base-cased-distilled-squad", framework="pt") if no more information is avalable in this link then answer no more information available and erase errors in this app like this streamlit.runtime.media_file_storage.MediaFileStorageError: Error opening '/assets/baren_field_square-4a827e5f09156962937eb100e4484f87e1e788f28a7c9daefe2a9297711a562a.jpg'
|
| 7 |
+
Traceback:
|
| 8 |
+
File "/home/user/app/app.py", line 69, in <module>
|
| 9 |
+
main()
|
| 10 |
+
File "/home/user/app/app.py", line 64, in main
|
| 11 |
+
st.image(plant_info["image_url"], caption=plant_info["name"], width=300)
|
| 12 |
+
File "/usr/local/lib/python3.10/site-packages/streamlit/runtime/metrics_util.py", line 409, in wrapped_func
|
| 13 |
+
result = non_optional_func(*args, **kwargs)
|
| 14 |
+
File "/usr/local/lib/python3.10/site-packages/streamlit/elements/image.py", line 172, in image
|
| 15 |
+
marshall_images(
|
| 16 |
+
File "/usr/local/lib/python3.10/site-packages/streamlit/elements/lib/image_utils.py", line 438, in marshall_images
|
| 17 |
+
proto_img.url = image_to_url(
|
| 18 |
+
File "/usr/local/lib/python3.10/site-packages/streamlit/elements/lib/image_utils.py", line 297, in image_to_url
|
| 19 |
+
url = runtime.get_instance().media_file_mgr.add(image, mimetype, image_id)
|
| 20 |
+
File "/usr/local/lib/python3.10/site-packages/streamlit/runtime/media_file_manager.py", line 226, in add
|
| 21 |
+
file_id = self._storage.load_and_get_id(
|
| 22 |
+
File "/usr/local/lib/python3.10/site-packages/streamlit/runtime/memory_media_file_storage.py", line 115, in load_and_get_id
|
| 23 |
+
file_data = self._read_file(path_or_data)
|
| 24 |
+
File "/usr/local/lib/python3.10/site-packages/streamlit/runtime/memory_media_file_storage.py", line 167, in _read_file
|
| 25 |
+
raise MediaFileStorageError(f"Error opening '{filename}'") from extext_generation_pipeline = pipeline("text-generation", model="gpt2", framework="pt")
|
| 26 |
+
|
| 27 |
+
# Function to fetch plant data using OpenFarm API
|
| 28 |
+
def fetch_plant_data(plant_name):
|
| 29 |
+
url = f"https://openfarm.cc/api/v1/crops?filter={plant_name}"
|
| 30 |
+
try:
|
| 31 |
+
response = requests.get(url)
|
| 32 |
+
response.raise_for_status() # Raise an error for bad status codes
|
| 33 |
+
data = response.json()
|
| 34 |
+
if data.get("data"):
|
| 35 |
+
plant_info = data["data"][0]["attributes"]
|
| 36 |
+
return {
|
| 37 |
+
"name": plant_info.get("name", plant_name),
|
| 38 |
+
"description": plant_info.get("description", "No description available."),
|
| 39 |
+
"image_url": plant_info.get("main_image_path", None),
|
| 40 |
+
"sun_requirements": plant_info.get("sun_requirements", "No information available."),
|
| 41 |
+
"watering": plant_info.get("watering", None),
|
| 42 |
+
}
|
| 43 |
+
return None
|
| 44 |
+
except requests.exceptions.RequestException as e:
|
| 45 |
+
st.error(f"Error fetching data: {e}")
|
| 46 |
+
return None
|
| 47 |
+
|
| 48 |
+
# Function to generate watering instructions using AI
|
| 49 |
+
def refine_watering_instructions(plant_name, basic_instructions=None):
|
| 50 |
+
prompt = (
|
| 51 |
+
f"Refine these watering instructions for the plant '{plant_name}': '{basic_instructions}'. "
|
| 52 |
+
"If no specific instructions are available, provide a detailed guideline for watering this plant type."
|
| 53 |
+
)
|
| 54 |
+
result = text_generation_pipeline(prompt, max_length=150, num_return_sequences=1)
|
| 55 |
+
return result[0]["generated_text"]
|
| 56 |
+
|
| 57 |
+
# Streamlit app
|
| 58 |
+
def main():
|
| 59 |
+
st.title("🌱 AI-Powered Plant Care Guide")
|
| 60 |
+
|
| 61 |
+
# Section 1: Search for plant care information
|
| 62 |
+
st.header("Search for Plant Care Information")
|
| 63 |
+
user_input = st.text_input("Enter the name of a plant", "")
|
| 64 |
+
|
| 65 |
+
if st.button("Search"):
|
| 66 |
+
if user_input.strip():
|
| 67 |
+
with st.spinner("Fetching plant data..."):
|
| 68 |
+
plant_info = fetch_plant_data(user_input)
|
| 69 |
+
|
| 70 |
+
if plant_info:
|
| 71 |
+
with st.spinner("Generating watering instructions..."):
|
| 72 |
+
basic_watering = plant_info["watering"] or "No specific instructions available."
|
| 73 |
+
plant_info["watering"] = refine_watering_instructions(user_input, basic_instructions=basic_watering)
|
| 74 |
+
|
| 75 |
+
st.subheader(f"Care Instructions for {plant_info['name']}")
|
| 76 |
+
st.write(f"**Description:** {plant_info['description']}")
|
| 77 |
+
st.write(f"**Sun Requirements:** {plant_info['sun_requirements']}")
|
| 78 |
+
st.write(f"**Watering Instructions:** {plant_info['watering']}")
|
| 79 |
+
if plant_info["image_url"]:
|
| 80 |
st.image(plant_info["image_url"], caption=plant_info["name"], width=300)
|
| 81 |
+
|
| 82 |
+
if __name__ == "__main__":
|
| 83 |
+
main()
|
|
|
|
|
|
|
|
|