Spaces:
Runtime error
Runtime error
Upload 3 files
Browse files- README.md +16 -0
- app.py +85 -0
- requirements.txt +5 -0
README.md
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
---
|
| 3 |
+
title: Causal Loop Diagram Generator
|
| 4 |
+
emoji: ♻️
|
| 5 |
+
colorFrom: blue
|
| 6 |
+
colorTo: green
|
| 7 |
+
sdk: streamlit
|
| 8 |
+
app_file: app.py
|
| 9 |
+
pinned: true
|
| 10 |
+
---
|
| 11 |
+
|
| 12 |
+
# ♻️ Causal Loop Diagram Generator (Simple Version)
|
| 13 |
+
|
| 14 |
+
Enter a system description or paragraph. The app extracts causal links and draws an interactive Causal Loop Diagram (CLD).
|
| 15 |
+
|
| 16 |
+
Powered by DeepSeek 7B Instruct (HF Inference Endpoint).
|
app.py
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
import streamlit as st
|
| 3 |
+
import requests
|
| 4 |
+
import networkx as nx
|
| 5 |
+
import matplotlib.pyplot as plt
|
| 6 |
+
|
| 7 |
+
# Replace with your actual HF Inference Endpoint URL
|
| 8 |
+
INFERENCE_API_URL = "https://your-inference-endpoint-url"
|
| 9 |
+
|
| 10 |
+
# Replace with your actual Hugging Face Access Token
|
| 11 |
+
HF_TOKEN = "Bearer YOUR-HUGGINGFACE-TOKEN"
|
| 12 |
+
|
| 13 |
+
headers = {
|
| 14 |
+
"Authorization": HF_TOKEN,
|
| 15 |
+
"Content-Type": "application/json"
|
| 16 |
+
}
|
| 17 |
+
|
| 18 |
+
st.set_page_config(page_title="Causal Loop Diagram Generator", page_icon="♻️")
|
| 19 |
+
st.title("♻️ Causal Loop Diagram (CLD) Generator (Simple Version)")
|
| 20 |
+
st.markdown("Enter a system description (even a paragraph). The app will extract causal links and draw the CLD diagram.")
|
| 21 |
+
|
| 22 |
+
user_input = st.text_area("Enter System Description:", height=200)
|
| 23 |
+
|
| 24 |
+
if st.button("Generate CLD Diagram"):
|
| 25 |
+
if user_input.strip() == "":
|
| 26 |
+
st.error("Please enter a valid system description.")
|
| 27 |
+
else:
|
| 28 |
+
try:
|
| 29 |
+
prompt_text = f"Extract causal relationships from the paragraph and format them like [Cause] → [Effect] (+/-):\n\n{user_input}"
|
| 30 |
+
response = requests.post(
|
| 31 |
+
INFERENCE_API_URL,
|
| 32 |
+
headers=headers,
|
| 33 |
+
json={"inputs": prompt_text}
|
| 34 |
+
)
|
| 35 |
+
|
| 36 |
+
if response.status_code == 200:
|
| 37 |
+
generated_text = response.json()[0]['generated_text']
|
| 38 |
+
causal_links = []
|
| 39 |
+
|
| 40 |
+
lines = generated_text.split("\n")
|
| 41 |
+
for line in lines:
|
| 42 |
+
line = line.strip()
|
| 43 |
+
if "→" in line and ("(+)" in line or "(-)" in line):
|
| 44 |
+
if line.endswith("(+)") or line.endswith("(-)"):
|
| 45 |
+
causal_links.append(line)
|
| 46 |
+
|
| 47 |
+
if causal_links:
|
| 48 |
+
st.success("✅ Extracted Causal Relationships:")
|
| 49 |
+
st.text("\n".join(causal_links))
|
| 50 |
+
|
| 51 |
+
G = nx.DiGraph()
|
| 52 |
+
|
| 53 |
+
for line in causal_links:
|
| 54 |
+
parts = line.split("→")
|
| 55 |
+
if len(parts) == 2:
|
| 56 |
+
cause = parts[0].strip()
|
| 57 |
+
rest = parts[1].strip()
|
| 58 |
+
|
| 59 |
+
if rest.endswith("(+)"):
|
| 60 |
+
effect = rest[:-3].strip()
|
| 61 |
+
polarity = "+"
|
| 62 |
+
elif rest.endswith("(-)"):
|
| 63 |
+
effect = rest[:-3].strip()
|
| 64 |
+
polarity = "-"
|
| 65 |
+
else:
|
| 66 |
+
continue
|
| 67 |
+
|
| 68 |
+
G.add_edge(cause, effect, polarity=polarity)
|
| 69 |
+
|
| 70 |
+
pos = nx.spring_layout(G, seed=42)
|
| 71 |
+
plt.figure(figsize=(10, 8))
|
| 72 |
+
nx.draw(G, pos, with_labels=True, arrows=True, node_color="skyblue", edge_color="black", node_size=2000, font_size=12, font_weight='bold')
|
| 73 |
+
|
| 74 |
+
edge_labels = {(u, v): G.edges[u, v]['polarity'] for u, v in G.edges()}
|
| 75 |
+
nx.draw_networkx_edge_labels(G, pos, edge_labels=edge_labels, font_color="red", font_size=14)
|
| 76 |
+
|
| 77 |
+
st.pyplot(plt)
|
| 78 |
+
|
| 79 |
+
else:
|
| 80 |
+
st.warning("⚠️ No causal relationships detected.")
|
| 81 |
+
else:
|
| 82 |
+
st.error(f"❌ API Error: {response.status_code} - {response.text}")
|
| 83 |
+
|
| 84 |
+
except Exception as e:
|
| 85 |
+
st.error(f"❌ Connection Error: {e}")
|
requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
streamlit
|
| 3 |
+
networkx
|
| 4 |
+
matplotlib
|
| 5 |
+
requests
|