bluenevus commited on
Commit
5be932a
·
verified ·
1 Parent(s): 218ce96

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +71 -49
app.py CHANGED
@@ -13,79 +13,101 @@ def process_file(api_key, file, instructions):
13
 
14
  try:
15
  df = pd.read_csv(file.name) if file.name.endswith('.csv') else pd.read_excel(file.name)
 
 
16
  except Exception as e:
17
- print(f"File Error: {str(e)}")
18
- return [None]*3
19
 
20
- # Enhanced prompt with strict syntax rules
21
- prompt = f"""Generate 3 Python code blocks for matplotlib visualizations with:
22
- 1. Perfect Python syntax
23
- 2. No markdown or incomplete lines
24
- 3. Each block must start with:
 
 
 
 
25
  plt.figure(figsize=(16,9), dpi=120)
26
  plt.style.use('ggplot')
27
- 4. Use ONLY df and plt variables
28
- 5. End with plt.tight_layout()
29
 
30
- Columns: {list(df.columns)}
31
- Sample: {df.head(3).to_dict()}
32
- Instructions: {instructions or 'None'}
33
 
34
  Format EXACTLY as:
35
  # Visualization 1
36
- [code]
37
-
38
- # Visualization 2
39
- [code]
40
-
41
- # Visualization 3
42
- [code]
43
  """
44
 
45
  try:
46
  response = model.generate_content(prompt)
47
  code_blocks = re.split(r'# Visualization \d+', response.text)[1:4]
48
  except Exception as e:
49
- print(f"Gemini Error: {str(e)}")
50
- return [None]*3
51
 
52
  visualizations = []
53
  for i, block in enumerate(code_blocks, 1):
54
- buf = io.BytesIO()
55
  try:
56
- # Advanced code cleaning
57
- cleaned_code = '\n'.join(
58
- line.strip().replace("'", "").replace('"', '') # Remove stray quotes
59
- for line in block.split('\n')
60
- if line.strip() and
61
- not any(c in line for c in ['`', '```', 'Annotation']) and
62
- re.match(r'^[a-zA-Z0-9_().=, <>:]+$', line) # Basic syntax validation
63
- )
64
 
65
- # Add missing parentheses check
66
- cleaned_code = re.sub(r'plt.style.use\([\'"]ggplot$',
67
- 'plt.style.use("ggplot")', cleaned_code)
68
-
69
- # Syntax validation
70
  ast.parse(cleaned_code)
71
-
72
- # Execute code
73
- exec_env = {'df': df, 'plt': plt}
74
- plt.figure(figsize=(16, 9), dpi=120)
75
- exec(cleaned_code, exec_env)
76
-
77
- plt.savefig(buf, format='png', bbox_inches='tight')
78
- plt.close()
79
- visualizations.append(Image.open(buf))
80
  except Exception as e:
81
  print(f"Visualization {i} Error: {str(e)}")
82
- print(f"Cleaned Code:\n{cleaned_code}")
83
- visualizations.append(None)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
84
 
85
- return visualizations + [None]*(3-len(visualizations))
 
 
 
86
 
87
  # Gradio interface
88
- with gr.Blocks() as demo:
89
  gr.Markdown("# Professional Data Visualizer")
90
 
91
  with gr.Row():
@@ -93,7 +115,7 @@ with gr.Blocks() as demo:
93
  file = gr.File(label="Upload Data File", file_types=[".csv", ".xlsx"])
94
 
95
  instructions = gr.Textbox(label="Visualization Instructions")
96
- submit = gr.Button("Generate", variant="primary")
97
 
98
  with gr.Row():
99
  outputs = [gr.Image(label=f"Visualization {i+1}", width=600) for i in range(3)]
 
13
 
14
  try:
15
  df = pd.read_csv(file.name) if file.name.endswith('.csv') else pd.read_excel(file.name)
16
+ if df.empty:
17
+ raise ValueError("Uploaded file contains no data")
18
  except Exception as e:
19
+ print(f"Data Error: {str(e)}")
20
+ return [generate_error_image(str(e))]*3
21
 
22
+ # Enhanced prompt with strict plotting requirements
23
+ prompt = f"""Generate 3 matplotlib codes with these rules:
24
+ 1. Use ONLY these variables: df (DataFrame), plt
25
+ 2. Each visualization MUST:
26
+ - Plot actual data from df
27
+ - Include title, axis labels, and data labels if needed
28
+ - Use clear color schemes
29
+ - Avoid empty plots
30
+ 3. Code structure:
31
  plt.figure(figsize=(16,9), dpi=120)
32
  plt.style.use('ggplot')
33
+ # Plotting code using df columns: {list(df.columns)}
34
+ plt.tight_layout()
35
 
36
+ Sample data: {df.head(3).to_dict()}
37
+ User instructions: {instructions or 'General insights'}
 
38
 
39
  Format EXACTLY as:
40
  # Visualization 1
41
+ [complete code]
 
 
 
 
 
 
42
  """
43
 
44
  try:
45
  response = model.generate_content(prompt)
46
  code_blocks = re.split(r'# Visualization \d+', response.text)[1:4]
47
  except Exception as e:
48
+ return [generate_error_image("API Error")]*3
 
49
 
50
  visualizations = []
51
  for i, block in enumerate(code_blocks, 1):
 
52
  try:
53
+ # Advanced code sanitization
54
+ cleaned_code = sanitize_code(block, df.columns)
 
 
 
 
 
 
55
 
56
+ # Validate and execute
 
 
 
 
57
  ast.parse(cleaned_code)
58
+ img = execute_plot_code(cleaned_code, df)
59
+ visualizations.append(img)
 
 
 
 
 
 
 
60
  except Exception as e:
61
  print(f"Visualization {i} Error: {str(e)}")
62
+ visualizations.append(generate_error_image(f"Plot {i} Error"))
63
+
64
+ return visualizations + [generate_error_image("Not Generated")]*(3-len(visualizations))
65
+
66
+ def sanitize_code(code_block, columns):
67
+ """Clean and validate generated code"""
68
+ replacements = {
69
+ r"'y_axis'": f"'{columns[1]}'" if len(columns) > 1 else "'Value'",
70
+ r"'x_axis'": f"'{columns[0]}'",
71
+ r"data": "df",
72
+ r"plt.legend\(\)": "" # Remove empty legend calls
73
+ }
74
+
75
+ cleaned = []
76
+ for line in code_block.split('\n'):
77
+ line = line.strip()
78
+ if not line or line.startswith('`'):
79
+ continue
80
+
81
+ # Apply replacements
82
+ for pattern, replacement in replacements.items():
83
+ line = re.sub(pattern, replacement, line)
84
+
85
+ cleaned.append(line)
86
+
87
+ return '\n'.join(cleaned)
88
+
89
+ def execute_plot_code(code, df):
90
+ """Safely execute plotting code"""
91
+ buf = io.BytesIO()
92
+ plt.figure(figsize=(16, 9), dpi=120)
93
+ plt.style.use('ggplot')
94
+
95
+ try:
96
+ exec(code, {'df': df, 'plt': plt})
97
+ plt.tight_layout()
98
+ plt.savefig(buf, format='png', bbox_inches='tight')
99
+ buf.seek(0)
100
+ return Image.open(buf)
101
+ finally:
102
+ plt.close()
103
 
104
+ def generate_error_image(message):
105
+ """Create error indication image"""
106
+ img = Image.new('RGB', (1920, 1080), color=(73, 109, 137))
107
+ return img
108
 
109
  # Gradio interface
110
+ with gr.Blocks(theme=gr.themes.Default(spacing_size="lg")) as demo:
111
  gr.Markdown("# Professional Data Visualizer")
112
 
113
  with gr.Row():
 
115
  file = gr.File(label="Upload Data File", file_types=[".csv", ".xlsx"])
116
 
117
  instructions = gr.Textbox(label="Visualization Instructions")
118
+ submit = gr.Button("Generate Insights", variant="primary")
119
 
120
  with gr.Row():
121
  outputs = [gr.Image(label=f"Visualization {i+1}", width=600) for i in range(3)]