ZahirJS commited on
Commit
9912372
·
verified ·
1 Parent(s): d297186

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -20
app.py CHANGED
@@ -1,12 +1,12 @@
1
  import gradio as gr
2
  import json
3
  from graphviz import Digraph
4
- import os
5
- from typing import Dict, Any
6
 
7
  def generate_concept_map(json_input: str) -> str:
8
  """
9
- Generates a concept map from structured JSON
10
 
11
  Args:
12
  json_input (str): JSON describing the concept map structure.
@@ -24,19 +24,14 @@ def generate_concept_map(json_input: str) -> str:
24
  }
25
 
26
  Returns:
27
- str: Image URL (compatible with Hugging Face Spaces)
28
-
29
- Raises:
30
- ValueError: If JSON doesn't meet required format
31
  """
32
  try:
33
- # Validate input
34
  if not json_input.strip():
35
  return "Error: Empty input"
36
 
37
  data = json.loads(json_input)
38
 
39
- # Validate basic structure
40
  if 'central_node' not in data or 'nodes' not in data:
41
  raise ValueError("Missing required fields: central_node or nodes")
42
 
@@ -119,15 +114,13 @@ def generate_concept_map(json_input: str) -> str:
119
  fontsize='8'
120
  )
121
 
122
- # Save image
123
- filename = f"/tmp/concept_map_{hash(json_input)}.gv"
124
- dot.render(filename, format='png', cleanup=True)
125
-
126
- # Return Hugging Face compatible URL
127
- return f"/file={filename}.png"
128
 
129
  except json.JSONDecodeError:
130
- return "Error: Invalid JSON"
131
  except Exception as e:
132
  return f"Error: {str(e)}"
133
 
@@ -141,16 +134,15 @@ if __name__ == "__main__":
141
  ),
142
  outputs=gr.Textbox(
143
  label="Image URL",
144
- placeholder="Concept map URL will be generated here"
145
  ),
146
  title="Concept Map Generator",
147
- description="Create concept maps from JSON (Claude compatible)"
148
  )
149
 
150
- # Hugging Face Spaces configuration
151
  demo.launch(
152
  mcp_server=True,
153
  share=False,
154
- server_port=int(os.getenv('PORT', 7860)),
155
  server_name="0.0.0.0"
156
  )
 
1
  import gradio as gr
2
  import json
3
  from graphviz import Digraph
4
+ import io
5
+ import base64
6
 
7
  def generate_concept_map(json_input: str) -> str:
8
  """
9
+ Generate concept map from JSON and return as base64 image
10
 
11
  Args:
12
  json_input (str): JSON describing the concept map structure.
 
24
  }
25
 
26
  Returns:
27
+ str: Base64 data URL of the generated concept map
 
 
 
28
  """
29
  try:
 
30
  if not json_input.strip():
31
  return "Error: Empty input"
32
 
33
  data = json.loads(json_input)
34
 
 
35
  if 'central_node' not in data or 'nodes' not in data:
36
  raise ValueError("Missing required fields: central_node or nodes")
37
 
 
114
  fontsize='8'
115
  )
116
 
117
+ # Convert to base64 image
118
+ img_data = dot.pipe(format='png')
119
+ img_base64 = base64.b64encode(img_data).decode()
120
+ return f"data:image/png;base64,{img_base64}"
 
 
121
 
122
  except json.JSONDecodeError:
123
+ return "Error: Invalid JSON format"
124
  except Exception as e:
125
  return f"Error: {str(e)}"
126
 
 
134
  ),
135
  outputs=gr.Textbox(
136
  label="Image URL",
137
+ placeholder="Base64 image URL will appear here"
138
  ),
139
  title="Concept Map Generator",
140
+ description="Create concept maps from JSON for web display"
141
  )
142
 
 
143
  demo.launch(
144
  mcp_server=True,
145
  share=False,
146
+ server_port=7860,
147
  server_name="0.0.0.0"
148
  )