Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
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()
|