ReportAgent / app.py
Quazim0t0's picture
Update app.py
bb29d2e verified
raw
history blame
2.62 kB
import gradio as gr
from smolagents import CodeAgent, HfApiModel
# Initialize the AI agent
agent = CodeAgent(
tools=[],
model=HfApiModel(model_id="Qwen/Qwen2.5-Coder-32B-Instruct"),
)
def analyze_content(file_paths, progress=gr.Progress()):
"""Process files and generate comprehensive report with progress tracking"""
progress(0, desc="Starting analysis...")
full_content = []
# Read files with progress
for i, path in enumerate(file_paths):
progress(i/len(file_paths), f"Reading {path.split('/')[-1]}...")
try:
with open(path, 'r', encoding='utf-8') as f:
content = f.read()
full_content.append(f"## {path.split('/')[-1]}\n{content}\n")
except Exception as e:
return f"Error processing {path}: {str(e)}"
# Generate report with progress
progress(0.8, "Analyzing content with AI...")
report = agent.run(f"""
Analyze these documents and create a detailed report:
{"".join(full_content)[:10000]}
Report structure:
1. Executive Summary
2. Key Findings
3. Important Patterns
4. Recommendations
Use professional markdown formatting with headings and bullet points.
""")
progress(1.0, "Analysis complete!")
return report
with gr.Blocks() as demo:
gr.Markdown("# Professional Document Analyzer")
with gr.Row():
with gr.Column(scale=1):
file_input = gr.File(
file_count="multiple",
file_types=[".txt"],
label="Upload Documents"
)
process_btn = gr.Button("Generate Report", variant="primary")
status = gr.Textbox(label="Processing Status", interactive=False)
with gr.Column(scale=2, variant="panel"):
gr.Markdown("## Analysis Report")
report_output = gr.Markdown(
elem_classes="report-box",
label="",
show_label=False
)
@process_btn.click(
inputs=file_input,
outputs=[status, report_output]
)
def process_files(files):
if not files:
return "No files uploaded", ""
file_paths = [f.name for f in files]
def update_progress(progress):
return lambda p: progress(p)
return (
gr.Progress().track() | # Progress indicator
analyze_content(file_paths)
)
if __name__ == "__main__":
demo.launch(
server_name="0.0.0.0",
server_port=7860,
share=True
)