bluenevus commited on
Commit
ccd7bab
·
verified ·
1 Parent(s): 1e2b302

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -7
app.py CHANGED
@@ -4,6 +4,7 @@ import matplotlib.pyplot as plt
4
  import io
5
  import base64
6
  import google.generativeai as genai
 
7
 
8
  def process_file(api_key, file, instructions):
9
  # Set up Gemini API
@@ -29,10 +30,9 @@ def process_file(api_key, file, instructions):
29
 
30
  User instructions: {instructions if instructions else 'No specific instructions provided.'}
31
 
32
- Generate Python code for 3 different visualizations using matplotlib. Each visualization should be unique and provide insights into the data. Do not include any explanations or descriptions, only the Python code for each visualization.
33
 
34
  Format your response as:
35
- ```python
36
  # Visualization 1
37
  # Your code here
38
 
@@ -41,21 +41,24 @@ def process_file(api_key, file, instructions):
41
 
42
  # Visualization 3
43
  # Your code here
44
- ```
45
  """
46
 
47
  response = model.generate_content(prompt)
48
  code = response.text.strip()
49
 
50
- # Extract code from markdown code block if present
51
- if "```python" in code and "```" in code:
52
- code = code.split("```python")[1].split("```")[0].strip()
53
 
54
  visualizations = []
55
  for i, viz_code in enumerate(code.split("# Visualization")[1:4], 1):
56
  plt.figure(figsize=(10, 6))
57
  try:
58
- exec(viz_code, {'df': df, 'plt': plt})
 
 
 
 
 
59
  plt.title(f"Visualization {i}")
60
 
61
  # Save the plot to a BytesIO object
 
4
  import io
5
  import base64
6
  import google.generativeai as genai
7
+ import textwrap
8
 
9
  def process_file(api_key, file, instructions):
10
  # Set up Gemini API
 
30
 
31
  User instructions: {instructions if instructions else 'No specific instructions provided.'}
32
 
33
+ Generate Python code for 3 different visualizations using matplotlib. Each visualization should be unique and provide insights into the data. Do not include any explanations or descriptions, only the Python code for each visualization. Ensure the code is not indented.
34
 
35
  Format your response as:
 
36
  # Visualization 1
37
  # Your code here
38
 
 
41
 
42
  # Visualization 3
43
  # Your code here
 
44
  """
45
 
46
  response = model.generate_content(prompt)
47
  code = response.text.strip()
48
 
49
+ print("Generated code:")
50
+ print(code)
 
51
 
52
  visualizations = []
53
  for i, viz_code in enumerate(code.split("# Visualization")[1:4], 1):
54
  plt.figure(figsize=(10, 6))
55
  try:
56
+ # Remove any leading spaces from each line
57
+ cleaned_code = '\n'.join(line.strip() for line in viz_code.split('\n') if line.strip())
58
+ print(f"\nVisualization {i} code:")
59
+ print(cleaned_code)
60
+
61
+ exec(cleaned_code, {'df': df, 'plt': plt})
62
  plt.title(f"Visualization {i}")
63
 
64
  # Save the plot to a BytesIO object