Spaces:
Runtime error
Runtime error
ehristoforu
commited on
import streamlit as st import time import random import os def text(): st.title("Home of ForgeStudio") st.markdown(""" ## About This is modern webui for new AI types. ## How to use In the navigate menu there is options of AI types. ### Build by [@ehristoforu](https://github.com/ehristoforu) """)
Browse files
page1.py
CHANGED
@@ -1,16 +1,196 @@
|
|
1 |
import streamlit as st
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
import time
|
3 |
import random
|
4 |
import os
|
|
|
5 |
|
6 |
def text():
|
7 |
-
st.title("
|
8 |
st.markdown("""
|
9 |
-
|
10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
|
12 |
-
|
13 |
-
|
|
|
|
|
14 |
|
15 |
-
|
16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
+
from langchain_core.messages import HumanMessage
|
3 |
+
from langchain_google_genai import ChatGoogleGenerativeAI
|
4 |
+
from langchain.chains import LLMChain
|
5 |
+
from langchain.prompts import PromptTemplate
|
6 |
+
from langchain.memory import ConversationBufferMemory
|
7 |
+
from langchain.memory.chat_message_histories import StreamlitChatMessageHistory
|
8 |
+
from streamlit_chat import message
|
9 |
import time
|
10 |
import random
|
11 |
import os
|
12 |
+
api = os.environ.get("api_key")
|
13 |
|
14 |
def text():
|
15 |
+
st.title("Vanilla Chat")
|
16 |
st.markdown("""
|
17 |
+
<style>
|
18 |
+
.anim-typewriter {
|
19 |
+
animation: typewriter 3s steps(40) 1s 1 normal both, blinkTextCursor 800ms steps(40) infinite normal;
|
20 |
+
overflow: hidden;
|
21 |
+
white-space: nowrap;
|
22 |
+
border-right: 3px solid;
|
23 |
+
font-family: serif;
|
24 |
+
font-size: 0.9em;
|
25 |
+
}
|
26 |
+
@keyframes typewriter {
|
27 |
+
from {
|
28 |
+
width: 0;
|
29 |
+
}
|
30 |
+
to {
|
31 |
+
width: 100%;
|
32 |
+
height: 100%
|
33 |
+
}
|
34 |
+
}
|
35 |
+
@keyframes blinkTextCursor {
|
36 |
+
from {
|
37 |
+
border-right-color: rgba(255, 255, 255, 0.75);
|
38 |
+
}
|
39 |
+
to {
|
40 |
+
border-right-color: transparent;
|
41 |
+
}
|
42 |
+
}
|
43 |
+
</style>
|
44 |
+
""", unsafe_allow_html=True)
|
45 |
+
text ="Hello 👋, how may I assist you today?"
|
46 |
+
animated_output = f'<div class="line-1 anim-typewriter">{text}</div>'
|
47 |
|
48 |
+
with st.chat_message("assistant").markdown(animated_output,unsafe_allow_html=True ):
|
49 |
+
st.markdown(animated_output,unsafe_allow_html=True)
|
50 |
+
apiKey = api
|
51 |
+
msgs = StreamlitChatMessageHistory(key="special_app_key")
|
52 |
|
53 |
+
memory = ConversationBufferMemory(memory_key="history", chat_memory=msgs)
|
54 |
+
if len(msgs.messages) == 0:
|
55 |
+
msgs.add_ai_message("How can I help you?")
|
56 |
+
template = """You are an AI chatbot having a conversation with a human.
|
57 |
+
|
58 |
+
{history}
|
59 |
+
Human: {human_input}
|
60 |
+
AI: """
|
61 |
+
prompt = PromptTemplate(input_variables=["history", "human_input"], template=template)
|
62 |
+
llm_chain = LLMChain( llm = ChatGoogleGenerativeAI(model="gemini-pro", google_api_key=apiKey), prompt=prompt, memory = memory)
|
63 |
+
|
64 |
+
if 'messages' not in st.session_state:
|
65 |
+
st.session_state['messages'] = []
|
66 |
+
|
67 |
+
for message in st.session_state.messages:
|
68 |
+
with st.chat_message(message["role"]):
|
69 |
+
st.markdown(message["content"])
|
70 |
+
|
71 |
+
prompt = st.chat_input("Say something")
|
72 |
+
|
73 |
+
if prompt:
|
74 |
+
with st.chat_message("user").markdown(prompt):
|
75 |
+
st.session_state.messages.append(
|
76 |
+
{
|
77 |
+
"role": "user",
|
78 |
+
"content": prompt
|
79 |
+
}
|
80 |
+
)
|
81 |
+
# Custom HTML and CSS for three-dot animation
|
82 |
+
spinner_html = """
|
83 |
+
<div class="col-3">
|
84 |
+
<div class="snippet" data-title="dot-pulse">
|
85 |
+
<div class="stage">
|
86 |
+
<div class="dot-pulse"></div>
|
87 |
+
</div>
|
88 |
+
</div>
|
89 |
+
</div>
|
90 |
+
"""
|
91 |
+
|
92 |
+
spinner_css = """
|
93 |
+
.dot-pulse {
|
94 |
+
position: relative;
|
95 |
+
left: -9999px;
|
96 |
+
|
97 |
+
width: 10px;
|
98 |
+
height: 10px;
|
99 |
+
border-radius: 5px;
|
100 |
+
background-color: #9880ff;
|
101 |
+
color: #9880ff;
|
102 |
+
box-shadow: 9999px 0 0 -5px;
|
103 |
+
animation: dot-pulse 1.5s infinite linear;
|
104 |
+
animation-delay: 0.25s;
|
105 |
+
}
|
106 |
+
.dot-pulse::before, .dot-pulse::after {
|
107 |
+
content: "";
|
108 |
+
display: inline-block;
|
109 |
+
position: absolute;
|
110 |
+
top: 0;
|
111 |
+
width: 10px;
|
112 |
+
height: 10px;
|
113 |
+
border-radius: 5px;
|
114 |
+
background-color: #9880ff;
|
115 |
+
color: #9880ff;
|
116 |
+
}
|
117 |
+
.dot-pulse::before {
|
118 |
+
box-shadow: 9984px 0 0 -5px;
|
119 |
+
animation: dot-pulse-before 1.5s infinite linear;
|
120 |
+
animation-delay: 0s;
|
121 |
+
}
|
122 |
+
.dot-pulse::after {
|
123 |
+
box-shadow: 10014px 0 0 -5px;
|
124 |
+
animation: dot-pulse-after 1.5s infinite linear;
|
125 |
+
animation-delay: 0.5s;
|
126 |
+
}
|
127 |
+
|
128 |
+
@keyframes dot-pulse-before {
|
129 |
+
0% {
|
130 |
+
box-shadow: 9984px 0 0 -5px;
|
131 |
+
}
|
132 |
+
30% {
|
133 |
+
box-shadow: 9984px 0 0 2px;
|
134 |
+
}
|
135 |
+
60%, 100% {
|
136 |
+
box-shadow: 9984px 0 0 -5px;
|
137 |
+
}
|
138 |
+
}
|
139 |
+
@keyframes dot-pulse {
|
140 |
+
0% {
|
141 |
+
box-shadow: 9999px 0 0 -5px;
|
142 |
+
}
|
143 |
+
30% {
|
144 |
+
box-shadow: 9999px 0 0 2px;
|
145 |
+
}
|
146 |
+
60%, 100% {
|
147 |
+
box-shadow: 9999px 0 0 -5px;
|
148 |
+
}
|
149 |
+
}
|
150 |
+
@keyframes dot-pulse-after {
|
151 |
+
0% {
|
152 |
+
box-shadow: 10014px 0 0 -5px;
|
153 |
+
}
|
154 |
+
30% {
|
155 |
+
box-shadow: 10014px 0 0 2px;
|
156 |
+
}
|
157 |
+
60%, 100% {
|
158 |
+
box-shadow: 10014px 0 0 -5px;
|
159 |
+
}
|
160 |
+
}
|
161 |
+
"""
|
162 |
+
|
163 |
+
st.markdown(f'<style>{spinner_css}</style>', unsafe_allow_html=True)
|
164 |
+
st.markdown(spinner_html, unsafe_allow_html=True)
|
165 |
+
|
166 |
+
for chunk in llm_chain.stream(prompt):
|
167 |
+
text_output = chunk.get("text", "")
|
168 |
+
st.markdown('<style>.dot-pulse { visibility: hidden; }</style>', unsafe_allow_html=True)
|
169 |
+
|
170 |
+
with st.chat_message("assistant").markdown(text_output):
|
171 |
+
st.session_state.messages.append(
|
172 |
+
{
|
173 |
+
"role": "assistant",
|
174 |
+
"content": text_output
|
175 |
+
}
|
176 |
+
)
|
177 |
+
|
178 |
+
#with st.chat_message("assistant"):
|
179 |
+
#message_placeholder = st.empty()
|
180 |
+
#full_response = ""
|
181 |
+
#assistant_response = random.choice(
|
182 |
+
#[
|
183 |
+
#"Hello there! How can I assist you today?",
|
184 |
+
#"Hi, human! Is there anything I can help you with?",
|
185 |
+
# "Do you need help?",
|
186 |
+
# ]
|
187 |
+
# )
|
188 |
+
# Simulate stream of response with milliseconds delay
|
189 |
+
# for chunk in text_output.split():
|
190 |
+
# full_response += chunk + " "
|
191 |
+
# time.sleep(0.05)
|
192 |
+
# Add a blinking cursor to simulate typing
|
193 |
+
# message_placeholder.markdown(full_response + "▌")
|
194 |
+
# message_placeholder.markdown(full_response)
|
195 |
+
# Add assistant response to chat history
|
196 |
+
# st.session_state.messages.append({"role": "assistant", "content": full_response})
|