Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,27 +1,68 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
|
| 3 |
-
# install streamlit-mermaid: pip install streamlit-mermaid
|
| 4 |
-
# and then uncomment the following import:
|
| 5 |
-
# import streamlit_mermaid as st_mermaid
|
| 6 |
|
| 7 |
-
#
|
| 8 |
-
#
|
| 9 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
DEFAULT_MERMAID = """
|
| 11 |
flowchart LR
|
| 12 |
-
%%
|
| 13 |
U((User ๐)) -- "Talk ๐ฃ๏ธ" --> LLM[LLM Agent ๐ค\\nExtract Info]
|
| 14 |
LLM -- "Query ๐" --> HS[Hybrid Search ๐\\nVector+NER+Lexical]
|
| 15 |
HS -- "Reason ๐ค" --> RE[Reasoning Engine ๐ ๏ธ\\nNeuralNetwork+Medical]
|
| 16 |
RE -- "Link ๐ก" --> KG((Knowledge Graph ๐\\nOntology+GAR+RAG))
|
| 17 |
"""
|
| 18 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
def main():
|
| 20 |
-
st.
|
| 21 |
|
| 22 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
left_col, right_col = st.columns(2)
|
| 24 |
-
|
| 25 |
# --- Left Column: Markdown Editor ---
|
| 26 |
with left_col:
|
| 27 |
st.subheader("Markdown Side ๐")
|
|
@@ -31,17 +72,18 @@ def main():
|
|
| 31 |
height=400
|
| 32 |
)
|
| 33 |
|
| 34 |
-
#
|
| 35 |
colA, colB = st.columns([1,1])
|
| 36 |
with colA:
|
| 37 |
if st.button("๐ Refresh Markdown"):
|
| 38 |
-
st.write("**Markdown** content refreshed!")
|
| 39 |
with colB:
|
| 40 |
if st.button("โ Clear Markdown"):
|
| 41 |
-
|
|
|
|
| 42 |
st.experimental_rerun()
|
| 43 |
-
|
| 44 |
-
#
|
| 45 |
st.markdown("---")
|
| 46 |
st.markdown("**Preview:**")
|
| 47 |
st.markdown(markdown_text)
|
|
@@ -49,31 +91,29 @@ def main():
|
|
| 49 |
# --- Right Column: Mermaid Editor ---
|
| 50 |
with right_col:
|
| 51 |
st.subheader("Mermaid Side ๐งโโ๏ธ")
|
| 52 |
-
|
| 53 |
"Edit Mermaid Code:",
|
| 54 |
-
value=
|
| 55 |
height=400
|
| 56 |
)
|
| 57 |
|
| 58 |
-
#
|
| 59 |
colC, colD = st.columns([1,1])
|
| 60 |
with colC:
|
| 61 |
if st.button("๐จ Refresh Diagram"):
|
| 62 |
-
st.
|
|
|
|
|
|
|
| 63 |
with colD:
|
| 64 |
if st.button("โ Clear Mermaid"):
|
| 65 |
-
|
| 66 |
st.experimental_rerun()
|
| 67 |
-
|
| 68 |
st.markdown("---")
|
| 69 |
st.markdown("**Mermaid Source:**")
|
| 70 |
-
st.code(
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
# try:
|
| 74 |
-
# st_mermaid.mermaid(mermaid_code)
|
| 75 |
-
# except Exception as e:
|
| 76 |
-
# st.error("Could not render Mermaid diagram. " + str(e))
|
| 77 |
|
| 78 |
if __name__ == "__main__":
|
| 79 |
main()
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
import streamlit.components.v1 as components
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
+
# ๐ก If you want to render Mermaid diagrams in pure Streamlit, you can use
|
| 5 |
+
# streamlit-mermaid or other solutions, but here's the direct HTML approach.
|
| 6 |
+
# ๐ฟ pip install streamlit-mermaid (optional) if you go that route.
|
| 7 |
+
|
| 8 |
+
# -------------------------------------------
|
| 9 |
+
# ๐ Default Mermaid code with emojis ๐
|
| 10 |
+
# -------------------------------------------
|
| 11 |
DEFAULT_MERMAID = """
|
| 12 |
flowchart LR
|
| 13 |
+
%% The user is unstoppable
|
| 14 |
U((User ๐)) -- "Talk ๐ฃ๏ธ" --> LLM[LLM Agent ๐ค\\nExtract Info]
|
| 15 |
LLM -- "Query ๐" --> HS[Hybrid Search ๐\\nVector+NER+Lexical]
|
| 16 |
HS -- "Reason ๐ค" --> RE[Reasoning Engine ๐ ๏ธ\\nNeuralNetwork+Medical]
|
| 17 |
RE -- "Link ๐ก" --> KG((Knowledge Graph ๐\\nOntology+GAR+RAG))
|
| 18 |
"""
|
| 19 |
|
| 20 |
+
def generate_mermaid_html(mermaid_code: str) -> str:
|
| 21 |
+
"""
|
| 22 |
+
๐ฟ Returns minimal HTML embedding a Mermaid diagram.
|
| 23 |
+
The code is centered using a simple inline style ๐บ
|
| 24 |
+
"""
|
| 25 |
+
return f"""
|
| 26 |
+
<html>
|
| 27 |
+
<head>
|
| 28 |
+
<script src="https://cdn.jsdelivr.net/npm/mermaid/dist/mermaid.min.js"></script>
|
| 29 |
+
<style>
|
| 30 |
+
/* Quick center alignment: your diagram in spotlight ๐คฉ */
|
| 31 |
+
.centered-mermaid {{
|
| 32 |
+
display: flex;
|
| 33 |
+
justify-content: center;
|
| 34 |
+
margin: 20px auto;
|
| 35 |
+
}}
|
| 36 |
+
</style>
|
| 37 |
+
</head>
|
| 38 |
+
<body>
|
| 39 |
+
<div class="mermaid centered-mermaid">
|
| 40 |
+
{mermaid_code}
|
| 41 |
+
</div>
|
| 42 |
+
<script>
|
| 43 |
+
mermaid.initialize({{ startOnLoad: true }});
|
| 44 |
+
</script>
|
| 45 |
+
</body>
|
| 46 |
+
</html>
|
| 47 |
+
"""
|
| 48 |
+
|
| 49 |
def main():
|
| 50 |
+
st.set_page_config(page_title="Inline Mermaid Demo", layout="wide")
|
| 51 |
|
| 52 |
+
# ๐ฅ Title for our spiffy app
|
| 53 |
+
st.title("Top-Centered Mermaid Diagram ๐บ")
|
| 54 |
+
|
| 55 |
+
# ๐งช Start with default code or user-modified
|
| 56 |
+
if "current_mermaid" not in st.session_state:
|
| 57 |
+
st.session_state["current_mermaid"] = DEFAULT_MERMAID
|
| 58 |
+
|
| 59 |
+
# ๐ Show the diagram *first*, in the center, via HTML embed
|
| 60 |
+
diagram_html = generate_mermaid_html(st.session_state["current_mermaid"])
|
| 61 |
+
components.html(diagram_html, height=400, scrolling=True)
|
| 62 |
+
|
| 63 |
+
# ๐ Now, place columns for Markdown & Mermaid editing
|
| 64 |
left_col, right_col = st.columns(2)
|
| 65 |
+
|
| 66 |
# --- Left Column: Markdown Editor ---
|
| 67 |
with left_col:
|
| 68 |
st.subheader("Markdown Side ๐")
|
|
|
|
| 72 |
height=400
|
| 73 |
)
|
| 74 |
|
| 75 |
+
# ๐ Button bar: short, sweet, emoji-laden
|
| 76 |
colA, colB = st.columns([1,1])
|
| 77 |
with colA:
|
| 78 |
if st.button("๐ Refresh Markdown"):
|
| 79 |
+
st.write("**Markdown** content refreshed! ๐ฟ")
|
| 80 |
with colB:
|
| 81 |
if st.button("โ Clear Markdown"):
|
| 82 |
+
# ๐ซง Bye-bye text
|
| 83 |
+
st.session_state["last_markdown"] = ""
|
| 84 |
st.experimental_rerun()
|
| 85 |
+
|
| 86 |
+
# Preview the userโs Markdown below
|
| 87 |
st.markdown("---")
|
| 88 |
st.markdown("**Preview:**")
|
| 89 |
st.markdown(markdown_text)
|
|
|
|
| 91 |
# --- Right Column: Mermaid Editor ---
|
| 92 |
with right_col:
|
| 93 |
st.subheader("Mermaid Side ๐งโโ๏ธ")
|
| 94 |
+
mermaid_input = st.text_area(
|
| 95 |
"Edit Mermaid Code:",
|
| 96 |
+
value=st.session_state["current_mermaid"],
|
| 97 |
height=400
|
| 98 |
)
|
| 99 |
|
| 100 |
+
# ๐น๏ธ Another lil button bar
|
| 101 |
colC, colD = st.columns([1,1])
|
| 102 |
with colC:
|
| 103 |
if st.button("๐จ Refresh Diagram"):
|
| 104 |
+
st.session_state["current_mermaid"] = mermaid_input
|
| 105 |
+
st.write("**Mermaid** diagram refreshed! ๐")
|
| 106 |
+
st.experimental_rerun()
|
| 107 |
with colD:
|
| 108 |
if st.button("โ Clear Mermaid"):
|
| 109 |
+
st.session_state["current_mermaid"] = ""
|
| 110 |
st.experimental_rerun()
|
| 111 |
+
|
| 112 |
st.markdown("---")
|
| 113 |
st.markdown("**Mermaid Source:**")
|
| 114 |
+
st.code(mermaid_input, language="python", line_numbers=True)
|
| 115 |
+
|
| 116 |
+
# ๐ฆ The show is over. Crabs? Possibly. ๐ฆ
|
|
|
|
|
|
|
|
|
|
|
|
|
| 117 |
|
| 118 |
if __name__ == "__main__":
|
| 119 |
main()
|