Sugamdeol commited on
Commit
724fe2b
·
verified ·
1 Parent(s): 3b0eb88

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -49
app.py CHANGED
@@ -3,68 +3,41 @@ import matplotlib.pyplot as plt
3
  import numpy as np
4
  import io
5
  from PIL import Image
6
- import graphviz
7
 
8
- def generate_graph(code):
9
- # Example: Assuming code is a multiplier for a simple function
 
 
10
  try:
11
- multiplier = float(code)
12
- except ValueError:
13
- multiplier = 1
14
-
 
 
 
 
15
  x = np.linspace(0, 10, 100)
16
  y = np.sin(x) * multiplier
 
17
  plt.figure()
18
  plt.plot(x, y)
19
  plt.title("Generated Graph")
20
- buf = io.BytesIO()
 
 
21
  plt.savefig(buf, format='png')
22
  buf.seek(0)
23
  img = Image.open(buf)
24
  return img
25
 
26
- def generate_diagram(code):
27
- # Example: Use code as label text in the diagram
28
- dot = graphviz.Digraph(comment='Diagram')
29
- dot.node('A', 'Start')
30
- dot.node('B', code)
31
- dot.edge('A', 'B')
32
- buf = io.BytesIO(dot.pipe(format='png'))
33
- buf.seek(0)
34
- img = Image.open(buf)
35
- return img
36
-
37
- def generate_mindmap(code):
38
- # Example: Use code as label text in the mind map
39
- dot = graphviz.Digraph(comment='Mind Map', graph_attr={'rankdir': 'LR'})
40
- dot.node('Root', 'Root')
41
- dot.node('Child1', code)
42
- dot.edge('Root', 'Child1')
43
- buf = io.BytesIO(dot.pipe(format='png'))
44
- buf.seek(0)
45
- img = Image.open(buf)
46
- return img
47
-
48
- def main(graph_code, diagram_code, mindmap_code):
49
- graph_img = generate_graph(graph_code)
50
- diagram_img = generate_diagram(diagram_code)
51
- mindmap_img = generate_mindmap(mindmap_code)
52
- return graph_img, diagram_img, mindmap_img
53
-
54
  interface = gr.Interface(
55
- fn=main,
56
- inputs=[
57
- gr.inputs.Textbox(lines=2, placeholder="Paste code or text for graph", label="Graph Code"),
58
- gr.inputs.Textbox(lines=2, placeholder="Paste code or text for diagram", label="Diagram Code"),
59
- gr.inputs.Textbox(lines=2, placeholder="Paste code or text for mind map", label="Mind Map Code")
60
- ],
61
- outputs=[
62
- gr.outputs.Image(type="pil", label="Generated Graph"),
63
- gr.outputs.Image(type="pil", label="Generated Diagram"),
64
- gr.outputs.Image(type="pil", label="Generated Mind Map")
65
- ],
66
- title="Advanced Visual Generator",
67
- description="Paste code or text to generate graphs, diagrams, and mind maps."
68
  )
69
 
70
  if __name__ == "__main__":
 
3
  import numpy as np
4
  import io
5
  from PIL import Image
 
6
 
7
+ def generate_graph_from_code(code):
8
+ # Create a buffer to capture output
9
+ buf = io.BytesIO()
10
+
11
  try:
12
+ # Define a dummy multiplier if not present in the code
13
+ exec(code)
14
+ if 'multiplier' not in locals():
15
+ multiplier = 1
16
+ except Exception as e:
17
+ return str(e)
18
+
19
+ # Generate the graph
20
  x = np.linspace(0, 10, 100)
21
  y = np.sin(x) * multiplier
22
+
23
  plt.figure()
24
  plt.plot(x, y)
25
  plt.title("Generated Graph")
26
+ plt.xlabel("x")
27
+ plt.ylabel("y")
28
+ plt.grid(True)
29
  plt.savefig(buf, format='png')
30
  buf.seek(0)
31
  img = Image.open(buf)
32
  return img
33
 
34
+ # Define the Gradio interface
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35
  interface = gr.Interface(
36
+ fn=generate_graph_from_code,
37
+ inputs=gr.inputs.Textbox(lines=10, placeholder="Paste your Python code here", label="Python Code"),
38
+ outputs=gr.outputs.Image(type="pil", label="Generated Graph"),
39
+ title="Python Code to Graph Generator",
40
+ description="Paste Python code that defines a variable `multiplier` to generate a graph."
 
 
 
 
 
 
 
 
41
  )
42
 
43
  if __name__ == "__main__":