Arivara's picture
Update app.py
f4f765a verified
import gradio as gr
from RAG_AGENT import gemini_explain_file, extract_clean_pdf_text
import os
css = """
.gradio-container {
font-family: 'Inter', sans-serif;
max-width: 850px !important;
margin: auto;
padding: 2rem;
background-color: #121212;
color: #e0e0e0;
}
.main-header {
text-align: center;
margin-bottom: 2rem;
}
.main-header h1 {
font-size: 2.2rem;
font-weight: 600;
color: #ffffff;
}
.main-header p {
font-size: 1rem;
color: #bbbbbb;
}
.upload-area {
border: 1px dashed #444 !important;
border-radius: 10px !important;
background-color: #1e1e1e !important;
padding: 1rem;
transition: 0.3s;
}
.upload-area:hover {
border-color: #777 !important;
}
.btn-primary {
background-color: #4F46E5 !important;
color: white !important;
border: none !important;
padding: 12px 20px !important;
border-radius: 8px !important;
font-size: 1rem;
font-weight: 500;
}
.btn-primary:hover {
background-color: #3730A3 !important;
}
#custom-output {
background-color: #1e1e1e;
border-radius: 10px;
padding: 1.5rem;
border: 1px solid #333;
min-height: 250px;
font-size: 1rem;
line-height: 1.6;
color: #e0e0e0;
margin-top: 2rem;
}
.footer {
text-align: center;
margin-top: 3rem;
font-size: 0.85rem;
color: #777;
}
"""
def analyze_file(file, question, analysis_type):
if not file:
return "⚠️ Please upload a file."
if analysis_type == "Extract PDF Text":
if not file.name.lower().endswith('.pdf'):
return "⚠️ This option only works with PDF files."
try:
text = extract_clean_pdf_text(file.name)
return f"**Extracted Text:**\n\n{text}"
except Exception as e:
return f"❌ Error: {e}"
elif analysis_type == "AI Analysis":
if not os.getenv('GEMINI_API_KEY'):
return "⚠️ Gemini API key not found in environment."
return gemini_explain_file(file, question)
return "⚠️ Invalid analysis type."
# Gradio UI
with gr.Blocks(css=css, title="Science Analyzer - Dark UI") as demo:
gr.HTML("""
<div class="main-header">
<h1>🧠 Science Analyzer</h1>
<p>Upload scientific documents and get AI-powered analysis</p>
</div>
""")
with gr.Column():
with gr.Row():
with gr.Column():
file_input = gr.File(
label="πŸ“ Upload File",
file_types=[".pdf", ".png", ".jpg", ".jpeg"],
file_count="single",
elem_classes=["upload-area"]
)
analysis_type = gr.Radio(
choices=["AI Analysis", "Extract PDF Text"],
value="AI Analysis",
label="πŸ” Analysis Type"
)
question_input = gr.Textbox(
label="Ask a Question (Optional)",
placeholder="Type your question here...",
lines=2
)
analyze_btn = gr.Button(
"πŸš€ Analyze",
variant="primary",
elem_classes=["btn-primary"]
)
output_box = gr.Markdown(
value="πŸ“„ Upload a file and click Analyze.",
elem_id="custom-output",
show_label=False
)
gr.HTML("""
<div class="footer">
<p>Β© 2025 β€’ Built with Gradio & Gemini API</p>
</div>
""")
analyze_btn.click(
fn=analyze_file,
inputs=[file_input, question_input, analysis_type],
outputs=output_box
)
file_input.change(
fn=lambda: "πŸ“€ File uploaded. Click 'Analyze' to begin.",
outputs=output_box
)
if __name__ == "__main__":
demo.launch(server_name="0.0.0.0", server_port=7860, share=False, show_error=True)