Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -28,33 +28,6 @@ class CodeExecutionEnvironment:
|
|
| 28 |
except:
|
| 29 |
return False
|
| 30 |
|
| 31 |
-
def extract_and_execute_code(self, text):
|
| 32 |
-
"""Extract code blocks from markdown and execute them"""
|
| 33 |
-
# Pattern for code blocks
|
| 34 |
-
code_blocks = re.findall(r'```python(.*?)```', text, re.DOTALL)
|
| 35 |
-
if not code_blocks:
|
| 36 |
-
return text, None, []
|
| 37 |
-
|
| 38 |
-
all_outputs = []
|
| 39 |
-
all_figures = []
|
| 40 |
-
|
| 41 |
-
for code in code_blocks:
|
| 42 |
-
success, output, figures = self.execute_code(code.strip())
|
| 43 |
-
if success:
|
| 44 |
-
all_outputs.append(output)
|
| 45 |
-
all_figures.extend(figures)
|
| 46 |
-
else:
|
| 47 |
-
all_outputs.append(f"Error: {output}")
|
| 48 |
-
|
| 49 |
-
# Replace code blocks with code + output
|
| 50 |
-
modified_text = text
|
| 51 |
-
for i, (code, output) in enumerate(zip(code_blocks, all_outputs)):
|
| 52 |
-
code_section = f"```python{code}```"
|
| 53 |
-
output_section = f"\nOutput:\n```\n{output}\n```"
|
| 54 |
-
modified_text = modified_text.replace(code_section, code_section + output_section)
|
| 55 |
-
|
| 56 |
-
return modified_text, "\n".join(all_outputs), all_figures
|
| 57 |
-
|
| 58 |
def execute_code(self, code):
|
| 59 |
"""Execute code in the managed environment"""
|
| 60 |
# Redirect stdout to capture prints
|
|
@@ -93,14 +66,26 @@ class CodeExecutionEnvironment:
|
|
| 93 |
exec(code, self.globals_dict)
|
| 94 |
output = redirected_output.getvalue()
|
| 95 |
|
| 96 |
-
#
|
| 97 |
figures = []
|
|
|
|
|
|
|
| 98 |
if plt.get_figs():
|
| 99 |
for i, fig in enumerate(plt.get_figs()):
|
| 100 |
-
fig_path = os.path.join(self.figures_dir, f"
|
| 101 |
fig.savefig(fig_path)
|
| 102 |
figures.append(fig_path)
|
| 103 |
plt.close('all')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
|
| 105 |
return True, output, figures
|
| 106 |
|
|
@@ -109,6 +94,32 @@ class CodeExecutionEnvironment:
|
|
| 109 |
finally:
|
| 110 |
sys.stdout = old_stdout
|
| 111 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
def query_deepseek(prompt: str, api_key: str, system_prompt: str = None):
|
| 113 |
"""Send a prompt to DeepSeek API"""
|
| 114 |
headers = {
|
|
@@ -165,6 +176,7 @@ class ChatAndCodeInterface:
|
|
| 165 |
"The user might ask you to analyze data or generate visualizations. "
|
| 166 |
"When you write code, wrap it in ```python``` blocks. "
|
| 167 |
"You can use any Python library - they will be automatically installed. "
|
|
|
|
| 168 |
"\nUser message: " + message
|
| 169 |
)
|
| 170 |
|
|
@@ -203,8 +215,13 @@ def create_interface():
|
|
| 203 |
)
|
| 204 |
|
| 205 |
with gr.Column(scale=3):
|
| 206 |
-
chatbot = gr.Chatbot(height=
|
| 207 |
-
gallery = gr.Gallery(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 208 |
|
| 209 |
with gr.Row():
|
| 210 |
msg = gr.Textbox(
|
|
@@ -230,17 +247,18 @@ def create_interface():
|
|
| 230 |
|
| 231 |
Example prompts:
|
| 232 |
- "Create a histogram of the numerical columns"
|
| 233 |
-
- "
|
| 234 |
-
- "
|
|
|
|
| 235 |
|
| 236 |
The assistant will:
|
| 237 |
- Generate and execute Python code automatically
|
| 238 |
-
-
|
| 239 |
-
-
|
| 240 |
""")
|
| 241 |
|
| 242 |
return demo
|
| 243 |
|
| 244 |
if __name__ == "__main__":
|
| 245 |
demo = create_interface()
|
| 246 |
-
demo.launch()
|
|
|
|
| 28 |
except:
|
| 29 |
return False
|
| 30 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
def execute_code(self, code):
|
| 32 |
"""Execute code in the managed environment"""
|
| 33 |
# Redirect stdout to capture prints
|
|
|
|
| 66 |
exec(code, self.globals_dict)
|
| 67 |
output = redirected_output.getvalue()
|
| 68 |
|
| 69 |
+
# Handle different types of figures
|
| 70 |
figures = []
|
| 71 |
+
|
| 72 |
+
# Handle Matplotlib figures
|
| 73 |
if plt.get_figs():
|
| 74 |
for i, fig in enumerate(plt.get_figs()):
|
| 75 |
+
fig_path = os.path.join(self.figures_dir, f"mpl_figure_{len(figures)}.png")
|
| 76 |
fig.savefig(fig_path)
|
| 77 |
figures.append(fig_path)
|
| 78 |
plt.close('all')
|
| 79 |
+
|
| 80 |
+
# Handle Plotly figures
|
| 81 |
+
if 'fig' in self.globals_dict and 'plotly' in str(type(self.globals_dict['fig'])):
|
| 82 |
+
fig = self.globals_dict['fig']
|
| 83 |
+
fig_path = os.path.join(self.figures_dir, f"plotly_figure_{len(figures)}.html")
|
| 84 |
+
fig.write_html(fig_path)
|
| 85 |
+
# Also save as image for gallery display
|
| 86 |
+
img_path = os.path.join(self.figures_dir, f"plotly_figure_{len(figures)}.png")
|
| 87 |
+
fig.write_image(img_path)
|
| 88 |
+
figures.append(img_path)
|
| 89 |
|
| 90 |
return True, output, figures
|
| 91 |
|
|
|
|
| 94 |
finally:
|
| 95 |
sys.stdout = old_stdout
|
| 96 |
|
| 97 |
+
def extract_and_execute_code(self, text):
|
| 98 |
+
"""Extract code blocks from markdown and execute them"""
|
| 99 |
+
code_blocks = re.findall(r'```python(.*?)```', text, re.DOTALL)
|
| 100 |
+
if not code_blocks:
|
| 101 |
+
return text, None, []
|
| 102 |
+
|
| 103 |
+
all_outputs = []
|
| 104 |
+
all_figures = []
|
| 105 |
+
|
| 106 |
+
for code in code_blocks:
|
| 107 |
+
success, output, figures = self.execute_code(code.strip())
|
| 108 |
+
if success:
|
| 109 |
+
all_outputs.append(output)
|
| 110 |
+
all_figures.extend(figures)
|
| 111 |
+
else:
|
| 112 |
+
all_outputs.append(f"Error: {output}")
|
| 113 |
+
|
| 114 |
+
# Replace code blocks with code + output
|
| 115 |
+
modified_text = text
|
| 116 |
+
for i, (code, output) in enumerate(zip(code_blocks, all_outputs)):
|
| 117 |
+
code_section = f"```python{code}```"
|
| 118 |
+
output_section = f"\nOutput:\n```\n{output}\n```"
|
| 119 |
+
modified_text = modified_text.replace(code_section, code_section + output_section)
|
| 120 |
+
|
| 121 |
+
return modified_text, "\n".join(all_outputs), all_figures
|
| 122 |
+
|
| 123 |
def query_deepseek(prompt: str, api_key: str, system_prompt: str = None):
|
| 124 |
"""Send a prompt to DeepSeek API"""
|
| 125 |
headers = {
|
|
|
|
| 176 |
"The user might ask you to analyze data or generate visualizations. "
|
| 177 |
"When you write code, wrap it in ```python``` blocks. "
|
| 178 |
"You can use any Python library - they will be automatically installed. "
|
| 179 |
+
"For interactive maps, use plotly.express and ensure you install required dependencies. "
|
| 180 |
"\nUser message: " + message
|
| 181 |
)
|
| 182 |
|
|
|
|
| 215 |
)
|
| 216 |
|
| 217 |
with gr.Column(scale=3):
|
| 218 |
+
chatbot = gr.Chatbot(height=500)
|
| 219 |
+
gallery = gr.Gallery(
|
| 220 |
+
label="Generated Visualizations",
|
| 221 |
+
columns=2,
|
| 222 |
+
height=400,
|
| 223 |
+
object_fit="contain"
|
| 224 |
+
)
|
| 225 |
|
| 226 |
with gr.Row():
|
| 227 |
msg = gr.Textbox(
|
|
|
|
| 247 |
|
| 248 |
Example prompts:
|
| 249 |
- "Create a histogram of the numerical columns"
|
| 250 |
+
- "Generate an interactive map of the locations"
|
| 251 |
+
- "Show the correlation between variables"
|
| 252 |
+
- "Create a summary dashboard of key metrics"
|
| 253 |
|
| 254 |
The assistant will:
|
| 255 |
- Generate and execute Python code automatically
|
| 256 |
+
- Handle both static and interactive visualizations
|
| 257 |
+
- Show code, output, and visualizations in one place
|
| 258 |
""")
|
| 259 |
|
| 260 |
return demo
|
| 261 |
|
| 262 |
if __name__ == "__main__":
|
| 263 |
demo = create_interface()
|
| 264 |
+
demo.launch()
|