Spaces:
Runtime error
Runtime error
| import streamlit as st | |
| import requests | |
| import networkx as nx | |
| import matplotlib.pyplot as plt | |
| # Replace with your actual HF Inference Endpoint URL | |
| INFERENCE_API_URL = "https://your-inference-endpoint-url" | |
| # Replace with your actual Hugging Face Access Token | |
| HF_TOKEN = "Bearer YOUR-HUGGINGFACE-TOKEN" | |
| headers = { | |
| "Authorization": HF_TOKEN, | |
| "Content-Type": "application/json" | |
| } | |
| st.set_page_config(page_title="Causal Loop Diagram Generator", page_icon="♻️") | |
| st.title("♻️ Causal Loop Diagram (CLD) Generator (Simple Version)") | |
| st.markdown("Enter a system description (even a paragraph). The app will extract causal links and draw the CLD diagram.") | |
| user_input = st.text_area("Enter System Description:", height=200) | |
| if st.button("Generate CLD Diagram"): | |
| if user_input.strip() == "": | |
| st.error("Please enter a valid system description.") | |
| else: | |
| try: | |
| prompt_text = f"Extract causal relationships from the paragraph and format them like [Cause] → [Effect] (+/-):\n\n{user_input}" | |
| response = requests.post( | |
| INFERENCE_API_URL, | |
| headers=headers, | |
| json={"inputs": prompt_text} | |
| ) | |
| if response.status_code == 200: | |
| generated_text = response.json()[0]['generated_text'] | |
| causal_links = [] | |
| lines = generated_text.split("\n") | |
| for line in lines: | |
| line = line.strip() | |
| if "→" in line and ("(+)" in line or "(-)" in line): | |
| if line.endswith("(+)") or line.endswith("(-)"): | |
| causal_links.append(line) | |
| if causal_links: | |
| st.success("✅ Extracted Causal Relationships:") | |
| st.text("\n".join(causal_links)) | |
| G = nx.DiGraph() | |
| for line in causal_links: | |
| parts = line.split("→") | |
| if len(parts) == 2: | |
| cause = parts[0].strip() | |
| rest = parts[1].strip() | |
| if rest.endswith("(+)"): | |
| effect = rest[:-3].strip() | |
| polarity = "+" | |
| elif rest.endswith("(-)"): | |
| effect = rest[:-3].strip() | |
| polarity = "-" | |
| else: | |
| continue | |
| G.add_edge(cause, effect, polarity=polarity) | |
| pos = nx.spring_layout(G, seed=42) | |
| plt.figure(figsize=(10, 8)) | |
| nx.draw(G, pos, with_labels=True, arrows=True, node_color="skyblue", edge_color="black", node_size=2000, font_size=12, font_weight='bold') | |
| edge_labels = {(u, v): G.edges[u, v]['polarity'] for u, v in G.edges()} | |
| nx.draw_networkx_edge_labels(G, pos, edge_labels=edge_labels, font_color="red", font_size=14) | |
| st.pyplot(plt) | |
| else: | |
| st.warning("⚠️ No causal relationships detected.") | |
| else: | |
| st.error(f"❌ API Error: {response.status_code} - {response.text}") | |
| except Exception as e: | |
| st.error(f"❌ Connection Error: {e}") | |