project-chimera / app.py
mgbam's picture
Create app.py
b172fa2 verified
raw
history blame
3.19 kB
# app.py
import gradio as gr
import asyncio
from src.chimera.core.orchestrator import run_analysis
from src.chimera.utils.logging_config import setup_logging, logger
from src.chimera.config import GEMINI_API_KEY # Import to check if configured
# Setup logging as soon as the app starts
setup_logging()
# Check essential configurations on startup
if not GEMINI_API_KEY:
logger.error("CRITICAL: GEMINI_API_KEY is not set. The application might not function correctly.")
# Optionally, raise an exception or display a persistent error in the UI
async def chimera_interface(query: str):
"""
Wrapper function for Gradio to call the async orchestrator.
"""
if not query:
return "Please enter a query."
if not GEMINI_API_KEY:
return "Error: Application is not configured correctly (Missing API Key)."
logger.info(f"Gradio interface received query: {query}")
try:
# Use asyncio.create_task if Gradio runs its own event loop correctly,
# otherwise run it directly if Gradio handles the async function call.
# Gradio generally handles async functions well.
result = await run_analysis(query)
logger.info("Gradio interface processing complete.")
return result
except Exception as e:
logger.exception("Error in Gradio interface call to run_analysis.")
return f"An unexpected error occurred in the backend: {e}"
# --- Gradio UI Definition ---
with gr.Blocks(theme=gr.themes.Soft(), title="Project Chimera") as demo:
gr.Markdown(
"""
# Project Chimera : Real-Time Global Analysis Engine
Enter your query to analyze real-time data from SERP and other sources using Gemini.
(Example: "Analyze recent news about renewable energy investments in the US")
"""
)
with gr.Row():
query_input = gr.Textbox(
label="Your Query:",
placeholder="Type your complex question or analysis request here...",
lines=3
)
submit_button = gr.Button("Analyze", variant="primary")
with gr.Row():
output_display = gr.Markdown(label="Chimera Analysis:") # Use Markdown for better formatting
# Link the button click to the interface function
submit_button.click(
fn=chimera_interface,
inputs=[query_input],
outputs=[output_display]
)
# Example usage display
gr.Examples(
examples=[
"Search recent news about AI impact on healthcare.",
"What are the latest developments in fusion energy according to recent searches?",
# Add more examples relevant to the APIs you integrate
],
inputs=[query_input],
outputs=[output_display],
fn=chimera_interface, # Make examples clickable
cache_examples=False # Depends if you want to cache example runs
)
# --- Launching the App ---
if __name__ == "__main__":
logger.info("Starting Gradio application...")
# Ensure loop is running if needed (Gradio might handle this)
# asyncio.run(demo.launch()) # If running locally and need explicit loop start
demo.launch() # Standard launch for Hugging Face Spaces