Spaces:
Runtime error
Runtime error
import gradio as gr | |
import matplotlib.pyplot as plt | |
import numpy as np | |
import io | |
from PIL import Image | |
import graphviz | |
def generate_graph(code): | |
# Example: Assuming code is a multiplier for a simple function | |
try: | |
multiplier = float(code) | |
except ValueError: | |
multiplier = 1 | |
x = np.linspace(0, 10, 100) | |
y = np.sin(x) * multiplier | |
plt.figure() | |
plt.plot(x, y) | |
plt.title("Generated Graph") | |
buf = io.BytesIO() | |
plt.savefig(buf, format='png') | |
buf.seek(0) | |
img = Image.open(buf) | |
return img | |
def generate_diagram(code): | |
# Example: Use code as label text in the diagram | |
dot = graphviz.Digraph(comment='Diagram') | |
dot.node('A', 'Start') | |
dot.node('B', code) | |
dot.edge('A', 'B') | |
buf = io.BytesIO(dot.pipe(format='png')) | |
buf.seek(0) | |
img = Image.open(buf) | |
return img | |
def generate_mindmap(code): | |
# Example: Use code as label text in the mind map | |
dot = graphviz.Digraph(comment='Mind Map', graph_attr={'rankdir': 'LR'}) | |
dot.node('Root', 'Root') | |
dot.node('Child1', code) | |
dot.edge('Root', 'Child1') | |
buf = io.BytesIO(dot.pipe(format='png')) | |
buf.seek(0) | |
img = Image.open(buf) | |
return img | |
def main(graph_code, diagram_code, mindmap_code): | |
graph_img = generate_graph(graph_code) | |
diagram_img = generate_diagram(diagram_code) | |
mindmap_img = generate_mindmap(mindmap_code) | |
return graph_img, diagram_img, mindmap_img | |
interface = gr.Interface( | |
fn=main, | |
inputs=[ | |
gr.inputs.Textbox(lines=2, placeholder="Paste code or text for graph", label="Graph Code"), | |
gr.inputs.Textbox(lines=2, placeholder="Paste code or text for diagram", label="Diagram Code"), | |
gr.inputs.Textbox(lines=2, placeholder="Paste code or text for mind map", label="Mind Map Code") | |
], | |
outputs=[ | |
gr.outputs.Image(type="pil", label="Generated Graph"), | |
gr.outputs.Image(type="pil", label="Generated Diagram"), | |
gr.outputs.Image(type="pil", label="Generated Mind Map") | |
], | |
title="Advanced Visual Generator", | |
description="Paste code or text to generate graphs, diagrams, and mind maps." | |
) | |
if __name__ == "__main__": | |
interface.launch() |