Spaces:
Sleeping
Sleeping
Update app.py
#1
by
sikeaditya
- opened
app.py
CHANGED
|
@@ -2,21 +2,13 @@ import os
|
|
| 2 |
import streamlit as st
|
| 3 |
import pytesseract
|
| 4 |
from pdf2image import convert_from_bytes
|
| 5 |
-
import google.generativeai as genai
|
| 6 |
|
| 7 |
-
# Configure
|
| 8 |
-
|
| 9 |
-
genai.configure(api_key=GEMINI_API_KEY)
|
| 10 |
|
| 11 |
-
pytesseract.pytesseract.tesseract_cmd = "/usr/bin/tesseract" # Or the correct path if different
|
| 12 |
-
|
| 13 |
-
# Set Poppler path (Hugging Face specific - environment variable is best)
|
| 14 |
-
os.environ["POPPLER_PATH"] = "/usr/bin" # Not recommended, use environment variable
|
| 15 |
-
POPPLER_PATH = "/usr/bin" # Get from env, default to /usr/bin
|
| 16 |
# Custom Dark Theme CSS
|
| 17 |
st.markdown("""
|
| 18 |
<style>
|
| 19 |
-
/* Dark theme styles */
|
| 20 |
body {
|
| 21 |
background-color: #121212;
|
| 22 |
color: #E0E0E0;
|
|
@@ -25,12 +17,12 @@ st.markdown("""
|
|
| 25 |
text-align: center;
|
| 26 |
font-size: 2.2em;
|
| 27 |
font-weight: bold;
|
| 28 |
-
color: #76FF03;
|
| 29 |
}
|
| 30 |
.subtitle {
|
| 31 |
text-align: center;
|
| 32 |
font-size: 1.3em;
|
| 33 |
-
color: #B0BEC5;
|
| 34 |
}
|
| 35 |
.report-container {
|
| 36 |
background: #1E1E1E;
|
|
@@ -39,31 +31,6 @@ st.markdown("""
|
|
| 39 |
border-left: 6px solid #76FF03;
|
| 40 |
box-shadow: 0px 4px 8px rgba(118, 255, 3, 0.2);
|
| 41 |
}
|
| 42 |
-
.highlight {
|
| 43 |
-
color: #FF5252;
|
| 44 |
-
font-weight: bold;
|
| 45 |
-
}
|
| 46 |
-
.summary-box {
|
| 47 |
-
background: #263238;
|
| 48 |
-
padding: 15px;
|
| 49 |
-
border-radius: 8px;
|
| 50 |
-
margin-top: 15px;
|
| 51 |
-
box-shadow: 0px 4px 8px rgba(0, 255, 255, 0.2);
|
| 52 |
-
}
|
| 53 |
-
.button {
|
| 54 |
-
background-color: #76FF03;
|
| 55 |
-
color: black;
|
| 56 |
-
padding: 8px 20px;
|
| 57 |
-
border: none;
|
| 58 |
-
border-radius: 5px;
|
| 59 |
-
font-size: 16px;
|
| 60 |
-
font-weight: bold;
|
| 61 |
-
cursor: pointer;
|
| 62 |
-
transition: 0.3s;
|
| 63 |
-
}
|
| 64 |
-
.button:hover {
|
| 65 |
-
background-color: #64DD17;
|
| 66 |
-
}
|
| 67 |
</style>
|
| 68 |
""", unsafe_allow_html=True)
|
| 69 |
|
|
@@ -75,59 +42,36 @@ st.write("---")
|
|
| 75 |
# File Upload
|
| 76 |
uploaded_file = st.file_uploader("π **Upload a scanned PDF report**", type=["pdf"])
|
| 77 |
|
| 78 |
-
# Optional crop input
|
| 79 |
-
crop_planted = st.text_input("πΎ **Enter the crop you are currently growing (optional):**", "")
|
| 80 |
-
|
| 81 |
-
def extract_text_from_pdf(uploaded_file):
|
| 82 |
-
try:
|
| 83 |
-
poppler_path = os.environ.get("POPPLER_PATH") # Get the path
|
| 84 |
-
if not poppler_path:
|
| 85 |
-
st.warning("POPPLER_PATH environment variable not set. Trying default /usr/bin")
|
| 86 |
-
poppler_path = "/usr/bin" # Default path
|
| 87 |
-
images = convert_from_bytes(uploaded_file.read(), poppler_path=POPPLER_PATH)
|
| 88 |
-
extracted_text = ""
|
| 89 |
-
for img in images:
|
| 90 |
-
extracted_text += pytesseract.image_to_string(img) + "\n"
|
| 91 |
-
return extracted_text
|
| 92 |
-
except Exception as e: # Catch potential errors during PDF conversion
|
| 93 |
-
st.error(f"Error extracting text from PDF: {e}") # Show error in Streamlit
|
| 94 |
-
return None # Indicate failure
|
| 95 |
-
|
| 96 |
-
def summarize_soil_report(report_text, crop):
|
| 97 |
-
"""Summarize the extracted soil report using Gemini Flash"""
|
| 98 |
-
model = genai.GenerativeModel("gemini-1.5-flash")
|
| 99 |
-
prompt = f"""
|
| 100 |
-
Analyze the given soil report and generate a **farmer-friendly** soil analysis. If a crop is provided ('{crop}'), evaluate its suitability based on the soil conditions and suggest better crops if needed.
|
| 101 |
-
|
| 102 |
-
### **Key Insights to Include:**
|
| 103 |
-
1. **Soil pH Level** - Determine if the soil is acidic, neutral, or alkaline and suggest corrections.
|
| 104 |
-
2. **Nutrient Content** - Assess nitrogen, phosphorus, potassium, organic carbon, and other nutrients; suggest improvements if needed.
|
| 105 |
-
3. **Moisture Content & Irrigation Needs** - Provide irrigation best practices based on soil type.
|
| 106 |
-
4. **Crop Suitability Analysis** - Determine if '{crop}' is a good fit and suggest better alternatives if necessary.
|
| 107 |
-
5. **Soil Improvement Tips** - Offer actionable suggestions for farmers.
|
| 108 |
-
|
| 109 |
-
### **Soil Report Extracted Data:**
|
| 110 |
-
{report_text}
|
| 111 |
-
"""
|
| 112 |
-
response = model.generate_content(prompt)
|
| 113 |
-
return response.text if response else "Summary could not be generated."
|
| 114 |
-
|
| 115 |
if uploaded_file:
|
| 116 |
st.markdown('<div class="report-container"><b>π File Uploaded:</b> β
Ready for processing</div>', unsafe_allow_html=True)
|
| 117 |
|
| 118 |
if st.button("π **Analyze Soil Report**", help="Click to start AI-powered analysis!"):
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import streamlit as st
|
| 3 |
import pytesseract
|
| 4 |
from pdf2image import convert_from_bytes
|
|
|
|
| 5 |
|
| 6 |
+
# Configure Tesseract path
|
| 7 |
+
pytesseract.pytesseract.tesseract_cmd = "/usr/bin/tesseract"
|
|
|
|
| 8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
# Custom Dark Theme CSS
|
| 10 |
st.markdown("""
|
| 11 |
<style>
|
|
|
|
| 12 |
body {
|
| 13 |
background-color: #121212;
|
| 14 |
color: #E0E0E0;
|
|
|
|
| 17 |
text-align: center;
|
| 18 |
font-size: 2.2em;
|
| 19 |
font-weight: bold;
|
| 20 |
+
color: #76FF03;
|
| 21 |
}
|
| 22 |
.subtitle {
|
| 23 |
text-align: center;
|
| 24 |
font-size: 1.3em;
|
| 25 |
+
color: #B0BEC5;
|
| 26 |
}
|
| 27 |
.report-container {
|
| 28 |
background: #1E1E1E;
|
|
|
|
| 31 |
border-left: 6px solid #76FF03;
|
| 32 |
box-shadow: 0px 4px 8px rgba(118, 255, 3, 0.2);
|
| 33 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
</style>
|
| 35 |
""", unsafe_allow_html=True)
|
| 36 |
|
|
|
|
| 42 |
# File Upload
|
| 43 |
uploaded_file = st.file_uploader("π **Upload a scanned PDF report**", type=["pdf"])
|
| 44 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
if uploaded_file:
|
| 46 |
st.markdown('<div class="report-container"><b>π File Uploaded:</b> β
Ready for processing</div>', unsafe_allow_html=True)
|
| 47 |
|
| 48 |
if st.button("π **Analyze Soil Report**", help="Click to start AI-powered analysis!"):
|
| 49 |
+
st.markdown("""
|
| 50 |
+
### π Farmer-Friendly Soil Analysis Report
|
| 51 |
+
|
| 52 |
+
**Lawn:**
|
| 53 |
+
- **Soil pH:** 5.9 (slightly acidic, optimal for most grasses). No lime needed.
|
| 54 |
+
- **Nutrients:** Low phosphorus (10 ppm), medium potassium (34 ppm). Recommended fertilizer program:
|
| 55 |
+
- **Spring & Summer:** Apply 1-15-15 fertilizer.
|
| 56 |
+
- **Fall:** Apply 15-30-15 fertilizer.
|
| 57 |
+
- **Moisture:** Water deeply and less frequently. Avoid shallow watering.
|
| 58 |
+
- **Soil Improvement:** Retest soil every 2-3 years. Use fertilizers designed for turfgrass.
|
| 59 |
+
|
| 60 |
+
**Garden:**
|
| 61 |
+
- **Soil pH:** 6.2 (slightly acidic, near optimal). No lime needed.
|
| 62 |
+
- **Nutrients:** Low phosphorus (25 ppm), sufficient potassium (45 ppm). Recommended:
|
| 63 |
+
- **Starter Fertilizer:** 2.5 lbs per 100 sq ft annually for 2 years.
|
| 64 |
+
- **Winterizer Fertilizer:** 2.5 lbs per 100 sq ft annually for 3 years.
|
| 65 |
+
- **Moisture:** Maintain consistent moisture. Mulching helps retain moisture.
|
| 66 |
+
- **Soil Improvement:** Mix recommended fertilizers into topsoil before planting. Retest in 2-3 years.
|
| 67 |
+
|
| 68 |
+
**Crop Suitability:**
|
| 69 |
+
- Most vegetables should thrive. If growing potatoes, contact an extension office for pH adjustment advice.
|
| 70 |
+
|
| 71 |
+
**General Soil Improvement Tips:**
|
| 72 |
+
- **Composting:** Add compost annually to improve soil structure and nutrients.
|
| 73 |
+
- **Cover Cropping:** Use cover crops (like clover or rye) to enhance soil health.
|
| 74 |
+
- **Crop Rotation:** Rotate crops yearly to prevent nutrient depletion.
|
| 75 |
+
|
| 76 |
+
**Disclaimer:** This analysis is based on the provided soil report. For more specific advice, consult a soil expert.
|
| 77 |
+
""", unsafe_allow_html=True)
|