Update app.py via AI Editor
Browse files
app.py
CHANGED
|
@@ -96,9 +96,6 @@ def clear_session(sid):
|
|
| 96 |
logging.error(f"Error clearing session {sid}: {e}")
|
| 97 |
|
| 98 |
def parse_ai_response_to_graphviz(ai_response):
|
| 99 |
-
# Simple heuristic to parse AI response to Graphviz digraph elements.
|
| 100 |
-
# If the AI response is in markdown or text, try to extract nodes and edges.
|
| 101 |
-
# This is a minimal approach; for more complex parsing, extend as needed.
|
| 102 |
lines = [line.strip() for line in ai_response.split("\n") if line.strip()]
|
| 103 |
edges = []
|
| 104 |
nodes = set()
|
|
@@ -124,20 +121,19 @@ def parse_ai_response_to_graphviz(ai_response):
|
|
| 124 |
return nodes, edges
|
| 125 |
|
| 126 |
def generate_diagram(description, sid):
|
| 127 |
-
sdir = get_session_dir(sid)
|
| 128 |
-
img_path = sdir / "diagram.png"
|
| 129 |
-
# Parse the description into nodes and edges
|
| 130 |
nodes, edges = parse_ai_response_to_graphviz(description)
|
| 131 |
dot = graphviz.Digraph(format="png")
|
| 132 |
for node in nodes:
|
| 133 |
dot.node(node)
|
| 134 |
for src, dst in edges:
|
| 135 |
dot.edge(src, dst)
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
|
|
|
|
|
|
| 141 |
|
| 142 |
def get_ai_response(model, prompt):
|
| 143 |
if model == 'gpt-3.5-turbo':
|
|
|
|
| 96 |
logging.error(f"Error clearing session {sid}: {e}")
|
| 97 |
|
| 98 |
def parse_ai_response_to_graphviz(ai_response):
|
|
|
|
|
|
|
|
|
|
| 99 |
lines = [line.strip() for line in ai_response.split("\n") if line.strip()]
|
| 100 |
edges = []
|
| 101 |
nodes = set()
|
|
|
|
| 121 |
return nodes, edges
|
| 122 |
|
| 123 |
def generate_diagram(description, sid):
|
|
|
|
|
|
|
|
|
|
| 124 |
nodes, edges = parse_ai_response_to_graphviz(description)
|
| 125 |
dot = graphviz.Digraph(format="png")
|
| 126 |
for node in nodes:
|
| 127 |
dot.node(node)
|
| 128 |
for src, dst in edges:
|
| 129 |
dot.edge(src, dst)
|
| 130 |
+
try:
|
| 131 |
+
img_bytes = dot.pipe(format="png")
|
| 132 |
+
diagram_base64 = base64.b64encode(img_bytes).decode('utf-8')
|
| 133 |
+
return diagram_base64
|
| 134 |
+
except Exception as e:
|
| 135 |
+
logging.error(f"Graphviz error: {e}")
|
| 136 |
+
raise Exception("Graphviz rendering failed. Please check your description or try again.")
|
| 137 |
|
| 138 |
def get_ai_response(model, prompt):
|
| 139 |
if model == 'gpt-3.5-turbo':
|