Enoch1359 commited on
Commit
c2d0123
·
verified ·
1 Parent(s): 4b6b65b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -18
app.py CHANGED
@@ -1,34 +1,42 @@
1
  import streamlit as st
2
  from dotenv import load_dotenv
3
- from langchain_openai import ChatOpenAI
4
  import os
 
 
 
5
  load_dotenv("apiroute.env")
6
- api_key=os.getenv("OPENAI_API_KEY")
7
- api_base=os.getenv("OPENAI_API_BASE")
 
 
 
 
 
 
 
 
 
 
 
 
8
 
9
- llm=ChatOpenAI(model_name="google/gemma-3n-e2b-it:free",temperature=0.7)
10
- # Streamlit UI
11
  st.set_page_config(page_title="Chatbot", layout="centered")
12
- st.title("💬 Chat with me")
 
13
 
14
- # Chat history session state
15
  if "history" not in st.session_state:
16
  st.session_state.history = []
17
 
18
 
19
- #Then place the prompt box at the bottom
20
- user_input = st.text_input("Ask me anything:", key="input")
 
 
 
21
 
22
- # Handle input
23
  if user_input:
24
- # Add user message to history
25
  st.session_state.history.append(("You", user_input))
26
-
27
- # Get response from the model
28
  response = llm.invoke(user_input)
29
-
30
- # Add bot response to history
31
  st.session_state.history.append(("Bot", response.content))
32
-
33
- # Clear the input box after submission
34
- st.experimental_rerun()
 
1
  import streamlit as st
2
  from dotenv import load_dotenv
 
3
  import os
4
+ from langchain_openai import ChatOpenAI
5
+
6
+ # Load environment variables
7
  load_dotenv("apiroute.env")
8
+ api_key = os.getenv("OPENAI_API_KEY")
9
+ api_base = os.getenv("OPENAI_API_BASE")
10
+
11
+ # Ensure they exist
12
+ if not api_key or not api_base:
13
+ st.error("❌ Missing API credentials in apiroute.env.")
14
+ st.stop()
15
+
16
+ # Set for LangChain/OpenAI use
17
+ os.environ["OPENAI_API_KEY"] = api_key
18
+ os.environ["OPENAI_API_BASE"] = api_base
19
+
20
+ # Initialize LLM
21
+ llm = ChatOpenAI(model_name="google/gemma-3n-e2b-it:free", temperature=0.7)
22
 
23
+ # Streamlit setup
 
24
  st.set_page_config(page_title="Chatbot", layout="centered")
25
+ st.title("💬 Chat with LLM")
26
+
27
 
 
28
  if "history" not in st.session_state:
29
  st.session_state.history = []
30
 
31
 
32
+ for sender, msg in st.session_state.history:
33
+ st.markdown(f"**{sender}:** {msg}")
34
+
35
+ user_input = st.text_input("Type your message and press Enter", key="input")
36
+
37
 
 
38
  if user_input:
 
39
  st.session_state.history.append(("You", user_input))
 
 
40
  response = llm.invoke(user_input)
 
 
41
  st.session_state.history.append(("Bot", response.content))
42
+ st.experimental_rerun() # force UI refresh