Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,60 +1,12 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import os
|
| 3 |
-
import pandas as pd
|
| 4 |
-
import fitz # PyMuPDF
|
| 5 |
-
from google import genai
|
| 6 |
-
from google.genai import types
|
| 7 |
-
from docx import Document
|
| 8 |
-
from pptx import Presentation
|
| 9 |
-
from reportlab.lib.pagesizes import A4
|
| 10 |
-
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer
|
| 11 |
-
from reportlab.lib.styles import getSampleStyleSheet
|
| 12 |
-
from bs4 import BeautifulSoup
|
| 13 |
-
import re
|
| 14 |
import base64
|
|
|
|
| 15 |
|
| 16 |
-
#
|
| 17 |
client = genai.Client(api_key=os.environ.get("GEMINI_API_KEY"))
|
| 18 |
|
| 19 |
-
#
|
| 20 |
-
def extract_text_from_docx(docx_file):
|
| 21 |
-
document = Document(docx_file)
|
| 22 |
-
return "\n".join([para.text for para in document.paragraphs])
|
| 23 |
-
|
| 24 |
-
def extract_text_from_csv(csv_file):
|
| 25 |
-
df = pd.read_csv(csv_file)
|
| 26 |
-
return df.to_string(index=False)
|
| 27 |
-
|
| 28 |
-
def extract_text_from_xlsx(xlsx_file):
|
| 29 |
-
df = pd.read_excel(xlsx_file)
|
| 30 |
-
return df.to_string(index=False)
|
| 31 |
-
|
| 32 |
-
def extract_text_from_pptx(pptx_file):
|
| 33 |
-
prs = Presentation(pptx_file)
|
| 34 |
-
text_runs = []
|
| 35 |
-
for slide in prs.slides:
|
| 36 |
-
for shape in slide.shapes:
|
| 37 |
-
if hasattr(shape, "text"):
|
| 38 |
-
text_runs.append(shape.text)
|
| 39 |
-
return "\n".join(text_runs)
|
| 40 |
-
|
| 41 |
-
def extract_text_from_pdf(pdf_file):
|
| 42 |
-
doc = fitz.open(stream=pdf_file.read(), filetype="pdf")
|
| 43 |
-
text = ""
|
| 44 |
-
for page in doc:
|
| 45 |
-
text += page.get_text()
|
| 46 |
-
return text
|
| 47 |
-
|
| 48 |
-
def extract_text_from_html(html_file):
|
| 49 |
-
soup = BeautifulSoup(html_file.read(), "html.parser")
|
| 50 |
-
return soup.get_text()
|
| 51 |
-
|
| 52 |
-
def extract_text_from_tex(tex_file):
|
| 53 |
-
content = tex_file.read().decode("utf-8")
|
| 54 |
-
content = re.sub(r'\\[a-zA-Z]+\{[^}]*\}', '', content)
|
| 55 |
-
content = re.sub(r'\\[a-zA-Z]+', '', content)
|
| 56 |
-
return content
|
| 57 |
-
|
| 58 |
def process_image(image_file):
|
| 59 |
image_bytes = image_file.read()
|
| 60 |
encoded_image = base64.b64encode(image_bytes).decode("utf-8")
|
|
@@ -65,175 +17,45 @@ def process_image(image_file):
|
|
| 65 |
}
|
| 66 |
}
|
| 67 |
|
| 68 |
-
|
| 69 |
-
video_bytes = video_file.read()
|
| 70 |
-
filetype = video_file.name.split(".")[-1].lower()
|
| 71 |
-
mime_map = {
|
| 72 |
-
"avi": "video/x-msvideo",
|
| 73 |
-
"mkv": "video/x-matroska",
|
| 74 |
-
"flv": "video/x-flv",
|
| 75 |
-
"wmv": "video/x-ms-wmv",
|
| 76 |
-
"mpeg": "video/mpeg"
|
| 77 |
-
}
|
| 78 |
-
mime_type = mime_map.get(filetype, video_file.type)
|
| 79 |
-
encoded_video = base64.b64encode(video_bytes).decode("utf-8")
|
| 80 |
-
return {
|
| 81 |
-
"inline_data": {
|
| 82 |
-
"mime_type": mime_type,
|
| 83 |
-
"data": encoded_video
|
| 84 |
-
}
|
| 85 |
-
}
|
| 86 |
-
|
| 87 |
-
def export_conversation_to_pdf(conversation_history):
|
| 88 |
-
pdf_path = "Conversation.pdf"
|
| 89 |
-
doc = SimpleDocTemplate(pdf_path, pagesize=A4)
|
| 90 |
-
styles = getSampleStyleSheet()
|
| 91 |
-
elements = []
|
| 92 |
-
|
| 93 |
-
for i, (user_q, gemini_a) in enumerate(conversation_history):
|
| 94 |
-
elements.append(Paragraph(f"<b>Q{i+1}:</b> {user_q}", styles["Normal"]))
|
| 95 |
-
elements.append(Spacer(1, 8))
|
| 96 |
-
elements.append(Paragraph(f"<b>A{i+1}:</b> {gemini_a.replace(chr(10), '<br/>')}", styles["Normal"]))
|
| 97 |
-
elements.append(Spacer(1, 16))
|
| 98 |
-
|
| 99 |
-
doc.build(elements)
|
| 100 |
-
return pdf_path
|
| 101 |
-
|
| 102 |
-
# --------- Main App ---------
|
| 103 |
def main():
|
| 104 |
-
st.set_page_config(page_title="
|
| 105 |
-
st.title("
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
if
|
| 112 |
-
st.
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
elif filetype == "xlsx":
|
| 138 |
-
all_text_content += extract_text_from_xlsx(uploaded_file) + "\n"
|
| 139 |
-
elif filetype == "pptx":
|
| 140 |
-
all_text_content += extract_text_from_pptx(uploaded_file) + "\n"
|
| 141 |
-
elif filetype == "pdf":
|
| 142 |
-
all_text_content += extract_text_from_pdf(uploaded_file) + "\n"
|
| 143 |
-
elif filetype in ["html", "htm"]:
|
| 144 |
-
all_text_content += extract_text_from_html(uploaded_file) + "\n"
|
| 145 |
-
elif filetype == "tex":
|
| 146 |
-
all_text_content += extract_text_from_tex(uploaded_file) + "\n"
|
| 147 |
-
elif filetype in ["jpg", "jpeg", "png"]:
|
| 148 |
-
st.session_state.image_data = process_image(uploaded_file)
|
| 149 |
-
all_text_content += "Image uploaded and processed.\n"
|
| 150 |
-
st.image(uploaded_file, caption=uploaded_file.name, use_container_width=True)
|
| 151 |
-
elif filetype in ["mp4", "webm", "mov", "avi", "mkv", "flv", "wmv", "mpeg"]:
|
| 152 |
-
st.session_state.video_data = process_video(uploaded_file)
|
| 153 |
-
all_text_content += f"Video file '{uploaded_file.name}' uploaded and processed.\n"
|
| 154 |
-
st.video(uploaded_file)
|
| 155 |
-
else:
|
| 156 |
-
st.error(f"Unsupported file format: {uploaded_file.name}")
|
| 157 |
-
except Exception as e:
|
| 158 |
-
st.error(f"Failed to extract/process {uploaded_file.name}: {e}")
|
| 159 |
-
|
| 160 |
-
st.session_state.documents_text = all_text_content
|
| 161 |
-
|
| 162 |
-
if st.session_state.documents_text:
|
| 163 |
-
st.markdown("### \U0001F4AC Conversation")
|
| 164 |
-
for user_q, gemini_a in st.session_state.conversation:
|
| 165 |
-
st.markdown(f"**You:** {user_q}")
|
| 166 |
-
st.markdown(f"**Gemini:**\n\n{gemini_a}")
|
| 167 |
-
|
| 168 |
-
if st.session_state.chat_active:
|
| 169 |
-
with st.form(key="chat_form", clear_on_submit=True):
|
| 170 |
-
user_input = st.text_input("Ask a question about the documents (type 'exit' to stop):")
|
| 171 |
-
submit = st.form_submit_button("Send")
|
| 172 |
-
|
| 173 |
-
if submit and user_input:
|
| 174 |
-
if user_input.strip().lower() == "exit":
|
| 175 |
-
st.session_state.chat_active = False
|
| 176 |
-
st.success("Chat ended. Reload to start again.")
|
| 177 |
-
else:
|
| 178 |
-
with st.spinner("Let me think..."):
|
| 179 |
-
content_blocks = []
|
| 180 |
-
|
| 181 |
-
# System prompt inserted first here:
|
| 182 |
-
content_blocks.append({
|
| 183 |
-
"text": (
|
| 184 |
-
"Your role is to analyze documents, images, and videos to help identify and classify driver states, "
|
| 185 |
-
"including: drowsy, alert, yawning, microsleep, eyes/lips open or closed, and other distractions. "
|
| 186 |
-
"The user may ask for timestamps of events in video, technical explanations from code/docs, "
|
| 187 |
-
"or structured summaries. Prioritize your answers for this use case and act as a helpful assistant "
|
| 188 |
-
"within this domain. "
|
| 189 |
-
"Additionally, your classification may rely on the State Farm Distracted Driver Detection dataset, "
|
| 190 |
-
"which defines 10 distraction categories such as texting, phone use, reaching behind, adjusting the radio, "
|
| 191 |
-
"and safe driving. Use this labeling scheme when interpreting relevant images."
|
| 192 |
-
)
|
| 193 |
-
})
|
| 194 |
-
|
| 195 |
-
if st.session_state.conversation:
|
| 196 |
-
history_text = "\n\n".join(
|
| 197 |
-
f"Q: {entry[0]}\nA: {entry[1]}"
|
| 198 |
-
for entry in st.session_state.conversation
|
| 199 |
-
)
|
| 200 |
-
content_blocks.append({"text": f"Previous conversation:\n{history_text}"})
|
| 201 |
-
|
| 202 |
-
if st.session_state.documents_text.strip():
|
| 203 |
-
content_blocks.append({"text": f"Context:\n{st.session_state.documents_text}"})
|
| 204 |
-
|
| 205 |
-
if "image_data" in st.session_state:
|
| 206 |
-
content_blocks.append(st.session_state.image_data)
|
| 207 |
-
|
| 208 |
-
if "video_data" in st.session_state:
|
| 209 |
-
content_blocks.append(st.session_state.video_data)
|
| 210 |
-
|
| 211 |
-
content_blocks.append({"text": f"Question: {user_input}"})
|
| 212 |
-
|
| 213 |
-
response = client.models.generate_content(
|
| 214 |
-
model="gemini-2.5-flash",
|
| 215 |
-
contents=content_blocks,
|
| 216 |
-
config={"tools": [{"google_search": {}}]}
|
| 217 |
-
)
|
| 218 |
-
|
| 219 |
-
st.session_state.conversation.append((user_input, response.text))
|
| 220 |
-
st.success("\U0001F4A1 Answer:")
|
| 221 |
-
st.write(response.text)
|
| 222 |
-
|
| 223 |
-
st.session_state.chat_history.append({
|
| 224 |
-
"question": user_input,
|
| 225 |
-
"answer": response.text
|
| 226 |
-
})
|
| 227 |
-
|
| 228 |
-
if st.session_state.conversation:
|
| 229 |
-
pdf_path = export_conversation_to_pdf(st.session_state.conversation)
|
| 230 |
-
with open(pdf_path, "rb") as f:
|
| 231 |
-
st.download_button(
|
| 232 |
-
label="\U0001F4E5 Export Conversation as PDF",
|
| 233 |
-
data=f,
|
| 234 |
-
file_name="Conversation.pdf",
|
| 235 |
-
mime="application/pdf"
|
| 236 |
)
|
| 237 |
|
|
|
|
|
|
|
|
|
|
| 238 |
if __name__ == "__main__":
|
| 239 |
-
main()
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
import base64
|
| 4 |
+
from google import genai
|
| 5 |
|
| 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")
|
|
|
|
| 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()
|