Spaces:
Build error
Build error
import gradio as gr | |
import pandas as pd | |
from io import StringIO | |
from smolagents import CodeAgent, HfApiModel | |
# Initialize the AI agent | |
agent = CodeAgent( | |
tools=[], | |
model=HfApiModel(model_id="Qwen/Qwen2.5-Coder-32B-Instruct"), | |
) | |
def process_text(content): | |
"""Convert text content to structured format""" | |
csv_output = agent.run(f"Convert to CSV (include headers):\n{content}\nOutput ONLY valid CSV:") | |
try: | |
return pd.read_csv(StringIO(csv_output), keep_default_na=False) | |
except: | |
return pd.DataFrame() | |
def analyze_content(full_text): | |
"""Generate comprehensive report""" | |
report = agent.run(f""" | |
Create detailed analysis report from this data: | |
{full_text[:5000]} | |
Include: | |
1. Key insights and patterns | |
2. Important statistics | |
3. Actionable recommendations | |
4. Potential anomalies | |
Use markdown formatting with headers. | |
""") | |
return report | |
def handle_upload(files): | |
"""Handle multiple file uploads correctly""" | |
all_dfs = [] | |
full_content = [] | |
for file in files: | |
try: | |
content = file.name + "\n" + file.read().decode() | |
df = process_text(content) | |
all_dfs.append(df) | |
full_content.append(content) | |
except Exception as e: | |
print(f"Error processing {file.name}: {str(e)}") | |
combined_df = pd.concat(all_dfs, ignore_index=True) if all_dfs else pd.DataFrame() | |
report = analyze_content("\n\n".join(full_content)) if full_content else "No valid content found" | |
return combined_df, report | |
with gr.Blocks() as demo: | |
gr.Markdown("# Multi-Document Analyzer") | |
with gr.Row(): | |
file_input = gr.File( | |
file_count="multiple", | |
file_types=[".txt"], | |
label="Upload Text Files" | |
) | |
process_btn = gr.Button("Analyze Documents", variant="primary") | |
with gr.Row(): | |
data_output = gr.Dataframe(label="Structured Data Preview", wrap=True) | |
report_output = gr.Markdown(label="Analysis Report") | |
process_btn.click( | |
handle_upload, | |
inputs=file_input, | |
outputs=[data_output, report_output] | |
) | |
if __name__ == "__main__": | |
demo.launch( | |
server_name="0.0.0.0", | |
server_port=7860, | |
share=True # Enable public sharing | |
) |