File size: 1,361 Bytes
cbd0eab 64a3019 c2d0123 cbd0eab c2d0123 bd0a043 c2d0123 bd0a043 c2d0123 bd0a043 c2d0123 7316326 bd0a043 cbd0eab bd0a043 cbd0eab bd0a043 cbd0eab bd0a043 c2d0123 bd0a043 cbd0eab bd0a043 cbd0eab bd0a043 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
import streamlit as st
from dotenv import load_dotenv
import os
from langchain_openai import ChatOpenAI
# Load environment variables
load_dotenv("apiroute.env")
api_key = os.getenv("OPENAI_API_KEY")
api_base = os.getenv("OPENAI_API_BASE")
# Sanity check
if not api_key or not api_base:
st.error("β API key or base URL missing in apiroute.env.")
st.stop()
# Init LLM
llm = ChatOpenAI(model_name="google/gemma-3n-e2b-it:free", temperature=0.7)
# Page settings
st.set_page_config(page_title="Chatbot", layout="centered")
st.title("π¬ Chat with me")
# Chat history
if "history" not in st.session_state:
st.session_state.history = []
# Display chat history
for sender, msg in st.session_state.history:
st.markdown(f"**{sender}:** {msg}")
# Input at bottom
user_input = st.text_input("AsK me Anything", key="input")
# Handle input
if user_input and st.session_state.get("input_submitted") is not True:
st.session_state.history.append(("You", user_input))
response = llm.invoke(user_input)
st.session_state.history.append(("Bot", response.content))
# β
Mark input as handled and clear box
st.session_state.input_submitted = True
st.session_state.input = "" # Clear input box
st.rerun()
# β
Reset flag after rerun
if st.session_state.get("input_submitted"):
st.session_state.input_submitted = False
|