File size: 3,615 Bytes
b172fa2
aaa20d9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b172fa2
 
aaa20d9
 
 
b172fa2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# app.py
import sys
import os

print("--- DEBUG INFO ---")
print("Current Working Directory:", os.getcwd())
print("sys.path:")
for p in sys.path:
    print(f"  - {p}")
print("Root Directory Contents:", os.listdir('.'))
if os.path.exists('src'):
    print("src Directory Contents:", os.listdir('src'))
else:
    print("'src' directory NOT FOUND at root.")
print("--- END DEBUG INFO ---")

# Original imports (keep the failing one to see the error after the debug info)
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 gradio as gr
import asyncio

# 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