Muhammad-Izhan commited on
Commit
b42229b
Β·
verified Β·
1 Parent(s): 19c1ffe

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +8 -9
src/streamlit_app.py CHANGED
@@ -1,25 +1,26 @@
 
1
  import streamlit as st
2
  from langchain.chat_models import ChatOpenAI
3
  from langchain.chains import ConversationChain
4
  from langchain.memory import ConversationBufferMemory
5
- import os
6
 
7
- # Set OpenRouter API Key and Base URL
8
  OPENAI_API_KEY = os.environ.get("OPENAI_API_KEY")
9
 
10
- if not api_key:
11
- st.error("API key not found. Please set OPENAI_API_KEY in Space secrets.")
 
12
  st.stop()
13
 
14
  # Set up the model
15
  llm = ChatOpenAI(
16
  temperature=0.7,
17
  model_name="deepseek/deepseek-chat-v3-0324:free",
18
- openai_api_key=OPENAI_API_KEY,
19
  openai_api_base="https://openrouter.ai/api/v1"
20
  )
21
 
22
- # Set up memory
23
  if "memory" not in st.session_state:
24
  st.session_state.memory = ConversationBufferMemory()
25
 
@@ -31,7 +32,6 @@ conversation = ConversationChain(
31
 
32
  # Streamlit UI
33
  st.set_page_config(page_title="LLM Chatbot", page_icon="πŸ€–")
34
-
35
  st.title("Langchain Chatbot by Muhammad Izhan")
36
 
37
  user_input = st.text_input("You:", key="input")
@@ -42,8 +42,7 @@ if user_input:
42
  st.session_state.memory.chat_memory.add_ai_message(response)
43
  st.write(f"**Bot:** {response}")
44
 
45
- # Show chat history
46
  if st.checkbox("Show Chat History"):
47
  for message in st.session_state.memory.chat_memory.messages:
48
  role = "You" if message.type == "human" else "Bot"
49
- st.markdown(f"**{role}:** {message.content}")
 
1
+ import os
2
  import streamlit as st
3
  from langchain.chat_models import ChatOpenAI
4
  from langchain.chains import ConversationChain
5
  from langchain.memory import ConversationBufferMemory
 
6
 
7
+ # Get API key from environment variables
8
  OPENAI_API_KEY = os.environ.get("OPENAI_API_KEY")
9
 
10
+ # Check if API key is available
11
+ if not OPENAI_API_KEY:
12
+ st.error("πŸ”‘ API key not found. Please set OPENAI_API_KEY in Space secrets.")
13
  st.stop()
14
 
15
  # Set up the model
16
  llm = ChatOpenAI(
17
  temperature=0.7,
18
  model_name="deepseek/deepseek-chat-v3-0324:free",
19
+ openai_api_key=OPENAI_API_KEY, # Use the variable here
20
  openai_api_base="https://openrouter.ai/api/v1"
21
  )
22
 
23
+ # Rest of your chatbot code...
24
  if "memory" not in st.session_state:
25
  st.session_state.memory = ConversationBufferMemory()
26
 
 
32
 
33
  # Streamlit UI
34
  st.set_page_config(page_title="LLM Chatbot", page_icon="πŸ€–")
 
35
  st.title("Langchain Chatbot by Muhammad Izhan")
36
 
37
  user_input = st.text_input("You:", key="input")
 
42
  st.session_state.memory.chat_memory.add_ai_message(response)
43
  st.write(f"**Bot:** {response}")
44
 
 
45
  if st.checkbox("Show Chat History"):
46
  for message in st.session_state.memory.chat_memory.messages:
47
  role = "You" if message.type == "human" else "Bot"
48
+ st.markdown(f"**{role}:** {message.content}")