Sayandip commited on
Commit
e45191b
·
verified ·
1 Parent(s): fb911fd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -42
app.py CHANGED
@@ -6,56 +6,49 @@ from google import genai
6
  # Initialize Gemini Client with API key from environment
7
  client = genai.Client(api_key=os.environ.get("GEMINI_API_KEY"))
8
 
9
- # Helper to encode image for Gemini
10
- def process_image(image_file):
11
- image_bytes = image_file.read()
12
- encoded_image = base64.b64encode(image_bytes).decode("utf-8")
13
- return {
14
- "inline_data": {
15
- "mime_type": image_file.type,
16
- "data": encoded_image
17
- }
18
- }
19
 
20
  # Main App
21
  def main():
22
- st.set_page_config(page_title="🌿 Leaf Disease Detector", layout="centered")
23
- st.title("🌱 Leaf Disease Detector")
24
- st.write("Upload an image of a plant leaf, and we'll analyze it to detect possible diseases, "
25
- "provide treatment suggestions, and find real-world statistics from the internet about this disease.")
26
-
27
- uploaded_image = st.file_uploader("Upload a leaf image (JPG or PNG)", type=["jpg", "jpeg", "png"])
28
-
29
- if uploaded_image:
30
- st.image(uploaded_image, caption="Uploaded Leaf", use_container_width=True)
31
- image_data = process_image(uploaded_image)
32
-
33
- if st.button("🧪 Detect Disease"):
34
- with st.spinner("Analyzing leaf and searching for information..."):
35
- content_blocks = [
36
- {
37
- "text": (
38
- "You are a plant disease diagnostic AI. Analyze the uploaded leaf image and do the following:\n\n"
39
- "1. Identify any plant disease visible on the leaf.\n"
40
- "2. Provide the disease name and a short scientific description.\n"
41
- "3. Suggest treatment or prevention methods farmers can use.\n"
42
- "4. Use **Google Search** to find:\n"
43
- " - Estimated global or regional financial losses due to this disease\n"
44
- " - Impactful statistics or facts due to this disease (e.g., crops affected, common locations, yield reduction, etc.)\n"
45
- "If the leaf looks healthy, clearly state that."
46
- )
47
- },
48
- image_data
49
- ]
50
 
51
  response = client.models.generate_content(
52
  model="gemini-2.5-flash",
53
- contents=content_blocks,
54
- config={"tools": [{"google_search": {}}]}
55
  )
56
 
57
- st.success("📋 Analysis Result:")
58
  st.markdown(response.text)
59
 
60
  if __name__ == "__main__":
61
- main()
 
6
  # Initialize Gemini Client with API key from environment
7
  client = genai.Client(api_key=os.environ.get("GEMINI_API_KEY"))
8
 
9
+ # Supported file types
10
+ SUPPORTED_EXTENSIONS = ["py", "c", "cpp", "java", "js", "jsx"]
11
+
12
+ # Helper: Read and decode code file
13
+ def read_code_file(file):
14
+ content = file.read().decode("utf-8")
15
+ filename = file.name
16
+ return filename, content
 
 
17
 
18
  # Main App
19
  def main():
20
+ st.set_page_config(page_title="🧠 Code Analyzer & Fixer", layout="centered")
21
+ st.title("🛠️ Code Analyzer & Bug Fixer")
22
+ st.write("Upload a code file (Python, C, C++, Java, or React) and get a detailed analysis including:\n"
23
+ "- README-style description\n"
24
+ "- Bug detection\n"
25
+ "- Corrected code\n"
26
+ "- Summary of changes")
27
+
28
+ uploaded_file = st.file_uploader("Upload your code file", type=SUPPORTED_EXTENSIONS)
29
+
30
+ if uploaded_file:
31
+ filename, code_text = read_code_file(uploaded_file)
32
+ st.code(code_text, language=filename.split(".")[-1])
33
+
34
+ if st.button("🔍 Analyze and Fix Code"):
35
+ with st.spinner("Analyzing your code using Gemini..."):
36
+ prompt = (
37
+ f"You are a software code analysis assistant. Given the code below, perform the following tasks:\n\n"
38
+ f"1. Provide a short README-style summary of what the code is intended to do.\n"
39
+ f"2. Identify any bugs or issues in the code.\n"
40
+ f"3. Provide a corrected, clean version of the entire code.\n"
41
+ f"4. List the changes you made in bullet points.\n\n"
42
+ f"--- CODE START ---\n{code_text}\n--- CODE END ---"
43
+ )
 
 
 
 
44
 
45
  response = client.models.generate_content(
46
  model="gemini-2.5-flash",
47
+ contents=[{"text": prompt}]
 
48
  )
49
 
50
+ st.success(" Analysis Complete:")
51
  st.markdown(response.text)
52
 
53
  if __name__ == "__main__":
54
+ main()