mgbam commited on
Commit
b172fa2
·
verified ·
1 Parent(s): 2e2ec9c

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +81 -0
app.py ADDED
@@ -0,0 +1,81 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # app.py
2
+ import gradio as gr
3
+ import asyncio
4
+ from src.chimera.core.orchestrator import run_analysis
5
+ from src.chimera.utils.logging_config import setup_logging, logger
6
+ from src.chimera.config import GEMINI_API_KEY # Import to check if configured
7
+
8
+ # Setup logging as soon as the app starts
9
+ setup_logging()
10
+
11
+ # Check essential configurations on startup
12
+ if not GEMINI_API_KEY:
13
+ logger.error("CRITICAL: GEMINI_API_KEY is not set. The application might not function correctly.")
14
+ # Optionally, raise an exception or display a persistent error in the UI
15
+
16
+ async def chimera_interface(query: str):
17
+ """
18
+ Wrapper function for Gradio to call the async orchestrator.
19
+ """
20
+ if not query:
21
+ return "Please enter a query."
22
+ if not GEMINI_API_KEY:
23
+ return "Error: Application is not configured correctly (Missing API Key)."
24
+
25
+ logger.info(f"Gradio interface received query: {query}")
26
+ try:
27
+ # Use asyncio.create_task if Gradio runs its own event loop correctly,
28
+ # otherwise run it directly if Gradio handles the async function call.
29
+ # Gradio generally handles async functions well.
30
+ result = await run_analysis(query)
31
+ logger.info("Gradio interface processing complete.")
32
+ return result
33
+ except Exception as e:
34
+ logger.exception("Error in Gradio interface call to run_analysis.")
35
+ return f"An unexpected error occurred in the backend: {e}"
36
+
37
+ # --- Gradio UI Definition ---
38
+ with gr.Blocks(theme=gr.themes.Soft(), title="Project Chimera") as demo:
39
+ gr.Markdown(
40
+ """
41
+ # Project Chimera : Real-Time Global Analysis Engine
42
+ Enter your query to analyze real-time data from SERP and other sources using Gemini.
43
+ (Example: "Analyze recent news about renewable energy investments in the US")
44
+ """
45
+ )
46
+ with gr.Row():
47
+ query_input = gr.Textbox(
48
+ label="Your Query:",
49
+ placeholder="Type your complex question or analysis request here...",
50
+ lines=3
51
+ )
52
+ submit_button = gr.Button("Analyze", variant="primary")
53
+ with gr.Row():
54
+ output_display = gr.Markdown(label="Chimera Analysis:") # Use Markdown for better formatting
55
+
56
+ # Link the button click to the interface function
57
+ submit_button.click(
58
+ fn=chimera_interface,
59
+ inputs=[query_input],
60
+ outputs=[output_display]
61
+ )
62
+
63
+ # Example usage display
64
+ gr.Examples(
65
+ examples=[
66
+ "Search recent news about AI impact on healthcare.",
67
+ "What are the latest developments in fusion energy according to recent searches?",
68
+ # Add more examples relevant to the APIs you integrate
69
+ ],
70
+ inputs=[query_input],
71
+ outputs=[output_display],
72
+ fn=chimera_interface, # Make examples clickable
73
+ cache_examples=False # Depends if you want to cache example runs
74
+ )
75
+
76
+ # --- Launching the App ---
77
+ if __name__ == "__main__":
78
+ logger.info("Starting Gradio application...")
79
+ # Ensure loop is running if needed (Gradio might handle this)
80
+ # asyncio.run(demo.launch()) # If running locally and need explicit loop start
81
+ demo.launch() # Standard launch for Hugging Face Spaces