Enoch1359 commited on
Commit
bd0a043
Β·
verified Β·
1 Parent(s): 185fe3e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -14
app.py CHANGED
@@ -8,35 +8,43 @@ 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("ASK ME anything", 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.rerun() # force UI refresh
 
 
 
 
 
 
 
 
 
 
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
+ # Page settings
22
  st.set_page_config(page_title="Chatbot", layout="centered")
23
+ st.title("πŸ’¬ Chat with me")
 
24
 
25
+ # Chat history
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 at bottom
34
+ user_input = st.text_input("AsK me Anything", key="input")
35
 
36
+ # Handle input
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(("Bot", response.content))
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
+