# Streamlit is an open-source framework for building interactive web apps in Python. # Run this file with the SAME interpreter the notebook uses (has the matching # LangChain 1.x versions): # d:\2026\July\Agentic-RAG-with-LangGraph-and-Ollama\.venv\Scripts\python.exe -m streamlit run app.py # (NOT `python app.py` — that skips Streamlit's runtime and breaks session state.) import os import time import logging import streamlit as st from dotenv import load_dotenv from langchain_openai import ChatOpenAI from langchain_huggingface import HuggingFaceEndpoint, ChatHuggingFace # ---- Logging setup --------------------------------------------------------- # Logs print to the terminal where you ran `streamlit run app.py`. logging.basicConfig( level=logging.INFO, format="%(asctime)s | %(levelname)-7s | %(message)s", datefmt="%H:%M:%S", ) log = logging.getLogger("langchain-demo") # Load OPENAI_API_KEY and HUGGINGFACEHUB_API_TOKEN from the .env file log.info("STEP 0: Loading environment variables from .env") load_dotenv() log.info(" OPENAI_API_KEY set: %s", bool(os.getenv("OPENAI_API_KEY"))) log.info(" HUGGINGFACEHUB_API_TOKEN set: %s", bool(os.getenv("HUGGINGFACEHUB_API_TOKEN"))) # ---- Answer function ------------------------------------------------------- # Same code shape as LLM_Intro.ipynb, with step-by-step logging to the terminal. def load_answer(provider, question): t0 = time.perf_counter() log.info("STEP 1: Provider selected → %s", provider) log.info("STEP 2: Question → %r", question) if provider == "OpenAI": log.info("STEP 3: Building ChatOpenAI(model='gpt-4o')") llm = ChatOpenAI(model="gpt-4o") log.info("STEP 4: Calling llm.invoke(...) — waiting for OpenAI") answer = llm.invoke(question).content else: log.info("STEP 3: Building HuggingFaceEndpoint(repo_id='Qwen/Qwen2.5-72B-Instruct', " "task='conversational', provider='novita')") llm = HuggingFaceEndpoint( repo_id="Qwen/Qwen2.5-72B-Instruct", task="conversational", provider="novita", huggingfacehub_api_token=os.getenv("HUGGINGFACEHUB_API_TOKEN"), ) log.info("STEP 4: Wrapping in ChatHuggingFace and calling chat.invoke(...) — waiting for HF") chat = ChatHuggingFace(llm=llm) answer = chat.invoke(question).content elapsed = time.perf_counter() - t0 log.info("STEP 5: Got response (%d chars) in %.2fs", len(answer), elapsed) return answer # ---- App UI ---------------------------------------------------------------- st.set_page_config(page_title="LangChain Demo", page_icon=":robot:") st.header("HuggingFace Demo") # Sidebar: let the user choose the provider. with st.sidebar: st.subheader("Model settings") provider = st.selectbox("Provider", ["OpenAI", "HuggingFace"]) # Gets the user input def get_text(): input_text = st.text_input("You: ", key="input") return input_text user_input = get_text() submit = st.button("Generate") # Only call the LLM when the button is clicked and there is input if submit and user_input: with st.spinner(f"Asking {provider}…"): try: response = load_answer(provider, user_input) st.subheader("Answer:") st.write(response) except Exception as e: log.exception("ERROR during load_answer") st.error(f"{type(e).__name__}: {e}") elif submit: st.warning("Please enter a question first.")