2001muhammadumair commited on
Commit
a1c0116
·
verified ·
1 Parent(s): c51bd87

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +86 -0
app.py ADDED
@@ -0,0 +1,86 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from dotenv import load_dotenv
2
+ import streamlit as st
3
+ import os
4
+ from groq import Groq
5
+
6
+ # Load environment variables from the .env file
7
+ load_dotenv()
8
+
9
+ # Securely get the GROQ API key from environment variables
10
+ groq_api_key = os.getenv("GROQ_API_KEY")
11
+ if not groq_api_key:
12
+ raise ValueError("GROQ_API_KEY environment variable not set.")
13
+
14
+ # Initialize the client with the API key
15
+ client = Groq(api_key=groq_api_key)
16
+
17
+
18
+ # Function to get a response from Groq API
19
+ def get_groq_response(prompt):
20
+ try:
21
+ chat_completion = client.chat.completions.create(
22
+ messages=[{"role": "user", "content": prompt}],
23
+ model="llama3-8b-8192"
24
+ )
25
+ return chat_completion.choices[0].message.content
26
+ except Exception as e:
27
+ return f"Error: {str(e)}"
28
+
29
+ # Streamlit app layout
30
+ st.title("Crop Advisory Chatbot")
31
+ st.write("Provide your details to get personalized crop advice.")
32
+
33
+ # Input fields for user details
34
+ soil_type = st.text_input("Soil type (e.g., sandy, clay, loamy):")
35
+ climate = st.text_input("Climate (e.g., hot, moderate, cold):")
36
+ area_size = st.number_input("Area size in acres:", min_value=0.1, step=0.1)
37
+ season = st.selectbox("Current season:", ["Select", "Summer", "Winter", "Rainy", "Autumn", "Spring"])
38
+
39
+ if st.button("Get Advisory"):
40
+ # Check if all fields are filled
41
+ if soil_type and climate and area_size and season != "Select":
42
+ # Prompts for different advisory features
43
+ crop_recommendation_prompt = (
44
+ f"Suggest suitable crops for soil type '{soil_type}', climate '{climate}', "
45
+ f"area size '{area_size}' acres, and season '{season}'."
46
+ )
47
+
48
+ fertilizer_prompt = (
49
+ f"Provide fertilizer recommendations for crops suitable in soil '{soil_type}' "
50
+ f"and climate '{climate}'."
51
+ )
52
+
53
+ irrigation_prompt = (
54
+ f"Suggest irrigation methods and water requirements for crops in "
55
+ f"'{soil_type}' soil with '{climate}' climate."
56
+ )
57
+
58
+ harvest_prompt = (
59
+ f"When is the best harvesting time for crops in '{climate}' climate during the '{season}' season?"
60
+ )
61
+
62
+ # Fetch responses from Groq API
63
+ with st.spinner("Fetching advisory..."):
64
+ crop_recommendation = get_groq_response(crop_recommendation_prompt)
65
+ fertilizer_advice = get_groq_response(fertilizer_prompt)
66
+ irrigation_advice = get_groq_response(irrigation_prompt)
67
+ harvest_advice = get_groq_response(harvest_prompt)
68
+
69
+ # Display results
70
+ st.subheader("Crop Recommendations")
71
+ st.write(crop_recommendation)
72
+
73
+ st.subheader("Fertilizer Recommendations")
74
+ st.write(fertilizer_advice)
75
+
76
+ st.subheader("Irrigation Advice")
77
+ st.write(irrigation_advice)
78
+
79
+ st.subheader("Harvesting Advice")
80
+ st.write(harvest_advice)
81
+
82
+ else:
83
+ st.warning("Please fill in all the details to get the advisory.")
84
+
85
+ # To run the app in Colab, you need to use ngrok or run it locally with:
86
+ # `streamlit run app.py`