Skip to content

Commit d3edf24

Browse files
authored
Remove redundant check
1 parent 1f92f67 commit d3edf24

File tree

1 file changed

+40
-45
lines changed

1 file changed

+40
-45
lines changed

src/axiomatic/magic.py

Lines changed: 40 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -26,54 +26,49 @@ def ax_api(self, *args, **kwargs):
2626
pass
2727

2828
def axquery(self, query, cell=None):
29-
if self.api_key:
30-
if cell:
31-
# REFINE
32-
try:
33-
exec(cell)
34-
feedback = "Code executed successfully."
35-
except Exception as e:
36-
feedback = f"Errors:\n{e}"
37-
print(feedback)
38-
current_query = f"{self.query}\n{query}"
39-
result = self.client.pic.refine(
40-
query=current_query, code=cell, feedback=feedback
29+
if cell:
30+
# REFINE
31+
try:
32+
exec(cell)
33+
feedback = "Code executed successfully."
34+
except Exception as e:
35+
feedback = f"Errors:\n{e}"
36+
print(feedback)
37+
current_query = f"{self.query}\n{query}"
38+
result = self.client.pic.refine(
39+
query=current_query, code=cell, feedback=feedback
40+
)
41+
else:
42+
# GENERATE FROM SCRATCH
43+
self.query = query
44+
result = self.client.pic.generate(query=query)
45+
46+
# Process output
47+
pre_thought = result.raw_content.split("<thought>")[0]
48+
thought = result.thought_text.replace("\n", "<br>")
49+
if not thought:
50+
output = result.raw_content.replace("\n", "<br>")
51+
else:
52+
output = pre_thought + "<br>" + thought
53+
html_content = f"""<div style='font-family: Arial, sans-serif; line-height: 1.5;'>"
54+
<div style='color: #6EB700;'><strong>AX:</strong> {output}</div>"""
55+
display(HTML(html_content))
56+
57+
# Process code
58+
# remove last three lines (saving file)
59+
if result.code:
60+
code = "\n".join(result.code.split("\n")[:-3] + ["c"])
61+
if "google.colab" in sys.modules:
62+
# When running in colab
63+
from google.colab import _frontend # type: ignore
64+
65+
_frontend.create_scratch_cell(
66+
f"""# {query}\n# %%ax_fix\n{code}""", bottom_pane=True
4167
)
4268
else:
43-
# GENERATE FROM SCRATCH
44-
self.query = query
45-
result = self.client.pic.generate(query=query)
46-
47-
# Process output
48-
pre_thought = result.raw_content.split("<thought>")[0]
49-
thought = result.thought_text.replace("\n", "<br>")
50-
if not thought:
51-
output = result.raw_content.replace("\n", "<br>")
52-
else:
53-
output = pre_thought + "<br>" + thought
54-
html_content = f"""<div style='font-family: Arial, sans-serif; line-height: 1.5;'>"
55-
<div style='color: #6EB700;'><strong>AX:</strong> {output}</div>"""
56-
display(HTML(html_content))
57-
58-
# Process code
59-
# remove last three lines (saving file)
60-
if result.code:
61-
code = "\n".join(result.code.split("\n")[:-3] + ["c"])
62-
if "google.colab" in sys.modules:
63-
# When running in colab
64-
from google.colab import _frontend # type: ignore
65-
66-
_frontend.create_scratch_cell(
67-
f"""# {query}\n# %%ax_fix\n{code}""", bottom_pane=True
68-
)
69-
else:
70-
# When running in jupyter
71-
get_ipython().set_next_input(f"# %%ax_fix\n{code}", replace=False)
69+
# When running in jupyter
70+
get_ipython().set_next_input(f"# %%ax_fix\n{code}", replace=False)
7271

73-
else:
74-
print(
75-
"Please set your Axiomatic API key first with the command %ax_api API_KEY and restart the kernel. Request the api key at our Customer Service."
76-
)
7772

7873
def ax_query(self, query, cell=None):
7974
# Updates the target circuit query

0 commit comments

Comments
 (0)