Transformers
Italian
English
semantic-search
explainable-ai
faiss
ai-ethics
responsible-ai
llm
prompt-engineering
multimodal-ai
ai-transparency
ethical-intelligence
explainable-llm
cognitive-ai
ethical-ai
scientific-retrieval
modular-ai
memory-augmented-llm
trustworthy-ai
reasoning-engine
ai-alignment
next-gen-llm
thinking-machines
open-source-ai
explainability
ai-research
semantic audit
cognitive agent
human-centered-ai
Create visualization/visualization.py
Browse files
src/visualization/visualization.py
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# © 2025 Elena Marziali — Code released under Apache 2.0 license.
|
| 2 |
+
# See LICENSE in the repository for details.
|
| 3 |
+
# Removal of this copyright is prohibited.
|
| 4 |
+
|
| 5 |
+
# The system can analyze text and generate interactive visualizations
|
| 6 |
+
# (e.g., bar charts, line plots, scatter plots) using Plotly.
|
| 7 |
+
|
| 8 |
+
# === Function to generate the interactive chart ===
|
| 9 |
+
def extract_numeric_values(text):
|
| 10 |
+
""" Extracts numeric ranges from the problem text. """
|
| 11 |
+
pattern = r"(\d+)\s*-\s*(\d+)|(\d+\.\d+|\d+)\s*(K|Pa|m/s)?"
|
| 12 |
+
matches = re.findall(pattern, text)
|
| 13 |
+
|
| 14 |
+
values = []
|
| 15 |
+
for match in matches:
|
| 16 |
+
if match[0] and match[1]: # Range (300 - 600)
|
| 17 |
+
values.append((int(match[0]), int(match[1])))
|
| 18 |
+
elif match[2]: # Single number with optional unit
|
| 19 |
+
values.append(float(match[2]))
|
| 20 |
+
|
| 21 |
+
return values if values else [1, 10] # Default if no numbers found
|
| 22 |
+
|
| 23 |
+
# Determines the most suitable chart type based on content
|
| 24 |
+
def determine_chart_type(text):
|
| 25 |
+
text_lower = text.lower()
|
| 26 |
+
if re.search(r"(growth|decay|population)", text_lower):
|
| 27 |
+
return "exponential_growth"
|
| 28 |
+
elif re.search(r"(oscillation|frequency|wave)", text_lower):
|
| 29 |
+
return "sinusoidal"
|
| 30 |
+
elif re.search(r"(temperature|pressure)", text_lower):
|
| 31 |
+
return "temperature_pressure"
|
| 32 |
+
elif re.search(r"(speed|time|acceleration)", text_lower):
|
| 33 |
+
return "motion"
|
| 34 |
+
elif "linear" in text_lower:
|
| 35 |
+
return "linear"
|
| 36 |
+
elif "logarithmic" in text_lower:
|
| 37 |
+
return "logarithmic"
|
| 38 |
+
elif "gaussian" in text_lower or "normal distribution" in text_lower:
|
| 39 |
+
return "gaussian"
|
| 40 |
+
else:
|
| 41 |
+
return "generic"
|
| 42 |
+
|
| 43 |
+
# Extracts numeric values from text for visualization
|
| 44 |
+
def extract_numeric_values(text):
|
| 45 |
+
numbers = [float(n) for n in re.findall(r"\d+(?:\.\d+)?", text)]
|
| 46 |
+
if len(numbers) >= 2:
|
| 47 |
+
return numbers[:2]
|
| 48 |
+
elif len(numbers) == 1:
|
| 49 |
+
return [numbers[0], numbers[0] + 10]
|
| 50 |
+
else:
|
| 51 |
+
return [1, 10]
|
| 52 |
+
|
| 53 |
+
# Generates and saves the interactive chart
|
| 54 |
+
# The chart is displayed in the notebook and also saved as a PNG image.
|
| 55 |
+
def generate_interactive_chart(problem):
|
| 56 |
+
chart_type = determine_chart_type(problem)
|
| 57 |
+
start, end = extract_numeric_values(problem)
|
| 58 |
+
x = np.linspace(start, end, 100)
|
| 59 |
+
fig = go.Figure()
|
| 60 |
+
|
| 61 |
+
if chart_type == "exponential_growth":
|
| 62 |
+
y = np.exp(x / max(x))
|
| 63 |
+
fig.add_trace(go.Scatter(x=x, y=y, mode="lines", name="Exponential Growth"))
|
| 64 |
+
elif chart_type == "sinusoidal":
|
| 65 |
+
y = np.sin(x)
|
| 66 |
+
fig.add_trace(go.Scatter(x=x, y=y, mode="lines", name="Sinusoidal Wave"))
|
| 67 |
+
elif chart_type == "motion":
|
| 68 |
+
y = x ** 2
|
| 69 |
+
fig.add_trace(go.Scatter(x=x, y=y, mode="lines", name="Speed vs Time"))
|
| 70 |
+
elif chart_type == "linear":
|
| 71 |
+
y = x
|
| 72 |
+
fig.add_trace(go.Scatter(x=x, y=y, mode="lines", name="Linear Trend"))
|
| 73 |
+
elif chart_type == "logarithmic":
|
| 74 |
+
x_log = np.where(x <= 0, 1e-3, x)
|
| 75 |
+
y = np.log(x_log)
|
| 76 |
+
fig.add_trace(go.Scatter(x=x, y=y, mode="lines", name="Logarithmic"))
|
| 77 |
+
elif chart_type == "gaussian":
|
| 78 |
+
mu, sigma = np.mean(x), np.std(x)
|
| 79 |
+
y = np.exp(-((x - mu)**2) / (2 * sigma**2))
|
| 80 |
+
fig.add_trace(go.Scatter(x=x, y=y, mode="lines", name="Gaussian"))
|
| 81 |
+
else:
|
| 82 |
+
y = np.sin(x)
|
| 83 |
+
fig.add_trace(go.Scatter(x=x, y=y, mode="lines", name="Generic"))
|
| 84 |
+
|
| 85 |
+
caption = f"Visualization of the '{chart_type}' model from {start} to {end} for the problem: \"{problem}\""
|
| 86 |
+
fig.update_layout(
|
| 87 |
+
title=caption,
|
| 88 |
+
xaxis_title="X Axis",
|
| 89 |
+
yaxis_title="Y Axis",
|
| 90 |
+
template="plotly_white"
|
| 91 |
+
)
|
| 92 |
+
fig.show()
|
| 93 |
+
|
| 94 |
+
fig.write_image("grafico_output.png", format="png", width=800, height=500)
|
| 95 |
+
print("Image saved as 'grafico_output.png'")
|
| 96 |
+
return fig, caption
|
| 97 |
+
|
| 98 |
+
# === Run example chart ===
|
| 99 |
+
example_problem = "growth"
|
| 100 |
+
fig, caption = generate_interactive_chart(example_problem)
|