Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
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__":
|
71 |
+
interface.launch()
|