Spaces:
Running
Running
Update timeline_generator.py
Browse files- timeline_generator.py +1 -155
timeline_generator.py
CHANGED
@@ -1,157 +1,3 @@
|
|
1 |
-
# import graphviz
|
2 |
-
# import json
|
3 |
-
# from tempfile import NamedTemporaryFile
|
4 |
-
# import os
|
5 |
-
|
6 |
-
# def generate_timeline_diagram(json_input: str, output_format: str) -> str:
|
7 |
-
# """
|
8 |
-
# Generates a serpentine timeline diagram from JSON input.
|
9 |
-
|
10 |
-
# Args:
|
11 |
-
# json_input (str): A JSON string describing the timeline structure.
|
12 |
-
# It must follow the Expected JSON Format Example below.
|
13 |
-
|
14 |
-
# Expected JSON Format Example:
|
15 |
-
# {
|
16 |
-
# "title": "AI Development Timeline",
|
17 |
-
# "events_per_row": 4,
|
18 |
-
# "events": [
|
19 |
-
# {
|
20 |
-
# "id": "event_1",
|
21 |
-
# "label": "Machine Learning Foundations",
|
22 |
-
# "date": "1950-1960",
|
23 |
-
# "description": "Early neural networks and perceptrons"
|
24 |
-
# },
|
25 |
-
# {
|
26 |
-
# "id": "event_2",
|
27 |
-
# "label": "Expert Systems Era",
|
28 |
-
# "date": "1970-1980",
|
29 |
-
# "description": "Rule-based AI systems"
|
30 |
-
# },
|
31 |
-
# {
|
32 |
-
# "id": "event_3",
|
33 |
-
# "label": "Neural Network Revival",
|
34 |
-
# "date": "1980-1990",
|
35 |
-
# "description": "Backpropagation algorithm"
|
36 |
-
# }
|
37 |
-
# ]
|
38 |
-
# }
|
39 |
-
|
40 |
-
# Returns:
|
41 |
-
# str: The filepath to the generated PNG image file.
|
42 |
-
# """
|
43 |
-
# try:
|
44 |
-
# if not json_input.strip():
|
45 |
-
# return "Error: Empty input"
|
46 |
-
|
47 |
-
# data = json.loads(json_input)
|
48 |
-
|
49 |
-
# if 'events' not in data:
|
50 |
-
# raise ValueError("Missing required field: events")
|
51 |
-
|
52 |
-
# dot = graphviz.Digraph(
|
53 |
-
# name='Timeline',
|
54 |
-
# format='png',
|
55 |
-
# graph_attr={
|
56 |
-
# 'rankdir': 'TB', # Top-to-Bottom
|
57 |
-
# 'splines': 'ortho', # Straight lines with 90-degree bends
|
58 |
-
# 'bgcolor': 'white', # White background
|
59 |
-
# 'pad': '0.8', # Padding around the graph
|
60 |
-
# 'nodesep': '3.0', # Increased spacing between nodes horizontally
|
61 |
-
# 'ranksep': '2.5' # Increased spacing between ranks vertically
|
62 |
-
# }
|
63 |
-
# )
|
64 |
-
|
65 |
-
# # base_color = '#19191a'
|
66 |
-
# base_color = '#BEBEBE'
|
67 |
-
|
68 |
-
# title = data.get('title', '')
|
69 |
-
# events = data.get('events', [])
|
70 |
-
# events_per_row = data.get('events_per_row', 4)
|
71 |
-
|
72 |
-
# if not events:
|
73 |
-
# raise ValueError("Timeline must contain at least one event")
|
74 |
-
|
75 |
-
# if title:
|
76 |
-
# dot.node(
|
77 |
-
# 'title',
|
78 |
-
# title,
|
79 |
-
# shape='plaintext',
|
80 |
-
# fontsize='18',
|
81 |
-
# fontweight='bold',
|
82 |
-
# fontcolor=base_color,
|
83 |
-
# pos="6,2!"
|
84 |
-
# )
|
85 |
-
|
86 |
-
# total_events = len(events)
|
87 |
-
|
88 |
-
# for i, event in enumerate(events):
|
89 |
-
# event_id = event.get('id', f'event_{i}')
|
90 |
-
# event_label = event.get('label', f'Event {i+1}')
|
91 |
-
# event_date = event.get('date', '')
|
92 |
-
# event_description = event.get('description', '')
|
93 |
-
|
94 |
-
# if event_date and event_description:
|
95 |
-
# full_label = f"{event_date}\\n{event_label}\\n{event_description}"
|
96 |
-
# elif event_date:
|
97 |
-
# full_label = f"{event_date}\\n{event_label}"
|
98 |
-
# elif event_description:
|
99 |
-
# full_label = f"{event_label}\\n{event_description}"
|
100 |
-
# else:
|
101 |
-
# full_label = event_label
|
102 |
-
|
103 |
-
# if total_events == 1:
|
104 |
-
# opacity = 'FF'
|
105 |
-
# else:
|
106 |
-
# opacity_value = int(255 * (1.0 - (i * 0.7 / (total_events - 1))))
|
107 |
-
# opacity = format(opacity_value, '02x')
|
108 |
-
|
109 |
-
# node_color = f"{base_color}{opacity}"
|
110 |
-
# font_color = 'white' if i < total_events * 0.7 else 'black'
|
111 |
-
|
112 |
-
# row = i // events_per_row
|
113 |
-
# col = i % events_per_row
|
114 |
-
|
115 |
-
# if row % 2 == 1:
|
116 |
-
# visual_col = events_per_row - 1 - col
|
117 |
-
# else:
|
118 |
-
# visual_col = col
|
119 |
-
|
120 |
-
# dot.node(
|
121 |
-
# event_id,
|
122 |
-
# full_label,
|
123 |
-
# shape='box',
|
124 |
-
# style='filled,rounded',
|
125 |
-
# fillcolor=node_color,
|
126 |
-
# fontcolor=font_color,
|
127 |
-
# fontsize='12',
|
128 |
-
# width='2.5',
|
129 |
-
# height='1.2',
|
130 |
-
# pos=f"{visual_col * 4.5},{-row * 3}!"
|
131 |
-
# )
|
132 |
-
|
133 |
-
# for i in range(len(events) - 1):
|
134 |
-
# current_event_id = events[i].get('id', f'event_{i}')
|
135 |
-
# next_event_id = events[i + 1].get('id', f'event_{i + 1}')
|
136 |
-
|
137 |
-
# dot.edge(
|
138 |
-
# current_event_id,
|
139 |
-
# next_event_id,
|
140 |
-
# color='#666666',
|
141 |
-
# arrowsize='0.8',
|
142 |
-
# penwidth='2'
|
143 |
-
# )
|
144 |
-
|
145 |
-
# dot.engine = 'neato'
|
146 |
-
|
147 |
-
# with NamedTemporaryFile(delete=False, suffix=f'.{output_format}') as tmp:
|
148 |
-
# dot.render(tmp.name, format=output_format, cleanup=True)
|
149 |
-
# return f"{tmp.name}.{output_format}"
|
150 |
-
|
151 |
-
# except json.JSONDecodeError:
|
152 |
-
# return "Error: Invalid JSON format"
|
153 |
-
# except Exception as e:
|
154 |
-
# return f"Error: {str(e)}"
|
155 |
import graphviz
|
156 |
import json
|
157 |
from tempfile import NamedTemporaryFile
|
@@ -253,7 +99,7 @@ def generate_timeline_diagram(json_input: str, output_format: str) -> str:
|
|
253 |
else:
|
254 |
full_label = event_label
|
255 |
|
256 |
-
lightening_factor = 0.
|
257 |
base_r = int(base_color[1:3], 16)
|
258 |
base_g = int(base_color[3:5], 16)
|
259 |
base_b = int(base_color[5:7], 16)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import graphviz
|
2 |
import json
|
3 |
from tempfile import NamedTemporaryFile
|
|
|
99 |
else:
|
100 |
full_label = event_label
|
101 |
|
102 |
+
lightening_factor = 0.0417 # 1/24 = 0.0417 para que tarde 24 eventos en llegar a blanco
|
103 |
base_r = int(base_color[1:3], 16)
|
104 |
base_g = int(base_color[3:5], 16)
|
105 |
base_b = int(base_color[5:7], 16)
|