Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,73 +1,77 @@
|
|
| 1 |
-
# app.py
|
| 2 |
import os, asyncio, streamlit as st
|
| 3 |
from dotenv import load_dotenv
|
| 4 |
-
from
|
| 5 |
-
from
|
| 6 |
-
from langchain_google_genai import ChatGoogleGenerativeAI
|
| 7 |
|
| 8 |
-
# βββββββββββββββββββββββββ
|
| 9 |
try:
|
| 10 |
asyncio.get_running_loop()
|
| 11 |
except RuntimeError:
|
| 12 |
asyncio.set_event_loop(asyncio.new_event_loop())
|
| 13 |
-
if os.name == "nt":
|
| 14 |
-
asyncio
|
|
|
|
| 15 |
|
| 16 |
-
# βββββββββββββββββββββββββββ
|
| 17 |
-
st.set_page_config(
|
| 18 |
-
st.title("π€
|
| 19 |
-
st.
|
| 20 |
|
| 21 |
-
# Sidebar β API key input
|
| 22 |
with st.sidebar:
|
| 23 |
-
|
| 24 |
-
st.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
-
|
| 27 |
-
|
|
|
|
|
|
|
| 28 |
|
| 29 |
-
# ββββββββββββββββββββ lazy LLM constructor ββββββββββββββββββββββββββ
|
| 30 |
def get_llm():
|
| 31 |
if "llm" not in st.session_state:
|
| 32 |
-
key =
|
| 33 |
if not key:
|
| 34 |
-
raise ValueError("
|
| 35 |
-
os.environ["
|
| 36 |
-
st.session_state.llm =
|
| 37 |
-
model=
|
| 38 |
-
|
|
|
|
|
|
|
| 39 |
)
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
-
#
|
| 43 |
-
|
| 44 |
-
[
|
| 45 |
-
("
|
| 46 |
-
("user", "Question: {question}"),
|
| 47 |
]
|
| 48 |
-
)
|
| 49 |
-
PARSER = StrOutputParser()
|
| 50 |
|
| 51 |
-
#
|
| 52 |
if user_q:
|
| 53 |
-
|
| 54 |
-
with st.spinner("Thinkingβ¦"):
|
| 55 |
-
llm = get_llm()
|
| 56 |
-
chain = PROMPT | llm | PARSER
|
| 57 |
-
answer = chain.invoke({"question": user_q})
|
| 58 |
|
| 59 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
|
| 61 |
-
st.session_state.
|
| 62 |
-
[{"role": "user", "text": user_q},
|
| 63 |
-
{"role": "bot", "text": answer}]
|
| 64 |
-
)
|
| 65 |
|
| 66 |
except Exception as err:
|
| 67 |
-
st.error(f"
|
| 68 |
|
| 69 |
-
#
|
| 70 |
-
st.
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
|
|
|
| 1 |
+
# app.py β Streamlit + LangChain + Groq
|
| 2 |
import os, asyncio, streamlit as st
|
| 3 |
from dotenv import load_dotenv
|
| 4 |
+
from langchain.schema import SystemMessage, HumanMessage, AIMessage
|
| 5 |
+
from langchain_groq import ChatGroq
|
|
|
|
| 6 |
|
| 7 |
+
# βββββββββββββββββββββββββ bootstrap eventβloop βββββββββββββββββββββ
|
| 8 |
try:
|
| 9 |
asyncio.get_running_loop()
|
| 10 |
except RuntimeError:
|
| 11 |
asyncio.set_event_loop(asyncio.new_event_loop())
|
| 12 |
+
if os.name == "nt":
|
| 13 |
+
import asyncio as _asyncio
|
| 14 |
+
asyncio.set_event_loop_policy(_asyncio.WindowsSelectorEventLoopPolicy())
|
| 15 |
|
| 16 |
+
# βββββββββββββββββββββββββββ UI / SETTINGS ββββββββββββββββββββββββ
|
| 17 |
+
st.set_page_config("Groq Chatbot", "π€")
|
| 18 |
+
st.title("π€ Groqβpowered Advanced Chatbot")
|
| 19 |
+
st.caption("DeepSeekβR1βDistillβLlamaβ70B β’ LangChain β’ Streamlit")
|
| 20 |
|
|
|
|
| 21 |
with st.sidebar:
|
| 22 |
+
st.header("π Groq API Key")
|
| 23 |
+
groq_key = st.text_input("Paste your key here", type="password")
|
| 24 |
+
st.divider()
|
| 25 |
+
temperature = st.slider("Temperature", 0.0, 1.2, 0.7, 0.1)
|
| 26 |
+
top_p = st.slider("Topβp", 0.0, 1.0, 1.0, 0.05)
|
| 27 |
+
st.markdown("*All values remain local to your browser.*")
|
| 28 |
|
| 29 |
+
user_q = st.chat_input("Type your messageβ¦")
|
| 30 |
+
|
| 31 |
+
# ββββββββββββββββββββββββββ LLM (lazy init) βββββββββββββββββββββββββ
|
| 32 |
+
MODEL_NAME = "deepseek-r1-distill-llama-70b"
|
| 33 |
|
|
|
|
| 34 |
def get_llm():
|
| 35 |
if "llm" not in st.session_state:
|
| 36 |
+
key = groq_key or os.getenv("GROQ_API_KEY")
|
| 37 |
if not key:
|
| 38 |
+
raise ValueError("Add your Groq key in the sidebar.")
|
| 39 |
+
os.environ["GROQ_API_KEY"] = key # for the client
|
| 40 |
+
st.session_state.llm = ChatGroq(
|
| 41 |
+
model = MODEL_NAME,
|
| 42 |
+
groq_api_key = key,
|
| 43 |
+
temperature = temperature,
|
| 44 |
+
top_p = top_p,
|
| 45 |
)
|
| 46 |
+
# refresh sampling params if the sliders changed
|
| 47 |
+
llm = st.session_state.llm
|
| 48 |
+
llm.temperature = temperature
|
| 49 |
+
llm.top_p = top_p
|
| 50 |
+
return llm
|
| 51 |
|
| 52 |
+
# βββββββββββββββββββββββββ conversation memory ββββββββββββββββββββββ
|
| 53 |
+
if "history" not in st.session_state:
|
| 54 |
+
st.session_state.history = [
|
| 55 |
+
SystemMessage(content="You are an advanced, helpful assistant.")
|
|
|
|
| 56 |
]
|
|
|
|
|
|
|
| 57 |
|
| 58 |
+
# ββββββββββββββββββββββββββββ main loop βββββββββββββββββββββββββββββ
|
| 59 |
if user_q:
|
| 60 |
+
st.session_state.history.append(HumanMessage(content=user_q))
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
+
try:
|
| 63 |
+
with st.chat_message("assistant", avatar="π€"):
|
| 64 |
+
with st.spinner("Thinkingβ¦"):
|
| 65 |
+
answer = get_llm().invoke(st.session_state.history).content
|
| 66 |
+
st.markdown(answer)
|
| 67 |
|
| 68 |
+
st.session_state.history.append(AIMessage(content=answer))
|
|
|
|
|
|
|
|
|
|
| 69 |
|
| 70 |
except Exception as err:
|
| 71 |
+
st.error(f"**Error:** {err}")
|
| 72 |
|
| 73 |
+
# ββββββββββββββββββββββββ display chat history ββββββββββββββββββββββ
|
| 74 |
+
for msg in st.session_state.history[1:]: # skip system message
|
| 75 |
+
role = "user" if isinstance(msg, HumanMessage) else "assistant"
|
| 76 |
+
with st.chat_message(role):
|
| 77 |
+
st.markdown(msg.content)
|