import gradio as gr from agent_harness import run_analysis_agent import os def analyze_company(ticker: str, api_key: str): if api_key: os.environ["NVIDIA_API_KEY"] = api_key generator = run_analysis_agent(ticker) status_text = "" # Iterate through the generator yields while True: try: val = next(generator) if isinstance(val, str): status_text += f"\n- {val}" yield status_text, gr.update(value=[]), gr.update(visible=False) else: # Generator finished and returned a tuple break except StopIteration as e: # e.value contains the return value of the generator (final_message, insights, files) final_message, insights, files = e.value insights_md = "### Top 5 Critical Insights:\n" for i in insights: insights_md += f"- {i}\n" status_text += f"\n\n{insights_md}\n\n**Agent Summary:**\n{final_message}" # Make the file components visible with the generated files yield status_text, gr.update(value=files), gr.update(visible=True, value=files) return # Gradio Interface with gr.Blocks(title="Financial Analyzer Agent", theme=gr.themes.Soft()) as demo: gr.Markdown("# 📈 Financial Analyzer Agent") gr.Markdown("Enter a stock ticker (e.g. `RELIANCE.NS`) to have the Mistral Agent autonomously fetch financials, run an audit against peers, and generate professional Word and Excel reports.") with gr.Row(): with gr.Column(scale=2): ticker_input = gr.Textbox(label="Company Ticker", placeholder="e.g. TCS.NS or AAPL") api_key_input = gr.Textbox( label="Nvidia API Key (Optional if set in Hugging Face Secrets)", placeholder="nvapi-...", type="password" ) run_btn = gr.Button("Run Analysis", variant="primary") with gr.Column(scale=3): status_output = gr.Markdown("### Agent Progress:\nWaiting for input...") with gr.Row(): file_output = gr.File(label="Download Generated Reports", visible=False) run_btn.click( fn=analyze_company, inputs=[ticker_input, api_key_input], outputs=[status_output, file_output, file_output] # Output twice to update value and visibility ) if __name__ == "__main__": demo.launch()