Update app.py
Browse files
app.py
CHANGED
|
@@ -0,0 +1,102 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import matplotlib.pyplot as plt
|
| 4 |
+
from huggingface_hub import InferenceClient
|
| 5 |
+
import os
|
| 6 |
+
|
| 7 |
+
# Initialize HF Inference client
|
| 8 |
+
client = InferenceClient(token=os.environ.get("HF_TOKEN"))
|
| 9 |
+
MAX_TOKENS = 1000
|
| 10 |
+
|
| 11 |
+
def analyze_data(query, df, chat_history):
|
| 12 |
+
"""Process user query using HF API and generate response"""
|
| 13 |
+
if df is None:
|
| 14 |
+
return chat_history + [[query, "Please upload a CSV file first!"]]
|
| 15 |
+
|
| 16 |
+
try:
|
| 17 |
+
# Generate code using StarCoder model
|
| 18 |
+
code_response = client.text_generation(
|
| 19 |
+
prompt=f"""
|
| 20 |
+
Generate Python code to answer this data analysis question:
|
| 21 |
+
Dataset columns: {list(df.columns)}
|
| 22 |
+
Question: {query}
|
| 23 |
+
|
| 24 |
+
Return only the code with these requirements:
|
| 25 |
+
1. Use pandas and matplotlib
|
| 26 |
+
2. Store result in 'result' variable
|
| 27 |
+
3. Create plot using plt.figure()
|
| 28 |
+
4. No user input or file operations
|
| 29 |
+
""",
|
| 30 |
+
model="bigcode/starcoder",
|
| 31 |
+
max_new_tokens=MAX_TOKENS,
|
| 32 |
+
temperature=0.2
|
| 33 |
+
)
|
| 34 |
+
|
| 35 |
+
# Clean and execute code
|
| 36 |
+
code = clean_code(code_response)
|
| 37 |
+
exec_env = {'df': df, 'plt': plt}
|
| 38 |
+
exec(code, exec_env)
|
| 39 |
+
|
| 40 |
+
# Prepare response
|
| 41 |
+
response = []
|
| 42 |
+
if 'result' in exec_env:
|
| 43 |
+
response.append(f"Analysis Result:\n{str(exec_env['result']}")
|
| 44 |
+
|
| 45 |
+
fig = exec_env.get('plt', plt).gcf()
|
| 46 |
+
if fig.get_axes():
|
| 47 |
+
fig.savefig("temp_plot.png")
|
| 48 |
+
response.append(gr.Image("temp_plot.png"))
|
| 49 |
+
|
| 50 |
+
return chat_history + [[query, "\n\n".join(response)]]
|
| 51 |
+
|
| 52 |
+
except Exception as e:
|
| 53 |
+
return chat_history + [[query, f"Error: {str(e)}"]]
|
| 54 |
+
|
| 55 |
+
def clean_code(code):
|
| 56 |
+
"""Sanitize generated code"""
|
| 57 |
+
# Remove dangerous patterns
|
| 58 |
+
forbidden_patterns = [
|
| 59 |
+
'os.', 'subprocess', 'sys.', 'shutil', 'open(',
|
| 60 |
+
'__', 'lambda', 'exec(', 'eval(', 'getattr'
|
| 61 |
+
]
|
| 62 |
+
|
| 63 |
+
for pattern in forbidden_patterns:
|
| 64 |
+
if pattern in code:
|
| 65 |
+
raise ValueError(f"Forbidden pattern detected: {pattern}")
|
| 66 |
+
|
| 67 |
+
return code
|
| 68 |
+
|
| 69 |
+
def handle_file(file):
|
| 70 |
+
"""Process uploaded CSV file"""
|
| 71 |
+
try:
|
| 72 |
+
return pd.read_csv(file.name)
|
| 73 |
+
except Exception as e:
|
| 74 |
+
raise gr.Error(f"Error loading CSV: {str(e)}")
|
| 75 |
+
|
| 76 |
+
with gr.Blocks(title="AI CSV Analyzer") as demo:
|
| 77 |
+
gr.Markdown("# 📊 AI-Powered CSV Analyzer")
|
| 78 |
+
gr.Markdown("Upload a CSV file and ask questions about your data!")
|
| 79 |
+
|
| 80 |
+
with gr.Row():
|
| 81 |
+
data_state = gr.State()
|
| 82 |
+
file_input = gr.UploadButton(label="Upload CSV", file_types=[".csv"])
|
| 83 |
+
|
| 84 |
+
chatbot = gr.Chatbot(height=400)
|
| 85 |
+
msg = gr.Textbox(label="Your Question")
|
| 86 |
+
clear = gr.ClearButton([msg, chatbot])
|
| 87 |
+
|
| 88 |
+
file_input.upload(
|
| 89 |
+
fn=handle_file,
|
| 90 |
+
inputs=file_input,
|
| 91 |
+
outputs=data_state
|
| 92 |
+
)
|
| 93 |
+
|
| 94 |
+
msg.submit(
|
| 95 |
+
fn=analyze_data,
|
| 96 |
+
inputs=[msg, data_state, chatbot],
|
| 97 |
+
outputs=chatbot,
|
| 98 |
+
queue=False
|
| 99 |
+
)
|
| 100 |
+
|
| 101 |
+
if __name__ == "__main__":
|
| 102 |
+
demo.launch()
|