Spaces:
Sleeping
Sleeping
field for interaction with the bot
Browse files- src/streamlit_app.py +109 -99
src/streamlit_app.py
CHANGED
|
@@ -3,112 +3,16 @@ import os, socket
|
|
| 3 |
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
|
| 4 |
hostname = socket.gethostname()
|
| 5 |
|
| 6 |
-
if "hf" in hostname or "space" in hostname:
|
| 7 |
-
os.environ["STREAMLIT_CONFIG_DIR"] = "/tmp/streamlit"
|
| 8 |
-
os.makedirs(os.environ["STREAMLIT_HOME"], exist_ok=True)
|
| 9 |
-
|
| 10 |
-
print(f"BASE_DIR: {BASE_DIR}, HOSTNAME: {hostname}")
|
| 11 |
-
|
| 12 |
import streamlit as st
|
| 13 |
import time
|
| 14 |
-
|
| 15 |
-
# Basis-Setup
|
| 16 |
-
st.set_page_config(page_title="App der anderen Art", layout="wide")
|
| 17 |
-
|
| 18 |
-
# HTML & CSS für den Splash-Screen
|
| 19 |
-
splash_html = """
|
| 20 |
-
<div id="splash">
|
| 21 |
-
<div class="center">
|
| 22 |
-
<h1 style="color:white; font-size: 40px;">Eine App der anderen Art</h1>
|
| 23 |
-
<div class="loader"></div>
|
| 24 |
-
</div>
|
| 25 |
-
</div>
|
| 26 |
-
|
| 27 |
-
<style>
|
| 28 |
-
/* Splash Hintergrund */
|
| 29 |
-
#splash {
|
| 30 |
-
position: fixed;
|
| 31 |
-
top: 0;
|
| 32 |
-
left: 0;
|
| 33 |
-
width: 100%;
|
| 34 |
-
height: 100%;
|
| 35 |
-
background-color: black;
|
| 36 |
-
display: flex;
|
| 37 |
-
justify-content: center;
|
| 38 |
-
align-items: center;
|
| 39 |
-
z-index: 9999;
|
| 40 |
-
animation: fadeout 2s ease 3s forwards;
|
| 41 |
-
}
|
| 42 |
-
|
| 43 |
-
/* Text & Animation */
|
| 44 |
-
.center {
|
| 45 |
-
text-align: center;
|
| 46 |
-
}
|
| 47 |
-
.loader {
|
| 48 |
-
border: 8px solid #f3f3f3;
|
| 49 |
-
border-top: 8px solid #444;
|
| 50 |
-
border-radius: 50%;
|
| 51 |
-
width: 50px;
|
| 52 |
-
height: 50px;
|
| 53 |
-
margin: 20px auto;
|
| 54 |
-
animation: spin 2s linear infinite;
|
| 55 |
-
}
|
| 56 |
-
|
| 57 |
-
/* Animationen */
|
| 58 |
-
@keyframes spin {
|
| 59 |
-
0% { transform: rotate(0deg); }
|
| 60 |
-
100% { transform: rotate(360deg); }
|
| 61 |
-
}
|
| 62 |
-
@keyframes fadeout {
|
| 63 |
-
to { opacity: 0; visibility: hidden; }
|
| 64 |
-
}
|
| 65 |
-
</style>
|
| 66 |
-
"""
|
| 67 |
-
|
| 68 |
-
# Splash-Screen anzeigen
|
| 69 |
-
st.markdown(splash_html, unsafe_allow_html=True)
|
| 70 |
-
|
| 71 |
-
# Simuliere Ladezeit (z. B. Daten laden)
|
| 72 |
-
time.sleep(3)
|
| 73 |
-
|
| 74 |
-
st.markdown("""
|
| 75 |
-
<style>
|
| 76 |
-
.chat-bubble-user {
|
| 77 |
-
background-color: rgba(0, 150, 136, 0.2);
|
| 78 |
-
border-radius: 20px;
|
| 79 |
-
padding: 10px 20px;
|
| 80 |
-
margin: 5px;
|
| 81 |
-
color: white;
|
| 82 |
-
max-width: 70%;
|
| 83 |
-
float: right;
|
| 84 |
-
clear: both;
|
| 85 |
-
}
|
| 86 |
-
.chat-bubble-bot {
|
| 87 |
-
background-color: rgba(255, 111, 97, 0.2);
|
| 88 |
-
border-radius: 20px;
|
| 89 |
-
padding: 10px 20px;
|
| 90 |
-
margin: 5px;
|
| 91 |
-
color: white;
|
| 92 |
-
max-width: 70%;
|
| 93 |
-
float: left;
|
| 94 |
-
clear: both;
|
| 95 |
-
}
|
| 96 |
-
</style>
|
| 97 |
-
""", unsafe_allow_html=True)
|
| 98 |
-
|
| 99 |
-
st.markdown('<div class="chat-bubble-user">Hallo, ich bin Maria 🌸</div>', unsafe_allow_html=True)
|
| 100 |
-
st.markdown('<div class="chat-bubble-bot">Hallo Maria, ich bin dein Chatbot 🤖</div>', unsafe_allow_html=True)
|
| 101 |
-
|
| 102 |
import base64
|
| 103 |
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
# Funktion, um ein Bild in Base64 zu konvertieren
|
| 107 |
def get_base64_of_bin_file(path):
|
| 108 |
with open(path, "rb") as f:
|
| 109 |
return base64.b64encode(f.read()).decode()
|
| 110 |
|
| 111 |
-
#
|
| 112 |
image_path = os.path.join(BASE_DIR, "img", "background.png")
|
| 113 |
bg_base64 = get_base64_of_bin_file(image_path)
|
| 114 |
|
|
@@ -124,4 +28,110 @@ page_bg_img = f"""
|
|
| 124 |
</style>
|
| 125 |
"""
|
| 126 |
|
| 127 |
-
st.markdown(page_bg_img, unsafe_allow_html=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
|
| 4 |
hostname = socket.gethostname()
|
| 5 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
import streamlit as st
|
| 7 |
import time
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
import base64
|
| 9 |
|
| 10 |
+
# Function for converting the image
|
|
|
|
|
|
|
| 11 |
def get_base64_of_bin_file(path):
|
| 12 |
with open(path, "rb") as f:
|
| 13 |
return base64.b64encode(f.read()).decode()
|
| 14 |
|
| 15 |
+
# Path to Image
|
| 16 |
image_path = os.path.join(BASE_DIR, "img", "background.png")
|
| 17 |
bg_base64 = get_base64_of_bin_file(image_path)
|
| 18 |
|
|
|
|
| 28 |
</style>
|
| 29 |
"""
|
| 30 |
|
| 31 |
+
st.markdown(page_bg_img, unsafe_allow_html=True)
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
st.set_page_config(page_title="A different kind of application", layout="centered")
|
| 36 |
+
|
| 37 |
+
if "splash_done" not in st.session_state:
|
| 38 |
+
st.session_state.splash_done = False
|
| 39 |
+
|
| 40 |
+
if not st.session_state.splash_done:
|
| 41 |
+
st.set_page_config(page_title="A different kind of application", layout="wide")
|
| 42 |
+
|
| 43 |
+
splash_html = """
|
| 44 |
+
<div id="splash">
|
| 45 |
+
<div class="center">
|
| 46 |
+
<h1 style="color:white; font-size: 40px;">A different kind of application</h1>
|
| 47 |
+
<div class="loader"></div>
|
| 48 |
+
</div>
|
| 49 |
+
</div>
|
| 50 |
+
|
| 51 |
+
<style>
|
| 52 |
+
/* Splash Hintergrund */
|
| 53 |
+
#splash {
|
| 54 |
+
position: fixed;
|
| 55 |
+
top: 0;
|
| 56 |
+
left: 0;
|
| 57 |
+
width: 100%;
|
| 58 |
+
height: 100%;
|
| 59 |
+
background-color: black;
|
| 60 |
+
display: flex;
|
| 61 |
+
justify-content: center;
|
| 62 |
+
align-items: center;
|
| 63 |
+
z-index: 9999;
|
| 64 |
+
animation: fadeout 2s ease 3s forwards;
|
| 65 |
+
}
|
| 66 |
+
|
| 67 |
+
/* Text & Animation */
|
| 68 |
+
.center {
|
| 69 |
+
text-align: center;
|
| 70 |
+
}
|
| 71 |
+
.loader {
|
| 72 |
+
border: 8px solid #f3f3f3;
|
| 73 |
+
border-top: 8px solid #444;
|
| 74 |
+
border-radius: 50%;
|
| 75 |
+
width: 50px;
|
| 76 |
+
height: 50px;
|
| 77 |
+
margin: 20px auto;
|
| 78 |
+
animation: spin 2s linear infinite;
|
| 79 |
+
}
|
| 80 |
+
|
| 81 |
+
/* Animationen */
|
| 82 |
+
@keyframes spin {
|
| 83 |
+
0% { transform: rotate(0deg); }
|
| 84 |
+
100% { transform: rotate(360deg); }
|
| 85 |
+
}
|
| 86 |
+
@keyframes fadeout {
|
| 87 |
+
to { opacity: 0; visibility: hidden; }
|
| 88 |
+
}
|
| 89 |
+
</style>
|
| 90 |
+
"""
|
| 91 |
+
|
| 92 |
+
st.markdown(splash_html, unsafe_allow_html=True)
|
| 93 |
+
time.sleep(2)
|
| 94 |
+
st.session_state.splash_done = True
|
| 95 |
+
st.rerun() # -> Seite neu laden, danach kein Splash mehr
|
| 96 |
+
else:
|
| 97 |
+
|
| 98 |
+
st.set_page_config(layout="centered")
|
| 99 |
+
|
| 100 |
+
if "messages" not in st.session_state:
|
| 101 |
+
st.session_state.messages = []
|
| 102 |
+
|
| 103 |
+
user_input = st.chat_input("Write someting...")
|
| 104 |
+
|
| 105 |
+
if user_input:
|
| 106 |
+
st.session_state.messages.append(("user", user_input))
|
| 107 |
+
# Tipp-Animation
|
| 108 |
+
st.session_state.messages.append(("bot", "typing..."))
|
| 109 |
+
time.sleep(1.5) # simuliert "nachdenken"
|
| 110 |
+
st.session_state.messages[-1] = ("bot", f"Antwort auf: {user_input}")
|
| 111 |
+
|
| 112 |
+
# CSS für Chat
|
| 113 |
+
st.markdown("""
|
| 114 |
+
<style>
|
| 115 |
+
.chat-bubble {
|
| 116 |
+
padding: 10px 15px;
|
| 117 |
+
border-radius: 20px;
|
| 118 |
+
margin: 5px 0;
|
| 119 |
+
max-width: 70%;
|
| 120 |
+
animation: slidein 0.5s ease;
|
| 121 |
+
word-wrap: break-word;
|
| 122 |
+
overflow-wrap: break-word;
|
| 123 |
+
white-space: pre-wrap;
|
| 124 |
+
}
|
| 125 |
+
.user { background: rgba(68, 68, 68); color: white; margin-left: auto; }
|
| 126 |
+
.bot { background: rgba(221, 221, 221); color: black; margin-right: auto; }
|
| 127 |
+
|
| 128 |
+
@keyframes slidein {
|
| 129 |
+
from { transform: translateY(20px); opacity: 0; }
|
| 130 |
+
to { transform: translateY(0); opacity: 1; }
|
| 131 |
+
}
|
| 132 |
+
</style>
|
| 133 |
+
""", unsafe_allow_html=True)
|
| 134 |
+
|
| 135 |
+
# Render Chat
|
| 136 |
+
for role, msg in st.session_state.messages:
|
| 137 |
+
st.markdown(f'<div class="chat-bubble {role}">{msg}</div>', unsafe_allow_html=True)
|