Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,56 +1,56 @@
|
|
| 1 |
-
import os
|
| 2 |
-
|
| 3 |
from langchain_core.prompts import ChatPromptTemplate
|
| 4 |
from langchain_core.output_parsers import StrOutputParser
|
| 5 |
from langchain_google_genai import ChatGoogleGenerativeAI
|
| 6 |
-
from langchain_core.messages import HumanMessage, SystemMessage
|
| 7 |
-
from dotenv import load_dotenv
|
| 8 |
-
|
| 9 |
-
load_dotenv()
|
| 10 |
-
|
| 11 |
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
|
|
|
|
|
|
| 14 |
|
| 15 |
-
system_template = "You are a helpful assistant. Please
|
| 16 |
prompt_template = ChatPromptTemplate.from_messages([
|
| 17 |
('system', system_template),
|
| 18 |
-
('user', 'Question:{question}')
|
| 19 |
])
|
| 20 |
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
parser
|
| 24 |
-
chain
|
| 25 |
|
| 26 |
st.title("LangChain Chatbot Demo")
|
| 27 |
st.markdown("""
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
st.sidebar.header("Chatbot Settings")
|
| 32 |
-
st.sidebar.text("You can enter your query in the main section below.")
|
| 33 |
-
|
| 34 |
-
input_text = st.text_input("Enter your question:", "")
|
| 35 |
|
|
|
|
| 36 |
|
| 37 |
if input_text:
|
| 38 |
-
|
| 39 |
-
|
| 40 |
response = chain.invoke({"question": input_text})
|
| 41 |
st.write("**Chatbot Response:**")
|
| 42 |
st.write(response)
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
st.error(f"An error occurred: {e}")
|
| 51 |
|
| 52 |
st.sidebar.header("Conversation History")
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
st.sidebar.write(f"**{role}:** {message['text']}")
|
|
|
|
| 1 |
+
import os, asyncio, streamlit as st
|
| 2 |
+
from dotenv import load_dotenv
|
| 3 |
from langchain_core.prompts import ChatPromptTemplate
|
| 4 |
from langchain_core.output_parsers import StrOutputParser
|
| 5 |
from langchain_google_genai import ChatGoogleGenerativeAI
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
+
# ------------------------------------------------------------------ #
|
| 8 |
+
# 1️⃣ Ensure the current thread owns an asyncio event‑loop
|
| 9 |
+
try:
|
| 10 |
+
asyncio.get_running_loop()
|
| 11 |
+
except RuntimeError:
|
| 12 |
+
asyncio.set_event_loop(asyncio.new_event_loop())
|
| 13 |
+
if os.name == "nt": # optional Windows tweak
|
| 14 |
+
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
|
| 15 |
+
# ------------------------------------------------------------------ #
|
| 16 |
|
| 17 |
+
load_dotenv() # reads .env into os.environ
|
| 18 |
+
os.environ["GOOGLE_API_KEY"] = os.getenv("api") # keep your .env key name
|
| 19 |
|
| 20 |
+
system_template = "You are a helpful assistant. Please respond to the user queries."
|
| 21 |
prompt_template = ChatPromptTemplate.from_messages([
|
| 22 |
('system', system_template),
|
| 23 |
+
('user', 'Question: {question}')
|
| 24 |
])
|
| 25 |
|
| 26 |
+
model = ChatGoogleGenerativeAI(model="gemini-pro",
|
| 27 |
+
convert_system_message_to_human=True)
|
| 28 |
+
parser = StrOutputParser()
|
| 29 |
+
chain = prompt_template | model | parser
|
| 30 |
|
| 31 |
st.title("LangChain Chatbot Demo")
|
| 32 |
st.markdown("""
|
| 33 |
+
Welcome to the LangChain Chatbot Demo!
|
| 34 |
+
Type your query below and get responses powered by Google's Generative AI.
|
| 35 |
+
""")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
+
input_text = st.text_input("Enter your question:")
|
| 38 |
|
| 39 |
if input_text:
|
| 40 |
+
with st.spinner("Generating response…"):
|
| 41 |
+
try:
|
| 42 |
response = chain.invoke({"question": input_text})
|
| 43 |
st.write("**Chatbot Response:**")
|
| 44 |
st.write(response)
|
| 45 |
+
|
| 46 |
+
st.session_state.setdefault("history", []).extend([
|
| 47 |
+
{"role": "user", "text": input_text},
|
| 48 |
+
{"role": "chatbot", "text": response},
|
| 49 |
+
])
|
| 50 |
+
except Exception as e:
|
| 51 |
+
st.error(f"An error occurred: {e}")
|
|
|
|
| 52 |
|
| 53 |
st.sidebar.header("Conversation History")
|
| 54 |
+
for msg in st.session_state.get("history", []):
|
| 55 |
+
role = "You" if msg["role"] == "user" else "Chatbot"
|
| 56 |
+
st.sidebar.write(f"**{role}:** {msg['text']}")
|
|
|