rohitashva commited on
Commit
5d02590
Β·
verified Β·
1 Parent(s): c0f3a9f

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +68 -0
app.py ADDED
@@ -0,0 +1,68 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import pytesseract
3
+ from pdf2image import convert_from_bytes
4
+ import google.generativeai as genai
5
+
6
+ # Configure Google Gemini API
7
+ GEMINI_API_KEY = os.getenv('GEMINI')
8
+ genai.configure(api_key=GEMINI_API_KEY)
9
+
10
+ # Streamlit UI
11
+ st.set_page_config(page_title="Soil Report Analyzer", layout="wide")
12
+ st.title("🌱 Soil Report Analyzer")
13
+ st.write("Upload a **scanned soil report (PDF)** to generate a farmer-friendly soil analysis!")
14
+
15
+ # File Upload
16
+ uploaded_file = st.file_uploader("πŸ“‚ Upload a scanned PDF report", type=["pdf"])
17
+
18
+ # Optional crop input
19
+ crop_planted = st.text_input("🌾 Enter the crop you are currently growing (optional):", "")
20
+
21
+ def extract_text_from_pdf(uploaded_file):
22
+ """Extract text from scanned PDF using OCR"""
23
+ if uploaded_file is None:
24
+ return None
25
+
26
+ images = convert_from_bytes(uploaded_file.read())
27
+ extracted_text = ""
28
+
29
+ for image in images:
30
+ extracted_text += pytesseract.image_to_string(image) + "\n"
31
+
32
+ return extracted_text.strip()
33
+
34
+ def summarize_soil_report(report_text, crop):
35
+ """Summarize the extracted soil report using Gemini Flash"""
36
+ model = genai.GenerativeModel("gemini-1.5-flash")
37
+ prompt = f"""
38
+ 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.
39
+
40
+ ### **Key Insights to Include:**
41
+ 1. **Soil pH Level** - Determine if the soil is acidic, neutral, or alkaline and suggest corrections.
42
+ 2. **Nutrient Content** - Assess nitrogen, phosphorus, potassium, organic carbon, and other nutrients; suggest improvements if needed.
43
+ 3. **Moisture Content & Irrigation Needs** - Provide irrigation best practices based on soil type.
44
+ 4. **Crop Suitability Analysis** - Determine if '{crop}' is a good fit and suggest better alternatives if necessary.
45
+ 5. **Soil Improvement Tips** - Offer actionable suggestions for farmers.
46
+
47
+ ### **Soil Report Extracted Data:**
48
+ {report_text}
49
+ """
50
+ response = model.generate_content(prompt)
51
+ return response.text if response else "Summary could not be generated."
52
+
53
+ if uploaded_file:
54
+ if st.button("πŸ“Š Analyze Soil Report"):
55
+ with st.spinner("Processing... ⏳"):
56
+ extracted_text = extract_text_from_pdf(uploaded_file)
57
+ if extracted_text:
58
+ summary = summarize_soil_report(extracted_text, crop_planted)
59
+
60
+ st.subheader("πŸ“Œ Farmer-Friendly Soil Analysis")
61
+ st.write(summary)
62
+
63
+ # Option to download summary
64
+ st.download_button("πŸ“₯ Download Summary as Text", summary, file_name="Soil_Report_Summary.txt")
65
+ else:
66
+ st.error("⚠️ Could not extract text from the PDF. Please try again.")
67
+
68
+ st.info("πŸ‘† Upload a scanned soil report PDF and click 'Analyze Soil Report' to begin.")