Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,97 +1,77 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
from
|
| 4 |
-
from langchain.
|
| 5 |
from langchain_groq import ChatGroq
|
| 6 |
-
from langchain.schema import Document
|
| 7 |
-
from langchain_community.document_loaders import UnstructuredURLLoader
|
| 8 |
-
from langchain.document_loaders import PyPDFLoader
|
| 9 |
|
| 10 |
-
# βββββββββββββββββββββββββ
|
| 11 |
-
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
-
#
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
if groq_api_key:
|
| 19 |
-
os.environ["GROQ_API_KEY"] = groq_api_key # for libraries
|
| 20 |
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
-
|
| 26 |
-
def get_video_id(url: str) -> str | None:
|
| 27 |
-
m = re.search(r"(?:v=|\/)([0-9A-Za-z_-]{11})", url)
|
| 28 |
-
return m.group(1) if m else None
|
| 29 |
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
input_variables=["text"],
|
| 33 |
-
)
|
| 34 |
|
| 35 |
-
def
|
| 36 |
-
"""Instantiate ChatGroq once and cache it in session_state."""
|
| 37 |
if "llm" not in st.session_state:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
st.session_state.llm = ChatGroq(
|
| 39 |
-
model=
|
| 40 |
-
groq_api_key=
|
|
|
|
|
|
|
| 41 |
)
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
|
|
|
| 48 |
|
| 49 |
-
#
|
| 50 |
-
if
|
| 51 |
-
|
| 52 |
-
st.error("Please enter your Groq API key in the sidebar.")
|
| 53 |
-
elif not generic_url and not uploaded_file:
|
| 54 |
-
st.error("Provide a URL or upload a PDF, then press Summarize.")
|
| 55 |
-
else:
|
| 56 |
-
try:
|
| 57 |
-
with st.spinner("Fetching and summarizingβ¦"):
|
| 58 |
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
docs = PyPDFLoader(tmp_path).load()
|
| 65 |
-
st.success(summarize(docs))
|
| 66 |
-
os.remove(tmp_path)
|
| 67 |
|
| 68 |
-
|
| 69 |
-
elif "youtube" in generic_url or "youtu.be" in generic_url:
|
| 70 |
-
vid = get_video_id(generic_url)
|
| 71 |
-
if not vid:
|
| 72 |
-
st.error("Couldnβt extract a YouTube video ID π€")
|
| 73 |
-
else:
|
| 74 |
-
transcript = YouTubeTranscriptApi.get_transcript(vid)
|
| 75 |
-
text = " ".join(t["text"] for t in transcript)
|
| 76 |
-
st.success(summarize([Document(page_content=text)]))
|
| 77 |
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
if not validators.url(generic_url):
|
| 81 |
-
st.error("That doesnβt look like a valid URL.")
|
| 82 |
-
else:
|
| 83 |
-
docs = UnstructuredURLLoader(
|
| 84 |
-
urls=[generic_url],
|
| 85 |
-
ssl_verify=False,
|
| 86 |
-
headers={
|
| 87 |
-
"User-Agent":
|
| 88 |
-
"Mozilla/5.0 (X11; Linux) AppleWebKit/537.36 "
|
| 89 |
-
"(KHTML, like Gecko) Chrome/121.0 Safari/537.36"
|
| 90 |
-
},
|
| 91 |
-
).load()
|
| 92 |
-
st.success(summarize(docs))
|
| 93 |
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
|
|
|
|
|
| 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)
|