Update app.py
Browse files
app.py
CHANGED
@@ -3,48 +3,36 @@ from dotenv import load_dotenv
|
|
3 |
import os
|
4 |
from langchain_openai import ChatOpenAI
|
5 |
|
6 |
-
# Load
|
7 |
load_dotenv("apiroute.env")
|
8 |
api_key = os.getenv("OPENAI_API_KEY")
|
9 |
api_base = os.getenv("OPENAI_API_BASE")
|
10 |
|
11 |
-
# Sanity check
|
12 |
-
if not api_key or not api_base:
|
13 |
-
st.error("β API key or base URL missing in apiroute.env.")
|
14 |
-
st.stop()
|
15 |
-
|
16 |
|
17 |
|
18 |
# Init LLM
|
19 |
llm = ChatOpenAI(model_name="google/gemma-3n-e2b-it:free", temperature=0.7)
|
20 |
|
21 |
-
#
|
22 |
st.set_page_config(page_title="Chatbot", layout="centered")
|
23 |
-
st.title("π¬ Chat with
|
24 |
|
25 |
-
#
|
26 |
if "history" not in st.session_state:
|
27 |
st.session_state.history = []
|
28 |
|
29 |
-
# Display chat history
|
30 |
for sender, msg in st.session_state.history:
|
31 |
st.markdown(f"**{sender}:** {msg}")
|
32 |
|
33 |
-
# Input
|
34 |
-
|
|
|
|
|
35 |
|
36 |
-
|
37 |
-
if user_input and st.session_state.get("input_submitted") is not True:
|
38 |
st.session_state.history.append(("You", user_input))
|
39 |
response = llm.invoke(user_input)
|
40 |
-
st.session_state.history.append(("
|
41 |
-
|
42 |
-
# β
Mark input as handled and clear box
|
43 |
-
st.session_state.input_submitted = True
|
44 |
-
st.session_state.input = "" # Clear input box
|
45 |
st.rerun()
|
46 |
|
47 |
-
# β
Reset flag after rerun
|
48 |
-
if st.session_state.get("input_submitted"):
|
49 |
-
st.session_state.input_submitted = False
|
50 |
-
|
|
|
3 |
import os
|
4 |
from langchain_openai import ChatOpenAI
|
5 |
|
6 |
+
# Load API keys
|
7 |
load_dotenv("apiroute.env")
|
8 |
api_key = os.getenv("OPENAI_API_KEY")
|
9 |
api_base = os.getenv("OPENAI_API_BASE")
|
10 |
|
|
|
|
|
|
|
|
|
|
|
11 |
|
12 |
|
13 |
# Init LLM
|
14 |
llm = ChatOpenAI(model_name="google/gemma-3n-e2b-it:free", temperature=0.7)
|
15 |
|
16 |
+
# Streamlit UI
|
17 |
st.set_page_config(page_title="Chatbot", layout="centered")
|
18 |
+
st.title("π¬ Chat with Gremmy")
|
19 |
|
20 |
+
# Session for chat history
|
21 |
if "history" not in st.session_state:
|
22 |
st.session_state.history = []
|
23 |
|
24 |
+
# Display chat history at top
|
25 |
for sender, msg in st.session_state.history:
|
26 |
st.markdown(f"**{sender}:** {msg}")
|
27 |
|
28 |
+
# Input with "Send" button
|
29 |
+
with st.form(key="chat_form", clear_on_submit=True):
|
30 |
+
user_input = st.text_input("talk to me", key="user_message")
|
31 |
+
submitted = st.form_submit_button("Send")
|
32 |
|
33 |
+
if submitted and user_input:
|
|
|
34 |
st.session_state.history.append(("You", user_input))
|
35 |
response = llm.invoke(user_input)
|
36 |
+
st.session_state.history.append(("Gremmy", response.content))
|
|
|
|
|
|
|
|
|
37 |
st.rerun()
|
38 |
|
|
|
|
|
|
|
|