LLm_file / app.py
Enoch1359's picture
Update app.py
bd0a043 verified
raw
history blame
1.36 kB
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