AhmadRAZA23 commited on
Commit
eaf4c79
·
verified ·
1 Parent(s): 9a4cb82

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -68
app.py CHANGED
@@ -1,69 +1,24 @@
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")
7
- text_generation_pipeline = pipeline("text-generation", model="gpt2", framework="pt")
8
-
9
- # Function to fetch plant data using OpenFarm API
10
- def fetch_plant_data(plant_name):
11
- url = f"https://openfarm.cc/api/v1/crops?filter={plant_name}"
12
- try:
13
- response = requests.get(url)
14
- response.raise_for_status() # Raise an error for bad status codes
15
- data = response.json()
16
- if data.get("data"):
17
- plant_info = data["data"][0]["attributes"]
18
- return {
19
- "name": plant_info.get("name", plant_name),
20
- "description": plant_info.get("description", "No description available."),
21
- "image_url": plant_info.get("main_image_path", None),
22
- "sun_requirements": plant_info.get("sun_requirements", "No information available."),
23
- "watering": plant_info.get("watering", None),
24
- }
25
- return None
26
- except requests.exceptions.RequestException as e:
27
- st.error(f"Error fetching data: {e}")
28
- return None
29
-
30
- # Function to generate watering instructions using AI
31
- def refine_watering_instructions(plant_name, basic_instructions=None):
32
- prompt = (
33
- f"Refine these watering instructions for the plant '{plant_name}': '{basic_instructions}'. "
34
- "If no specific instructions are available, provide a detailed guideline for watering this plant type."
35
- )
36
- result = text_generation_pipeline(prompt, max_length=150, num_return_sequences=1)
37
- return result[0]["generated_text"]
38
-
39
- # Streamlit app
40
- def main():
41
- st.title("🌱 AI-Powered Plant Care Guide")
42
-
43
- # Section 1: Search for plant care information
44
- st.header("Search for Plant Care Information")
45
- user_input = st.text_input("Enter the name of a plant", "")
46
-
47
- if st.button("Search"):
48
- if user_input.strip():
49
- with st.spinner("Fetching plant data..."):
50
- plant_info = fetch_plant_data(user_input)
51
-
52
- if plant_info:
53
- with st.spinner("Generating watering instructions..."):
54
- basic_watering = plant_info["watering"] or "No specific instructions available."
55
- plant_info["watering"] = refine_watering_instructions(user_input, basic_instructions=basic_watering)
56
-
57
- st.subheader(f"Care Instructions for {plant_info['name']}")
58
- st.write(f"**Description:** {plant_info['description']}")
59
- st.write(f"**Sun Requirements:** {plant_info['sun_requirements']}")
60
- st.write(f"**Watering Instructions:** {plant_info['watering']}")
61
-
62
- # Display the image if available, otherwise show "Picture not available"
63
- if plant_info["image_url"]:
64
  st.image(plant_info["image_url"], caption=plant_info["name"], width=300)
65
- else:
66
- st.write("**Picture not available**")
67
-
68
- if __name__ == "__main__":
69
- main()
 
 
1
+ if st.button("Search"):
2
+ if user_input.strip():
3
+ with st.spinner("Fetching plant data..."):
4
+ plant_info = fetch_plant_data(user_input)
5
+
6
+ if plant_info:
7
+ with st.spinner("Generating watering instructions..."):
8
+ basic_watering = plant_info["watering"] or "No specific instructions available."
9
+ plant_info["watering"] = refine_watering_instructions(user_input, basic_instructions=basic_watering)
10
+
11
+ st.subheader(f"Care Instructions for {plant_info['name']}")
12
+ st.write(f"**Description:** {plant_info['description']}")
13
+ st.write(f"**Sun Requirements:** {plant_info['sun_requirements']}")
14
+ st.write(f"**Watering Instructions:** {plant_info['watering']}")
15
+
16
+ if plant_info["image_url"]:
17
+ try:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
  st.image(plant_info["image_url"], caption=plant_info["name"], width=300)
19
+ except Exception:
20
+ st.warning("Could not load the plant image.")
21
+ else:
22
+ st.error("No plant information found.")
23
+ else:
24
+ st.warning("Please enter a plant name.")